Replication in MariaDB allows changes made in one database or server (known as the master) to be copied to another database or server (known as the slave).
Setting a master-slave relationship involves several steps:
Step 1: Configure the Master Server
1. Edit the MariaDB configuration file (typically found in `/etc/mysql/my.cnf`) and make sure the following lines are in the `[mysqld]` section:
\`\`\` [mysqld] server\_id=1 log\_bin=mysql-bin binlog_do_db=your_database_name \`\`\` Replace “your_database_name” with the name of the database you want to replicate.1. Restart the MariaDB service: `service mysql restart`
1. Log into MySQL as root: `mysql -u root -p`
1. Define a Replication User which the Slave will use to connect to the Master:
\`\`\` GRANT REPLICATION SLAVE ON . TO ‘replication’@’%‘ IDENTIFIED BY ‘password’; FLUSH PRIVILEGES; \`\`\` Replace “password” with a secure password.1. Get the Master’s binary log coordinates:
\`\`\`SHOW MASTER STATUS; \`\`\` Note down the file and position numbers, you will need these for the Slave configuration.Step 2: Configure the Slave Server
1. Edit the MariaDB configuration file (typically found in `/etc/mysql/my.cnf`) and make sure the following lines are in the `[mysqld]` section:
\`\`\` [mysqld] server\_id=2 relay\_log=mysql-relay-bin log\_bin=mysql-bin replicate_do_db=your_database_name \`\`\` Replace “your_database_name” with the name of the database you want to replicate.1. Restart the MariaDB service: `service mysql restart`
1. Log into MySQL as root: `mysql -u root -p`
1. Stop the slave if running: `STOP SLAVE;`
1. Set the slave to replicate the master:
\`\`\` CHANGE MASTER TO MASTER_HOST=‘master_server\_ip’, MASTER\_USER=‘replication’, MASTER\_PASSWORD=‘password’, MASTER_LOG_FILE=‘log_file_name‘ MASTER_LOG_POS=log_file_position; \`\`\` Replace ‘master_server_ip’ with the master server’s IP address, ‘password’ with the replication user’s password, ‘log_file_name’ with the name of the log file you noted down earlier, and ‘log_file_position’ with the log file position you noted down.1. Start the slave: `START SLAVE;`
Check the Slave status with `SHOW SLAVE STATUS\G;`. If everything is configured correctly, you should see ‘Waiting for master to send event’ under ‘Slave_IO_State’, and both ‘Slave_IO_Running’ and ‘Slave_SQL_Running’ should be ‘Yes’.
Remember to open appropriate ports and allow connections if necessary on your firewall.
Also, please ensure that your data is protected and keep your data secure.
Please back up your data before you set up it. Making changes to your database without backup can result in data loss.