MySQL master-slave replication practice and deployment

This article mainly introduces the realization of MySQL master-slave replication function in a single-machine, single-database, and multi-instance environment.


Basic process:

1. Introduction to MySQL master-slave replication

2. Master-slave replication database actual combat environment preparation

3. Perform operation configuration on the main library Master

4. The operation process performed on the MySQL slave library

5. Start the slave library synchronization switch to test the active replication configuration

6. Summary of MySQL master-slave replication configuration steps




Process 1: Introduction to MySQL master-slave replication


  The master-slave replication scheme of Mysql database is similar to the file-level replication using scp/rsync and other commands. It is the remote transmission of data, but the master-slave replication of Mysql is its own function, without the help of third-party tools, and , Mysql's master-slave replication is not a direct copy of the files on the database disk, but is copied to the local server to be synchronized through the logical binlog log, and then the SQL statement in the log is read by the local thread and re-applied to the Mysql database. .

  Mysql's master-slave replication is an asynchronous process (although it generally feels real-time), data will be copied from one Mysql database (we call it Master) to another Mysql database (we call it Slave), in the Master The entire master-slave replication process with Slave is completed by three threads. There are two threads (SQL thread and I/O thread) on the Slave side, and another thread (I/O thread) on the Master side. To realize the master-slave replication of Mysql, the binlog recording function on the Master side must be turned on first, otherwise it cannot be realized. To turn on the MySQL binlog recording function, you can use the mysqld module in the MySQL configuration file my.cnf (the [mysqld] mark after the parameter section) to add the "log-bin" parameter option to achieve this. Because the entire replication process is actually that the slave side obtains the binlog log from the master side, and then executes various SQL operations recorded in the obtained binlog log on the slave in the same order.

 

  The key summary of MySQL master-slave replication principle:

  #Master-slave replication is asynchronous logical SQL statement-level replication.

  #When copying, the main library has one I/O thread, and the slave library has two threads, namely I/O and SQL threads.

  #The necessary condition for realizing master-slave replication is that the main library should enable the recording binlog function.

  #The server_id of all MySQL nodes that are replicated cannot be the same.

  The #binlog file only records SQL statements that have changes to the database (changes from the main database content), and does not record any query (such as select, show) statements.




Process 2: Preparation of master-slave replication database for actual combat environment


1) Preparation for master-slave replication practice

This article takes a single-machine database multi-instance environment as an example to explain. For configuring MySQL multi-instance, please refer to http://blog.51cto.com/13707680/2112502

The instance port information is as follows:

[root@localhost 3308]# netstat -tnlp | grep 330

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      8675/mysqld         

tcp        0      0 0.0.0.0:3307                0.0.0.0:*                   LISTEN      8271/mysqld         

tcp        0      0 0.0.0.0:3308                0.0.0.0:*                   LISTEN      9256/mysqld  


2) Define the server role for master-slave replication

Main library (mysql master): [IP is 192.168.1.72 and port is 3306]

Slave library (mysql slave): [ip is 192.168.1.72 port is 3307]

Slave library (mysql slave): [ip is 192.168.1.72 port is 3308]




Process 3: Execute operation configuration on the main library Master


1) Set the server_id value and enable the binlog function parameter

[root@localhost 3306]# egrep "server_id|log_bin" /data/3306/my.cnf  

log_bin=/data/3306/mysql-bin

server_id=3306 #Each machine or instance server_id used for synchronization cannot be the same

The above two parameters must be placed under the mysqld module, otherwise an error will occur.


2) Restart the main database MySQL service, the command is as follows:

[root@localhost 3306]# /data/3306/mysqld restart

mysqladmin: connect to server at 'localhost' failed

error: 'Access denied for user 'root'@'localhost' (using password: NO)'

Here is an error saying that a password is required, modify the startup script file

[root@localhost 3306]# grep Stop= /data/3306/mysqld

Stop='mysqladmin -uroot -pywxi123 -S /data/3306/mysql.sock shutdown'

[root@localhost 3306]# /data/3306/mysqld  restart

MySQL already Stoped                                       [  OK  ]

MySQL 3306 Starting                                        [  OK  ]


3) Log in to the database and check the parameter changes, as follows:

[root@localhost 3306]# mysql -uroot -pywxi123 -S /data/3306/mysql.sock 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.32-log MySQL Community Server (GPL)


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show variables like 'server_id';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| server_id     | 3306  |

+---------------+-------+

1 row in set (0.01 sec)


mysql> show variables like 'log_bin';  

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| log_bin       | ON    |

+---------------+-------+

1 row in set (0.00 sec)

#In this way, the binlog function is turned on normally.


4) Create an account for master-slave replication on the master database

mysql -uroot -pywxi123 -S /data/3306/mysql.sock

grant replication slave on *.* to 'rep'@'192.168.1.%' identified by 'ywxi123'; #rep is the synchronization account, *.* is all tables in all libraries, 192.168.1.% is the authorized host network part. Use % to allow the entire network segment

flush privileges; #Refresh privileges, the authorized privileges take effect

Check the rep replication account created by the main library and the results are as follows:

mysql> select user,host from mysql.user where user='rep';

+------+-------------+

| user | host        |

+------+-------------+

| rep  | 192.168.1.% |

+------+-------------+

1 row in set (0.00 sec)

mysql> show grants for rep@'192.168.1.%';

+--------------------------------------------------------------------------------------------------------------------------+

| Grants for [email protected].%                                                                                               |

+--------------------------------------------------------------------------------------------------------------------------+

| GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.1.%' IDENTIFIED BY PASSWORD '*1CDCFBE8F2B7FACC6BF289A82F072AB9A2D90844' |

+--------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)


5) Implement read-only access to the main database lock table

mysql> flush table with read lock;

Query OK, 0 rows affected (0.00 sec)

Check the main library status after locking the table:

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000006 |      334 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

#Lock table This operation is to better export and backup data

[root@localhost 3307]# mkdir -p /server/backup

[root@localhost 3307]# mysqldump -uroot -p'ywxi123' -S /data/3306/mysql.sock --events -A -B |gzip > /server/backup/mysql_bak.$(date +%F).sql.gz


#After exporting the data, unlock the main library and restore writability. The command is as follows:

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)




Process 4: The operation process performed on the MySQL slave library


1) Set the server_id value and close the binlog function parameter

If the slave library does not do cascading replication and does not use it for replication, do not open binlog. If it is turned on, it will increase the pressure on the disk I/O of the slave library.

[root@localhost 3306]# egrep "server_id|log_bin" /data/3307/my.cnf       

#log_bin=/data/3307/mysql-bin

server_id=3307 #ID is unique


2) Restart the slave database of 3307

[root@localhost 3307]# /data/3307/mysqld stop

[root@localhost 3307]# /data/3307/mysqld start


3) Log in to the database to check the change of parameters

mysql> show variables like 'log_bin';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| log_bin       | OFF   |

+---------------+-------+

1 row in set (0.00 sec)    

mysql> show variables like 'server_id';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| server_id     | 3307  |

+---------------+-------+

1 row in set (0.00 sec)


4) Restore the data exported from the master library mysqldump to the slave library

The operation command is as follows:

cd /server/backup/

mysqldump -uroot -p'ywxi123' -S /data/3307/mysql.sock --events -A -B |gzip > /server/backup/mysql_bak.$(date +%F).sql.gz


5) Configure the replication parameters on the 3307 slave library

[root@localhost backup]# mysql -uroot -pywxi123 -S /data/3306/mysql.sock -e "show master status"

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000006 |      334 |              |                  |

+------------------+----------+--------------+------------------+


[root@localhost backup]# mysql -uroot -pywxi123 -S /data/3307/mysql.sock <<EOF

CHANGE MASTER TO

MASTER_HOST='192.168.1.72',

MASTER_PORT=3306,

MASTER_USER='rep',

MASTER_PASSWORD='ywxi123',

MASTER_LOG_FILE='mysql-bin.000006',

MASTER_LOG_POS=334;

EOF

#The parameters of this step must not be wrong, otherwise, the database replication configuration will fail




Process 5: Start the slave library synchronization switch to test the active replication configuration


1) Start the master-slave replication switch of the slave library, and check the replication status

[root@localhost data]# mysql -uroot -pywxi123 -S /data/3307/mysql.sock -e "start slave;"

[root@localhost data]# mysql -uroot -pywxi123 -S /data/3307/mysql.sock -e "show slave status\G;"|egrep "IO_Running|SQL_Running|_Behind_Master"

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

        Seconds_Behind_Master: 0

#These are the three most critical state parameters, they must be consistent with the above, otherwise the test will go wrong


2) Test the master-slave replication results, plus a 3308 slave library

Write data on the master library, and then observe the data status of the slave library.

[root@localhost data]# mysql -uroot -p'ywxi123' -S /data/3306/mysql.sock -e "create database ywxi;"       

[root@localhost data]# mysql -uroot -p'ywxi123' -S /data/3307/mysql.sock -e "show databases like 'ywxi'"  

+-----------------+

| Database (ywxi) |

+-----------------+

| isxi |

+-----------------+

[root@localhost data]# mysql -uroot -pywxi123 -S /data/3306/mysql.sock -e "drop database ywxi;"

[root@localhost data]# mysql -uroot -pywxi123 -S /data/3307/mysql.sock -e "show databases like 'ywxi';"

[root@localhost data]# 

#According to the test, it can be judged that the master-slave library is synchronized.


Quickly add a slave library 3308 command set as follows:

sed  /log_bin/'s/.*/#&1/g' /data/3308/my.cnf -i

mysql -uroot -S /data/3308/mysql.sock -e "show variables like 'log_bin';"

mysql -uroot -S /data/3308/mysql.sock -e "show variables like 'server_id';"

cd /server/backup/

mysqldump -uroot  -S /data/3308/mysql.sock --events -A -B |gzip > /server/backup/mysql_bak.$(date +%F).sql.gz

mysql -uroot  -S /data/3308/mysql.sock <<EOF

> CHANGE MASTER TO

> MASTER_HOST='192.168.1.72',

> MASTER_PORT=3306,

> MASTER_USER='rep',

> MASTER_PASSWORD='ywxi123',

> MASTER_LOG_FILE='mysql-bin.000006',

> MASTER_LOG_POS=334;

> EOF

mysql -uroot -S /data/3308/mysql.sock -e "start slave;"

mysql -uroot  -S /data/3308/mysql.sock -e "show slave status\G;"|egrep "IO_Running|SQL_Running|_Behind_Master" 






Process 6: Summary of MySQL master-slave replication configuration steps


The complete steps of MySQL master-slave replication configuration are as follows:

1) Prepare two databases or a single multi-instance environment, and make sure that it can be started and logged in normally

2) Configure the my.cnf file: the main library is configured with log_bin and server_id parameters; the slave library is configured with server_id, this value cannot be the same as the main library, and it is generally not recommended to enable the log-bin function. After configuring the parameters, be sure to restart the service to take effect

3) Log in to the master database, add the account for the slave database to connect to the master database synchronization, for example: rep, and authorize the replication slave synchronization permission

4) Log in to the main database, flush the entire database lock table with read lock (it will expire after the window is closed, the time set by the timeout parameter has expired, and the lock table will also expire), and then show master status to view the location status of binlog

5) Open a new window, backup and export the original database data on the linux command line, and copy it to the server directory where the slave library is located. If the amount of database data is large, and downtime is allowed, you can shut down the package instead of mysqldump

6) After exporting the main data, execute unlock tables to unlock the main library

7) Restore the data exported from the master library to the slave library

8) Execute the change master to..... statement in the slave library according to the location status of the binlog viewed by the show master status of the master library.

9) Turn on the replication switch from the library, that is, execute start slave; (execute after the change statement, otherwise the statement cannot be inserted)

10) From the library show slave status\G, check the synchronization status, and perform an update test in the master library.




































  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325244109&siteId=291194637