Linux, MySQL master-slave replication

Modify my.cnf

vim /data/3306/my.cnf
[mysqld]
log-bin= /data/3306/log-bin
server-id= 6
vim /data/3307/my.cnf
[mysqld]
server-id= 7
  • Main library must be open binlog, if not cascade copy from the library may not open, but server-id must be unique
    to restart the database
/data/3306/mysql stop
/data/3306/mysql start
/data/3307/mysql stop
/data/3307/mysql start

Check the database configuration

mysql -S /data/3306/mysql.sock 
mysql> show variables like 'server_id';
mysql> show variables like 'log_bin';

Here Insert Picture Description

Establish an account rep for synchronization in the main library

mysql>grant replication slave on *.* to rep@'10.0.0.%' identified by '111111';
mysql> flush privileges;
parameter Explanation
replication slave Master-slave synchronization user must privileges, no privileges to other
*.* It means all tables, or you can specify a table in a library, for example: user under the table mysql.user, mysql database
[email protected].% is an account rep, 10.0.0.% as an authorized host network segment, use% represents the entire 10.0.0.0 network to allow users to access rep
identified by set password
  • Checking account established
mysql> select user,host from mysql.user;

Here Insert Picture Description

  • View user permissions
mysql>show grants for rep@'10.0.0.%';

Here Insert Picture Description

The master lock database table

mysql>flush table with read lock;

Different engines, lock the table will be controlled by the following parameters, if the operation time does not exceeds the set will automatically unlock the lock table
Here Insert Picture Description
lock control parameter table, unit: seconds, more than this time will automatically unlock

After the lock table view of the main library binlog status

  • Check the lock table emperor library binlog information
mysql>show master status; 

Here Insert Picture Description

Export Master Data Library

After the lock table must use connectivity tools to open a new window to export data, if the data is greater than 50G, and allows the shutdown, the library can be parked directly packed data file migration, that would be quicker

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

Here Insert Picture Description

  • To ensure that no data is written during data export, see binlog again
mysql>show master status;

Before the need to maintain export data, consistent with the binlog

  • After exporting the database can unlock the main library
msyql>unlock tables;

The main import from the library to the library data

  • In the main library from the library extracting backup file
gzip -d mysql_bak.2018-12-29.sql.gz 
mysql -uroot -S /data/3307/mysql.sock < /data/mysql_bak.2018-12-29.sql

If the parameters -A backup, restore the data in 3307 to the login password 3307 and 3306 will be the same, because the authorization table 3307 is also covered

Log from the library, copy the configuration parameters

change master to MASTER_HOST='10.0.0.200',MASTER_USER='rep',MASTER_PASSWORD='111111',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=324;

Here Insert Picture Description

parameter Explanation
MASTER_HOST Main library ip
MASTER_PORT Main library port number
MASTER_USER User for copying
MASTER_PASSWORD User password
MASTER_LOG_FILE It is to look at the main library show master status binlog file name, not by spaces
MASTER_LOG_POS In the main gallery show master status binlog offset, no spaces
  • Validation checks
cat /data/3307/data/master.info

Here Insert Picture Description

Open copy

mysql>start slave;
mysql>show slave status;

Here Insert Picture Description

Verification result is copied from the master

  • Main library
    Here Insert Picture Description

  • From the library
    Here Insert Picture Description

  • In the main library to create a new database

mysql>create database master;
mysql>show databases;

Here Insert Picture Description

  • View from the library
mysql>show databases;

Here Insert Picture Description

other

Open the master binary log records all the default action of all database tables, only the recording operation can be specified even the specified database table specified by the configuration, particularly the following may be added to modify options [mysqld] mysql profile

  • What libraries are not synchronized
    binlog-the ignore-db = MySQL
    binlog-the ignore-db = the Test
    binlog-the ignore-db = information_schema

  • Which only database synchronization, in addition to not sync
    binlog-do-db = master

Guess you like

Origin blog.csdn.net/qq_33235529/article/details/85337521