help centre
For more info visit status.atmail.com

How can we help?


Search our knowledge base for answers to
common questions and latest updates.



My activities New request

Follow

multi server setup guide - MariaDB master-slave replication

Stewart -

PROBLEM
How can I setup MariaDB master-slave replication for my atmail multiserver setup?

ENVIRONMENT

  • atmail on-premises mail server installations: 7.8+ only

CAUSE
atmail has not yet been installed.

RESOLUTION

Note: There are two sub-tutorials - one for MySQL and one for MariaDB. We highly recommend that you use the MariaDB documentation if running atmail 7.8+ on CentOS 7. The guide for MySQL setup can be found here.

Using MariaDB master-slave replication (atmail 7.8+ only)

Test systems details:

  • MariaDB Master: CentOS 7 64bit Minimal Server
  • Master IP Address: 192.168.1.1
  • MariaDB Slave: CentOS 7 64bit Minimal Server
  • Slave IP Address: 192.168.1.2

Install MariaDB on Master and Slave server

Run the following command on Master and Slave server to install MariaDB.

yum install mariadb-server mariadb

Start MariaDB service and set it to start automatically on every reboot:

systemctl start mariadb
systemctl enable mariadb

Set MariaDB root password on Master and Slave server

By default, MariaDB/MySQL root password is empty. To prevent unauthorized access, set the root user password.

Enter the following command to setup mysql root user password:

mysql_secure_installation

Sample output:

/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y ## Enter Y and press Enter
New password:   ## Enter new password
Re-enter new password:  ## Enter password again
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y  ## Enter Y and press Enter
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y  ## Enter Y and press Enter
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y  ## Enter Y and press Enter
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y  ## Enter Y and press Enter
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Configure MariaDB Master

The first step is to allow mysql default port “3306” through Firewall or Router.

As we use CentOS 7, we can allow the port as shown below.

firewall-cmd --permanent --add-port=3306/tcp

Reload firewall:

firewall-cmd --reload

Edit /etc/my.cnf file,

vi /etc/my.cnf

Add the following lines under [mysqld] section:

[mariadb]
server_id=1
log-basename=master
log-bin
binlog-format=row
binlog-do-db=atmail7
[...]

Here atmail7 is the database name to be replicated to the Slave system.

Once you are done that, restart the MariaDB service using command:

systemctl restart mariadb

Now login to MariaDB as root user:

mysql -u root -p

Create a Slave user and password. For example, we will use atmailuser as Slave username and changeme as password:

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'atmailuser'@'%' IDENTIFIED BY 'changeme';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW MASTER STATUS;
+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000001 |      460 | atmail7      |                  |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> exit
Bye

Note down the file (mariadb-bin.000001) and position number (460), you may need these values later.

Backup Master server database and transfer it to the Slave

Enter the following command to dump all Master databases and save them. We will transfer these databases to Slave server later:

mysqldump --all-databases --user=root --password --master-data > masterdatabase.sql

This will create a file called masterdatabase.sql in your current working directory. This will take some time depending upon the databases size.

Again login to MySQL as root user:

mysql -u root -p

Unlock the tables:

MariaDB [(none)]> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit
Bye

Copy the masterdatabase.sql file to your Slave server.

Copy this file to the /home folder of your slave server:

scp masterdatabase.sql root@192.168.1.2:/home

192.168.1.2 is the MariaDB slave server IP address in this example.

Configure MariaDB Slave

We have done the Master side installation. Now we have to start on the Slave side. Install MariaDB packages on the Slave server as stated in the MariaDB installation section above. Also, don’t forget to allow the port “3306” through the firewall/router.

Edit file /etc/my.cnf,

vi /etc/my.cnf

Add the following entries under [mysqld] section as shown below.

[mariadb]
server_id = 2
replicate-do-db=atmail7
[...]

Here, atmail7 is the database name of Master server. Be mindful that you should use different server-id for both master and slave servers.

Save and exit the file.

Then, Import the master database using command:

mysql -u root -p < /home/masterdatabase.sql 

We have already copied the masterdatabase.sql file from the master server to /home directory of the slave server.

Restart MariaDB service.

systemctl restart mariadb

Log into MariaDB as the root user using the command:

mysql -u root -p

Tell the Slave server where to look for the Master log file which we have created on the Master server using the command SHOW MASTER STATUS; (File –mariadb-bin.000001 and Position – 460). Make sure that you changed the Master server IP address, username and password as your own:

MariaDB [(none)]> STOP SLAVE;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.1', MASTER_USER='atmailuser', MASTER_PASSWORD='changeme', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=460;
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.1
                  Master_User: atmailuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.000001
          Read_Master_Log_Pos: 460
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 531
        Relay_Master_Log_File: mariadb-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: atmail7
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 460
              Relay_Log_Space: 827
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

Test MariaDB Replication

Master side:

Go to your MariaDB master server and log into MariaDB prompt using the command:

mysql -u root -p

Create a database called atmail7, add some tables and entries in it. Be mindful that the newly created name should be same as in the my.cnf file of both Master and Slave server.

MariaDB [(none)]> create database atmail7;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use atmail7;
Database changed

MariaDB [atmail7]> create table sample (c int);
Query OK, 0 rows affected (0.01 sec)

MariaDB [atmail7]> insert into sample (c) values (1);
Query OK, 1 row affected (0.01 sec)

MariaDB [atmail7]> select * from sample;
+------+
| c    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

MariaDB [atmail7]> 

Slave side:

Go to slave server and check the above created entries have been replicated in the slave server database.

Log into MariaDB prompt as root user:

mysql -u root -p

Run the following commands to verify whether the entries have been replicated correctly.

MariaDB [(none)]> use atmail7;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [atmail7]> select * from sample;
+------+
| c    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

MariaDB [atmail7]>

Now the tables created in the Master server have been automatically replicated to the Slave server.

Installing atmail's First Instance

Once MariaDB master-slave replication has been setup move onto the installation and filesystem replication guide.

Have more questions? Submit a request

Comments


Contact our support team


+61 (7) 5357 6605       support@atmail.com