A certain database in the production environment of the company needs to perform master-slave replication on the basis of the existing data in the master database. When it comes to the production environment, be cautious, so first test it in the test environment and record the operation process by the way.
1. Install the slave database
The main library already exists, the intranet IP is 10.200.9.183, and the slave library is ready to be built on 10.200.9.188.
(1) Download and unzip the database compression package
The database version used is mariadb-10.0.33, download method:
wget https://downloads.mariadb.com/MariaDB/mariadb-10.0.33/bintar-linux-x86_64/mariadb-10.0.33-linux-x86_64.tar.gz
Unzip the database compressed package:
tar -xzvf mariadb-10.0.33-linux-x86_64.tar.gz
Move to the specified location:
mv mariadb-10.0.33-linux-x86_64 /data/mariadb-10.0.33
(2) Change configuration
Backup the default database configuration file under /etc:
mv /etc/my.cnf /etc/my.cnf.bak
Copy the configuration file template in the database to /etc/
cp support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
Edit configuration file
vim /etc/my.cnf
Change the following
basedir=/data/mariadb-10.0.33
datadir=/data/mariadb-10.0.33/data
(3) Create a mysql user and change permissions
Create a mysql user:
useradd -s /sbin/nologin -M mysql
Change the user and group of all mysql files to mysql
chown -R mysql:mysql /data/mariadb-10.0.33
Specify user and file path to initialize mysql
./scripts/mysql_install_db --basedir=/data/mariadb-10.0.33 --datadir=/data/mariadb-10.0.33/data/ --user=mysql
(4) Set the startup script to start mysql
Mariadb provides a startup script. In the support-files directory of the installation directory, the file name is mysql.server, and copy it to /etc/init.d
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
Start mysql
/etc/init.d/mysqld start
or
service mysqld start
mysql startup self-start setting:
chkconfig --add mysqld
chkconfig mysqld on
Check whether mysql is added to the startup list:
chkconfig --list
As shown below, the addition is successful
(5) mysql root user authorization:
Login to mysql
/data/mariadb-10.0.33/bin/mysql -u root
Authorization:
grant all privileges on *.* to 'root'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'localhost' identified by '123456';
grant all privileges on *.* to 'root'@'127.0.0.1' identified by '123456';
Refresh permissions:
flush privileges;
(6) Add the mysql command to the environment variable
Edit the /etc/profile file:
vim /etc/profile
Add the following to the last line
export PATH=$PATH:/data/mariadb-10.0.33/bin
Refresh environment variables
source /etc/profile
2. Main library settings
(1) Edit the main library configuration file:
vim /etc/my.cnf
Configure the binlog file name:
log-bin = mysql-bin
Set the server-id (server-id must be unique and cannot be duplicated from the library):
server-id = 1
Set master-slave synchronization to synchronize only a certain database
binlog-do-db = newenergy
Set the binlog expiration time:
expire_logs_days = 2
Set log format
binlog_format = mixed
- statement saves the SQL statement
- row saves the impact record data
- The combination of the first two is
recommended to be set to mxed mode
(2) Create an account with copy permissions
IP is from the library IP
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@' 10.200.9.188' IDENTIFIED BY 'replpass';
Refresh permissions
FLUSH PRIVILEGES;
Restart the database
service mysqld restart
4. Set from the library
(1) Edit the configuration file from the library
vim /etc/my.cnf
Set relay log relay-log
relay-log = relay-bin
Set server-id (server-id must be unique, and cannot conflict with the main library and other slave libraries):
server-id = 2
(2) Restart the database
service mysqld restart
5. Synchronize the main database from the database
(1) Lock the main library so that it has only read-only permissions.
flush table with read lock;
(2) Check the master status and remember the backup point
The content in the red box needs to be kept and recorded, and the backup is started from this place.
(3) Back up the main database
Because only one database is synchronized, it is sufficient to back up this one database.
mysqldump -uroot -p123456 newenergy > newenergy.sql
(4) Unlock table
unlock tables;
(5) Import the backup from the library
mysql -uroot -p123456
source newenergy.sql
(6) From the library to the main library
It is recommended to use the SQL statement "CHANGE MASTER TO" to modify the relevant configuration. The main modifications are:
- master_host: master host IP
- master_port: port of the master database
- master_user: The user name used by the slave to connect to the master for authentication
- master_password: the password used by the slave to connect to the master for authentication
- master_log_file: The name of the master binary log
- master_log_pos: the starting position of the binary log where the slave starts to replicate
The setting contents of the latter two parameters master_log_file and master_log_pos are the contents of File and Position that were previously required to be retained during show master status.
Log in to the slave database
mysql -u root -p123456
Connect to the main library
CHANGE MASTER TO MASTER_HOST='10.200.9.183',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='mysql-bin.000002',master_port=3307,MASTER_LOG_POS=154;
(7) Start the slave
start slave;
(8) View slave status
show slava status\G;
As shown below, both Slave_SQL_Running and Slave_IO_Running show Yes to prove that the slave successfully connected to the master.
(9) Troubleshooting
If Slave_IO_Running does not display yes, Last_IO_Error will display the cause of the error, as shown in the figure:
Here the server-id of the master-slave database of the table name is duplicated, and one of the server-id needs to be modified and restarted.
This means that it cannot be connected to the main database, which may be due to the failure of the main database port or the unreachable IP. Check whether the slave library can be connected to the master library.
6. Check whether the synchronization is successful
Create a table or add a field in the newenergy database of the main library, and you can see it in the slave library to indicate that the synchronization is successful. You can also see the synchronization delay by viewing the value of Seconds_Behind_Master.