Skip to main content

Setup MariaDB Master-Slave Replication In CentOS 7

 

Setup MariaDB Master-Slave Replication In CentOS 7



Why MariaDB Replication?

As you may know, MariaDB is a drop in replacement for MySQL. It is a robust, scalable and reliable SQL server that comes rich set of enhancements.

MariaDB replication is a method to store multiple copies of data on many systems, and the data will automatically be copied from one database (Master) to another database (Slave). If one server goes down, the clients still can access the data from another (Slave) server database.


In this article, let us see how to configure MariaDB Master-Slave replication in CentOS 7. This method will work on all linux distributions, including RHEL, CentOS, Ubuntu, and openSUSE etc. All you need to know is how to install MariaDB in the specific distribution you use.


Here is my testing systems details:

MariaDB Master: CentOS 7 64bit Minimal Server
Master IP Address: 192.168.1.150/24
MariaDB Slave: CentOS 7 64bit Minimal Server
Slave IP Address: 192.168.1.151/24

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 let 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. So, to prevent unauthorized access, let us set 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!



After installing MariaDB on both servers, let us jump into the replication setup.


First, we will configure Master server.


Configure MariaDB Master

The first and foremost 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 rules using command:

firewall-cmd --reload

Then, Edit /etc/my.cnf file,


nano /etc/my.cnf

And add the following lines under [mysqld] section:

[mysqld]

datadir = /var/lib/mysql

server_id=1

log-basename=master

#log-bin

binlog-format=row

binlog-do-db=DATABASE

log_bin = /var/log/mysql/mysql-bin.log

bind-address = 0.0.0.0

socket = /var/lib/mysql/mysql.sock

log-error = /var/log/mysqld/mysqld.log

log-bin = mysql-bin

log-warnings=3

sql_mode="NO_ENGINE_SUBSTITUTION"

#

# include all files from the config directory

#

!includedir /etc/my.cnf.d

Save ctrl+x

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

Once you are done, 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 sk as Slave username and centos as password:

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

MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'sk'@'%' IDENTIFIED BY 'centos';
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 | DATABASE      |                  |
+--------------------+----------+--------------+------------------+
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
And, 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.

Here, I am going to copy this file to the /root folder of my slave server. So the command will be:

scp masterdatabase.sql root@192.168.1.151:/root
Remember, 192.168.1.151 is my MariaDB slave server IP address.

Configure MariaDB Slave

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

Then, Edit file /etc/my.cnf,


nano  /etc/my.cnf

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


[mysqld]

server-id = 2

replicate-do-db=DATABASE

[...]

Here, DATABASE 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 < /root/masterdatabase.sql 

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

Restart MariaDB service to take effect the changes.

systemctl restart mariadb

Now, log in to MariaDB as root user using command:

mysql -u root -p

And tell the Slave server to where to look for Master log file which is we have created on 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.150', MASTER_USER='sk', MASTER_PASSWORD='centos', MASTER_LOG_FILE='mariadb-bin.000001', MASTER_LOG_POS=460;

Query OK, 0 rows affected (0.03 sec)


MariaDB [(none)]> SLAVE START;

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.150

                  Master_User: sk

                  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: unixmen

          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 in to mysql prompt using command:

mysql -u root -p

Then, create a database called DATABASE, and 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 DATABASE;

Query OK, 1 row affected (0.00 sec)


MariaDB [(none)]> use DATABASE;

Database changed


MariaDB [DATABASE]> create table sample (c int);

Query OK, 0 rows affected (0.01 sec)


MariaDB [DATABASE]> insert into sample (c) values (1);

Query OK, 1 row affected (0.01 sec)


MariaDB [DATABASE]> select * from sample;

+------+

| c    |

+------+

|    1 |

+------+

1 row in set (0.00 sec)


MariaDB [DATABASE]> 

Slave side:

Now, go to slave server and check the above created entries have been replicated in slave server database.

Log in to MariaDB prompt as root user:

mysql -u root -p

Then, run the following commands to verify whether the entries have been replicated correctly.


MariaDB [(none)]> use DATABASE;

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 [DATABASE]> select * from sample;

+------+

| c    |

+------+

|    1 |

+------+

1 row in set (0.00 sec)


MariaDB [DATABASE]>

That’s it. Now the tables created in the Master server have been automatically replicated to the Slave server.








Comments

Popular posts from this blog

Vicidial Scratch installation Alma -9

Step 1 – Download the dependencies   hostnamectl set-hostname xxxxxx.xxxxx.xxx ### Use YOUR SubDomain vi /etc/hosts ##Change domain name for actual server ip (xxx.xxx.xxx.xxx   complete domain name    subdomain only) timedatectl set-timezone Asia/Kolkata yum check-update yum update -y yum -y install epel-release yum update -y yum install git -y yum install -y kernel* sudo dnf install kernel-devel-$(uname -r) -y #Disable SELINUX sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config     reboot Step 2 – Run the Script cd /usr/src/ git clone https://github.com/manish23k/vicidial-install-scripts cd vicidial-install-scripts chmod +x alma-rocky9-ast16.sh ./alma-rocky9-ast16.sh Or the Asterisk 18 version: chmod +x alma-rocky9-ast18.sh ./alma-rocky9-ast18.sh ####For PHP 8 use this script. chmod +x main-installer-php8.sh ./main-installer-php8.sh

How to delete old call logs and other logs in vicidial or goautodial.

Step 1 : SSH to the server using the Putty Step 2:   login to mysql by typing   mysql -p                    (if you dont know password try below command )               mysql -ucron -p1234 Step 3 : select the asterisk database by typing               use asterisk step 4: Run the below command to check total disk occupied by asterisk database                SELECT table_schema AS "asterisk", ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" FROM information_schema.TABLES GROUP BY table_schema; Step 5 : Run the below command to check disk space consumed by each table in asterisk database SELECT table_name AS "Table", ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)" FROM information_schema.TABLES WHERE table_schema = "asterisk" ORDER BY (data_length + inde...

Install the No-IP client (Dynamic Update Client) on AlmaLinux 9

 Install the No-IP client (Dynamic Update Client) on AlmaLinux 9 1. Install Required Packages First, install the necessary packages to compile the client from source. sudo dnf install gcc make wget tar 2. Download the No-IP DUC (Dynamic Update Client) Download the latest version of the No-IP DUC. cd /opt wget https://www.noip.com/client/linux/noip-duc-linux.tar.gz 3. Extract the Downloaded File Extract the downloaded tarball. tar -zxvf noip-duc-linux.tar.gz cd noip-2.1.9-1/ 4. Compile the Client Run the following commands to compile the client. sudo make install 5. Configure the No-IP Client After installation, you’ll be prompted to enter your No-IP account credentials (email and password) and choose the hostname(s) to update. If not, you can manually run the configuration: sudo /usr/local/bin/noip2 -C 6. Start the No-IP Client To start the client: sudo /usr/local/bin/noip2 7. Verify Installation To confirm that the No-IP DUC is running, use: sudo /usr/local/bin/noip2 -S 8. Set Up...