Using spring to achieve read-write separation, MySQL master-slave replication configuration tutorial

programming development

 1. MySQL master-slave replication

1.1. Principle

The principle of mysql master (called master) slave (called slave) replication:

1. The master records data changes in the binary log, which is the file specified by the configuration file log-bin (these records are called binary log events)

2. The slave copies the master's binary log events to its relay log

3. The slave redoes the events in the relay log and will change the data that reflects its own (data replay)

1.2. Things to pay attention to in master-slave configuration

1. The versions of the master DB server and the slave DB server database are the same

2. The database data of the master DB server and the slave DB server are consistent [here, the backup of the master can be restored on the slave, or the data directory of the master can be directly copied to the corresponding data directory of the slave]

3. The primary DB server opens the binary log, and the server_id of the primary DB server and the secondary DB server must be unique

1.3. Main library configuration ( similar under Windows and Linux )

Modify in my.ini:

#Enable master-slave replication, the configuration of the main library

log-bin = mysql3306-bin

#Designated main serverid

server-id=101

#Specify the database to synchronize, if not specified, synchronize all databases

binlog-do-db=mybatis_1128

Execute the SQL statement to query the status:

SHOW MASTER STATUS

 

The File and Position values ​​need to be recorded, and the synchronization start value needs to be set in the slave library.

1.4. Create a synchronization user in the main library

#Authorized user slave01 uses the 123456 password to log in to mysql

grant replication slave on *.* to 'slave01'@'127.0.0.1' identified by '123456';

flush privileges;

****主库配置完成后重启服务

1.5.从库配置

在my.ini修改:

#指定serverid,只要不重复即可,从库也只有这一个配置,其他都在SQL语句中操作

server-id=102

以下执行SQL:

CHANGE MASTER TO

master_host='127.0.0.1', --主库的ip地址

master_user='slave01',--主库授权的用户名

master_password='123456',--密码

master_port=3380,--主库的端口

master_log_file='mysql3306-bin.000006',--主库使用show master status 查询出来的file列的值

master_log_pos=1120;--主库使用show master status 查询出来的position列的值

#停止slave同步

STOP SLAVE;

#启动slave同步

START SLAVE;

#查看同步状态

SHOW SLAVE STATUS;

 

Guess you like

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