MySql—Master—Slave
Linux配置mysql主从
Ip:192.168.0.1 Master
Ip:192.168.0.2 Slave
配置文件
vim /etc/my.cnf
添加如下内容到my.cnf
- Master(主) IP:192.168.0.1
[mysqld]
server-id=1 #必须。设置服务器id,为1表示主服务器。
log_bin=mysql-bin #必须。启动MySQ二进制日志系统。
binlog-do-db=test#需要同步的数据库名,如果有多个数据库,配置多行
binlog-ignore-db=mysql #不同步mysql系统数据库。
- Slave(从) IP:192.168.0.2
[mysqld]
server-id=2 #必须。设置服务器id,为2表示从服务器。
log_bin=mysql-bin #不必须。启动MySQ二进制日志系统。
binlog-do-db=test#需要同步的数据库名,如果有多个数据库,可重复此参数,配置多行
binlog-ignore-db=mysql #不同步mysql系统数据库。
** 修改完my.cnf重启数据库**
重启后执行主从配置命令
Ip:192.168.0.2执行命令
[root@F8SFRZ ~]$mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 909
Server version: 10.1.36-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(mysql)]> slave stop;
MariaDB [(mysql)]> change master to master_host='192.168.0.1',master_user='admin',master_password='admin',master_log_file='mysql-bin.000001' ,master_log_pos=0;
MariaDB [(mysql)]> slave start;
MariaDB [(mysql)]> show slave status;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.1
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 983
Relay_Log_File: mysqld-relay-bin.000007
Relay_Log_Pos: 834
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 983
Relay_Log_Space: 1623
Until_Condition: None
.....
.....
.....
省略
-------------------------------------------------------------------------------
Slave_IO_Running、Slave_SQL_Running 等于Yes表示配置成功
如果出现No了,请尝试执行
MariaDB [(mysql)]> stop slave;set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;START SLAVE;
搞定!!!