您的位置:首页 > 数据库 > MySQL

MySQL主从配置同步数据

2016-11-16 00:00 225 查看
Centos x64
Mysql-5.5.43-linux2.6-x86_64
主server 1 IP:192.168.138.128
从server 2 IP:192.168.138.129

配置my.cnf
#主服务器my.cnf
vi /etc/my.cnf
####
#server-id=1 -默认,必须唯一
#sync_binlog=1 -添加
#保存后重启mysql
#systemctl restart mysql.server
#登录mysql
mysql -uroot -p
#输入密码后执行命令,检查是否已master形式启动
mysql>show master stutas;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000023 |      107 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
#建立同步从服务器所使用的账户(账户名:dbms,密码:c)
grant replication slave on *.* to 'dbms'@'192.168.138.129' identified by 'c';
#防火墙需开放3306端口
firewall-cmd –zone=public –add-port=3306/tcp --permanent
firewall-cmd –reload
#从服务器my.cnf
vi /etc/my.cnf
#server-id=2 -修改
#保存后登录mysql执行命令赋予权限(mysql 5.5版本以后)
mysql>CHANGE MASTER to MASTER_HOST='192.168.138.128', MASTER_PORT=3306, MASTER_USER='dbms', MASTER_PASSWORD='c', MASTER_LOG_FILE='mysql-bin.000023', MASTER_LOG_POS=107;
#重启mysql
#systemctl restart mysql.server
#查看状态,无错误则表示配置成功
show slave status \G;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.138.128
Master_User: dbms
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000023
Read_Master_Log_Pos: 107
Relay_Log_File: localhost-relay-bin.000005
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000023
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: 107
Relay_Log_Space: 559
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
*******************************
#####停止从服务器复制命令stop slave;开始复制命令start slave
#无错误发生,表示成功,在主服务器上数据操作会自动同步到从服务上,测试在主服务上进行,然后查看从服务器数据有没发生更改,在此不做演示


互为主从双向数据同步说明
#如果要做两服务器互为主从双向数据同步,主服务器my.cnf需添加
auto_increment_offset=1 #表示自增长字段开始数
auto_increment_increment=2 #自增长字段每次递增的量
#从服务器my.cnf添加
auto_increment_offset=2
auto_increment_increment=2
#避免两台服务器自增长字段的值(奇偶方式值避免值冲突)之间发生冲突。
#然后做一次相反配置就行了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: