In this step, we will configure a MySQL server to act as a slave, replicating data from the master server configured in the previous step. This involves configuring the slave server's settings and establishing a connection to the master.
-
Configure the Slave Server:
First, we need to configure the slave server with a unique server_id
and enable relay logging. Relay logs are used by the slave server to store the binary log events received from the master before applying them to its own database.
Edit the MySQL configuration file on the slave server (usually located at /etc/mysql/mysql.conf.d/mysqld.cnf
):
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Add or modify the following lines in the [mysqld]
section:
server_id = 2
relay_log = mysql-relay-bin
server_id = 2
: This sets a unique ID for the slave server. It must be different from the master server's server_id
.
relay_log = mysql-relay-bin
: This enables relay logging and sets the base name for the relay log files.
The mysqld
section of your configuration file should now look similar to this:
[mysqld]
#
## * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
server_id = 2
relay_log = mysql-relay-bin
## ... other configurations ...
Press Ctrl+X
, then Y
, then Enter
to save the changes and exit nano
.
-
Restart the MySQL server on the slave:
Restart the MySQL server to apply the changes:
sudo /etc/init.d/mysql restart
You should see output similar to:
[ ok ] Restarting mysql (via systemctl): mysql.service.
-
Create a Replication User on the Master Server:
On the master server, log in to the MySQL server as the root user:
sudo mysql -u root
Create a dedicated user for replication. Replace 'slave_user'
with your desired username and 'password'
with a strong password. Also, replace '%'
with the IP address of your slave server for better security. If you are unsure of the slave server's IP address, you can use '%'
to allow connections from any host (not recommended for production environments).
CREATE USER 'slave_user'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%';
FLUSH PRIVILEGES;
CREATE USER 'slave_user'@'%' IDENTIFIED BY 'password'
: Creates a new MySQL user named slave_user
that can connect from any host ('%'
).
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%'
: Grants the REPLICATION SLAVE
privilege to the slave_user
on all databases and tables. This privilege is required for the slave server to request binary log updates from the master.
FLUSH PRIVILEGES
: Reloads the grant tables to apply the new privileges.
Exit the MySQL monitor on the master server:
exit
-
Configure the Slave to Connect to the Master:
On the slave server, log in to the MySQL server as the root user:
sudo mysql -u root
Tell the slave server how to connect to the master server. Replace 'master_host'
with the IP address or hostname of your master server, 'slave_user'
with the replication username you created, and 'password'
with the replication password.
CHANGE MASTER TO
MASTER_HOST='master_host',
MASTER_USER='slave_user',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=4;
MASTER_HOST
: The hostname or IP address of the master server.
MASTER_USER
: The username the slave will use to connect to the master.
MASTER_PASSWORD
: The password for the replication user.
MASTER_LOG_FILE
: The name of the master's binary log file to start replication from. You can find this by running SHOW MASTER STATUS;
on the master server. For a fresh setup, it's usually mysql-bin.000001
.
MASTER_LOG_POS
: The position in the binary log file to start replication from. You can find this by running SHOW MASTER STATUS;
on the master server. For a fresh setup, it's usually 4
.
Important: You need to determine the correct MASTER_LOG_FILE
and MASTER_LOG_POS
from the master server. On the master server, execute the following query:
SHOW MASTER STATUS;
The output will show the File
(which corresponds to MASTER_LOG_FILE
) and Position
(which corresponds to MASTER_LOG_POS
). Use these values in the CHANGE MASTER TO
command on the slave server.
After executing the CHANGE MASTER TO
command, start the slave:
START SLAVE;
Exit the MySQL monitor on the slave server:
exit
You have now configured the slave server to replicate data from the master server. In the next step, we will verify that replication is working correctly.