Skip to main content

Add a Secondary Database

Learn how to add and connect a secondary database that works with the Multi Communications Server to reduce the load on the main server.

Info

Before you start​

If you use a domain or hostname instead of an IP address for database communication in MariaDB, make sure you review the guidance below.

  • If you do not use an IP address, you may not be able to run Create user or Grant.

  • If you communicate using a domain or hostname, make sure a user and permissions are registered for localhost.

  • If you installed BioStar X using Express Installation, the root account is registered only for localhost. Also register the user and permissions for the server IP address or 127.0.0.1.

How to register​

Check whether the root user with Host 127.0.0.1 has a password; if not, set one and grant permissions.

  1. Check whether the root user with Host 127.0.0.1 has a password.

    SELECT * FROM mysql.user;

    If it is blank, no password is set, so make sure to run the query in the next entry to add a password and grant permissions.

  2. Set a password and grant permissions for the root user with Host 127.0.0.1.

    ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY '<Main DB AC Schema Password>';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';

    CREATE USER '<Main DB AC User>'@'127.0.0.1' IDENTIFIED BY '<Main DB AC Schema Password>';
    GRANT ALL PRIVILEGES ON *.* TO '<Main DB AC User>'@'127.0.0.1';
    CREATE USER '<Main DB AC User>'@'::1' IDENTIFIED BY '<Main DB AC Schema Password>';
    GRANT ALL PRIVILEGES ON *.* TO '<Main DB AC User>'@'::1';

    CREATE USER '<Main DB TA User>'@'127.0.0.1' IDENTIFIED BY '<Main DB AC Schema Password>';
    GRANT ALL PRIVILEGES ON *.* TO '<Main DB TA User>'@'127.0.0.1';
    CREATE USER '<Main DB TA User>'@'::1' IDENTIFIED BY '<Main DB AC Schema Password>';
    GRANT ALL PRIVILEGES ON *.* TO '<Main DB TA User>'@'::1';

    CREATE USER '<Main DB VE User>'@'127.0.0.1' IDENTIFIED BY '<Main DB AC Schema Password>';
    GRANT ALL PRIVILEGES ON *.* TO '<Main DB VE User>'@'127.0.0.1';
    CREATE USER '<Main DB VE User>'@'::1' IDENTIFIED BY '<Main DB AC Schema Password>';
    GRANT ALL PRIVILEGES ON *.* TO '<Main DB VE User>'@'::1';

    FLUSH PRIVILEGES;
    Example
    ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY 'admin1234!';
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';
    FLUSH PRIVILEGES;

Set up communication by IP address​

Use the settings below to force communication by IP address.

  1. After you install BioStar X, go to the following path.

    C:\Program Files\BioStar X\ta\mariadb-11.4.4-winx64

  2. Open the my.cnf file with administrator privileges and add the skip-name-resolve option to the [mysqld] section.

    [mysqld]
    port = 3312
    character-set-server=utf8
    collation-server=utf8_unicode_ci
    socket = /tmp/mysql.sock
    skip-external-locking
    key_buffer_size = 32M
    max_allowed_packet = 64M
    ...
    skip-name-resolve
  3. Restart the MariaDB service.

Add a secondary database​

Learn how to add a secondary database. Depending on your database type, use one of two methods: MariaDB or SQL Server.

MariaDB​

Main database​

Create an account and grant permissions so the new secondary database can access the main database.

CREATE USER '<Main DB AC Schema User>'@'<Sub DB IP>' IDENTIFIED BY '<Main DB AC Schema Password>';

GRANT ALL PRIVILEGES ON <Main DB AC Schema>.* TO '<Main DB AC Schema User>'@'<Sub DB IP>';
FLUSH PRIVILEGES;
Example
CREATE USER 'biostarx_ac_user'@'192.168.12.42' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON biostar2_ac.* TO 'biostarx_ac_user'@'192.168.12.42';
FLUSH PRIVILEGES;

Secondary database​

Create an account and grant permissions so the main database can access the new secondary database.

CREATE USER '<Sub DB AC Schema User>'@'<Main DB IP>' IDENTIFIED BY '<Sub DB AC Schema Password>';

GRANT ALL PRIVILEGES ON <Sub DB AC Schema>.* TO '<Sub DB AC Schema User>'@'<Main DB IP>';
FLUSH PRIVILEGES;
Example
CREATE USER 'biostarx_ac_user'@'192.168.12.161' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON biostar2_ac.* TO 'biostarx_ac_user'@'192.168.12.161';
FLUSH PRIVILEGES;

Check whether FederatedX is enabled​

In both the main and secondary databases, run the command below to check whether the FederatedX storage engine is enabled.

SHOW ENGINES;

If the query result shows Engine : FEDERATED, Support : YES, it is already installed. If the FederatedX storage engine is not installed, run the query below.

INSTALL PLUGIN federated SONAME 'ha_federatedx';
Info

Check whether the ha_federatedx.dll file exists in the lib/plugin folder under the path where MariaDB is installed. Typically, the dll file is located at C:\Program Files\MariaDB {version}\lib\plugin.

Register a server alias​

Register a server alias so that the federatedX table created in the secondary database can access the source table in the main database.

CREATE SERVER 'default' FOREIGN DATA WRAPPER mysql OPTIONS (HOST '<Main DB IP>', PORT <Main DB PORT>, DATABASE '<Main DB AC Schema>', USER '<Main DB AC USER>', PASSWORD '<Main DB AC USER Password>');

-- Confirm registration
SELECT * FROM mysql.servers;

To change the Host, Port, DB, User, or Password information for a registered server name, modify it using an Alter statement. You can also change only some of the information.

ALTER SERVER 'default' OPTIONS (HOST '<Main DB IP>', PORT <Main DB PORT>, DATABASE '<Main DB AC Schema>', USER '<Main DB AC USER>', PASSWORD '<Main DB AC USER Password>');
Example
CREATE SERVER 'default' FOREIGN DATA WRAPPER mysql OPTIONS (HOST '192.168.12.161', PORT 3312, DATABASE 'biostar2_ac', USER 'biostarx_ac_user', PASSWORD 'password');

ALTER SERVER 'default' OPTIONS (HOST '192.168.12.161', PORT 3312, DATABASE 'biostar2_ac', USER 'biostarx_ac_usermt__fttid__', PASSWORD 'password');

Follow the steps below to link the required tables in the secondary database to the tables in the main database.

  • The following query generates CREATE TABLE statements in the main database.

    select
    concat(
    'CREATE TABLE IF NOT EXISTS <SubDB_AC_Database_Schema>.', table_name,
    ' ENGINE=FEDERATED ',
    'CONNECTION=''default/', table_name, ''';'
    ) as create_table_sql
    from information_schema.TABLES
    where TABLE_SCHEMA = '<MainDB_AC_Database_Schema>'
    AND TABLE_TYPE = 'BASE TABLE'
    AND NOT TABLE_NAME REGEXP '^t_lg[0-9]{6}$'
    AND NOT TABLE_NAME REGEXP '^t_almevt[0-9]{6}$'
    AND NOT TABLE_NAME REGEXP '^t_lgalmtrstrc[0-9]{6}$'
    ORDER BY TABLE_NAME;
    Info

    Due to the nature of FederatedX, set Table_type to 'BASE TABLE'. VIEW type also works, but is not recommended.

Set up Service Manager​

  1. Run BioStar X Service Manager. (Start → BioStar X → BioStar X Service Manager)

  2. Click the DATABASE menu in the left sidebar.

  3. Click + Add Database in the upper-right corner of the screen.

  4. In the Add Database screen, enter each field.

    • Name: Enter the database name.

    • Description: Enter the database description.

    • DB Type: Select the database type. (Maria, MS SQL)

    • Host: Enter the hostname or IP address of the database server.

    • Port: Enter the port number of the database server.

    • AC / TA: Enter the name, user, and password for the AC and TA databases.

  5. Click Test Connection in the upper-right corner to check if the database is connected properly.

  6. To save the settings, click Save in the upper-right corner of the screen.

SQL Server​

Grant permissions for the main database​

Using an account with sysadmin privileges, grant the following permissions on the main database.

GRANT ALTER ANY LINKED SERVER TO [<MAIN DATABASE AC USER>];
GRANT ALTER ANY LOGIN TO [<MAIN DATABASE AC USER>];
Example
GRANT ALTER ANY LINKED SERVER TO [biostar_x_user];
GRANT ALTER ANY LOGIN TO [biostar_x_user];

Grant permissions for the secondary database​

  1. sysadmin privileges are required to connect User Mapping for the tables and users in the secondary database. Then add the db_owner role.

  2. Grant the following permissions to the database you add.

    GRANT ALTER ANY LINKED SERVER TO [<SUB DATABASE AC USER>];
    GRANT ALTER ANY LOGIN TO [<SUB DATABASE AC USER>];
    Example
    GRANT ALTER ANY LINKED SERVER TO [biostar_x_user_sub];
    GRANT ALTER ANY LOGIN TO [biostar_x_user_sub];

Set up Service Manager​

To add a secondary database, use BioStar X Service Manager.

  1. Run BioStar X Service Manager. (Start → BioStar X → BioStar X Service Manager)

  2. Click the DATABASE menu in the left sidebar.

  3. Click + Add Database in the upper-right corner of the screen.

  4. In the Add Database screen, enter each field.

    • Name: Enter the database name.

    • Description: Enter the database description.

    • DB Type: Select the database type. (Maria, MS SQL)

    • Host: Enter the hostname or IP address of the database server.

    • Port: Enter the port number of the database server.

    • AC / TA: Enter the name, user, and password for the AC and TA databases.

  5. Click Test Connection in the upper-right corner to check if the database is connected properly.

  6. To save the settings, click Save in the upper-right corner of the screen.

Info
  • After setup is complete, Linked Server settings are applied automatically to the main and secondary databases.

  • When you edit database information, the Linked Server settings change automatically. When you edit the main database, the updated information is applied to the Linked Server settings for all registered secondary databases. When you edit a secondary database, the updated information is applied only to the main database and that secondary database.

Generate a Linked Temp Table query​

Run the query below in the main database to generate a Linked Temp Table query to run in the secondary database.

USE [<AC Schema>];
GO
DECLARE @LinkedServer sysname = N'default';
DECLARE @SourceSchema sysname = N'dbo';
DECLARE @TargetSchema sysname = N'dbo';

SELECT
'IF OBJECT_ID(N''' + QUOTENAME(@TargetSchema,'') + '.' + QUOTENAME(t.name,'') + ''', ''SN'') IS NOT NULL '
+ 'DROP SYNONYM ' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(t.name) + ';'
+ 'CREATE SYNONYM ' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(t.name)
+ ' FOR ' + QUOTENAME(@LinkedServer) + '.' + QUOTENAME(DB_NAME()) + '.' + QUOTENAME(s.name) + '.' + QUOTENAME(t.name) + ';'
AS recreate_synonym_sql
FROM sys.tables AS t
JOIN sys.schemas AS s
ON s.schema_id = t.schema_id
WHERE s.name = @SourceSchema
AND t.is_ms_shipped = 0
AND t.name NOT LIKE 't_lg[0-9][0-9][0-9][0-9][0-9][0-9]'
AND t.name NOT LIKE 't_almevt[0-9][0-9][0-9][0-9][0-9][0-9]'
AND t.name NOT LIKE 't_lgalmtrstrc[0-9][0-9][0-9][0-9][0-9][0-9]'
ORDER BY t.name;
Example
-- Example
USE [main_ac_x_215];
GO
DECLARE @LinkedServer sysname = N'default'; -- Linked Server name set in the sub DB
DECLARE @SourceSchema sysname = N'dbo'; -- Schema to target in the main DB
DECLARE @TargetSchema sysname = N'dbo'; -- Schema to create synonyms in the sub DB (reflected in the output string)

SELECT
'IF OBJECT_ID(N''' + QUOTENAME(@TargetSchema,'') + '.' + QUOTENAME(t.name,'') + ''', ''SN'') IS NOT NULL '
+ 'DROP SYNONYM ' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(t.name) + ';'
+ 'CREATE SYNONYM ' + QUOTENAME(@TargetSchema) + '.' + QUOTENAME(t.name)
+ ' FOR ' + QUOTENAME(@LinkedServer) + '.' + QUOTENAME(DB_NAME()) + '.' + QUOTENAME(s.name) + '.' + QUOTENAME(t.name) + ';'
AS recreate_synonym_sql
FROM sys.tables AS t
JOIN sys.schemas AS s
ON s.schema_id = t.schema_id
WHERE s.name = @SourceSchema
AND t.is_ms_shipped = 0
AND t.name NOT LIKE 't_lg[0-9][0-9][0-9][0-9][0-9][0-9]'
AND t.name NOT LIKE 't_almevt[0-9][0-9][0-9][0-9][0-9][0-9]'
AND t.name NOT LIKE 't_lgalmtrstrc[0-9][0-9][0-9][0-9][0-9][0-9]'
ORDER BY t.name;

Run the query in the secondary database​

Copy the entire CREATE SYNONYM query that you generated earlier. Connect to the secondary database server you are adding, and then write and run the query as shown below. They are stored in Synonyms under the AC schema of the secondary database.

use [<Sub DB AC Database>];

IF OBJECT_ID(N'[dbo].[T_ACSGR]', 'SN') IS NOT NULL DROP SYNONYM [dbo].[T_ACSGR];CREATE SYNONYM [dbo].[T_ACSGR] FOR [default].[main_ac_x_215].[dbo].[T_ACSGR];
IF OBJECT_ID(N'[dbo].[T_ACSGRLVLS]', 'SN') IS NOT NULL DROP SYNONYM [dbo].[T_ACSGRLVLS];CREATE SYNONYM [dbo].[T_ACSGRLVLS] FOR [default].[main_ac_x_215].[dbo].[T_ACSGRLVLS];
IF OBJECT_ID(N'[dbo].[T_ACSGRSENT]', 'SN') IS NOT NULL DROP SYNONYM [dbo].[T_ACSGRSENT];CREATE SYNONYM [dbo].[T_ACSGRSENT] FOR [default].[main_ac_x_215].[dbo].[T_ACSGRSENT];
IF OBJECT_ID(N'[dbo].[T_ACSGRUSS]', 'SN') IS NOT NULL DROP SYNONYM [dbo].[T_ACSGRUSS];CREATE SYNONYM [dbo].[T_ACSGRUSS] FOR [default].[main_ac_x_215].[dbo].[T_ACSGRUSS];
...
Was this page helpful?