Mysql主从复制
------------------------------------------------
步骤:
1.授权用户
grant all privileges on *.* to "slave"@"%" identified by "kkk";
2.开启bin_log
主:
vim my.cnf
server_id=1
log_bin=zhilei_bin #代表所有的log_bin文件都是以zhilei_bin开头
binlog-do-db=test #设置要生成的二进制操作的库
binlog-ignore-db=information_schema #忽略进行同步的db
从:
server_id = 2
replicate-do-db=test
注意:
1.两个server_id不能一样
2.两个的uuid也不能一样
/usr/local/mysql/data/auto.conf #里面的uuid是不能一样的
3.查看bin_log有没有开启
show variables like "%bin%";
4.常用命令:
flush logs; #重新生成一份binlog日志
show master sataus; #查看最后一次的binlog日志
reset master; #清空所有的binlog日志
mysqlbinlog zhilei_bin; #查看二进制日志 >5.1
(如果出错的话,则要加上--no-defaults)
5.利用binlog来恢复数据
1)重新生成一个binlog -> flush logs;
2)删除数据表里面数据
3)用mysqlbinlog 打开前一个binlog然后 | mysql -uroot -pkkk test;
4)以上就能导入bin-log里面了
6.msyql备份
mysqldump -uroot -pkkk -l(读锁,也就是只能读,但是不能写) -F(刷新日志)
在用binlog恢复日志的时候可以用位置
--start-position="193"
--stop-position="280"
或者是用时间
--start-date="2016.12.26"
--stop-date="2016.12.26"
Mysql5.6以后配置文件中不再有
master_host
master_user
master_password
master_port
stop slave;
change master to master_host='192.168.211.11',master_user='slave',master_password='kkk',master_log_file='zhilei-bin.000018', master_log_pos=120;
start slave;
show slave status;
mysql5.6之后引入uuid的概念
主从不能一样
/usr/local/mysql/data/auto.conf #里面的uuid是不能一样的
具体的内容如下:
mysql> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.211.11
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: zhilei-bin.000002
Read_Master_Log_Pos: 221
Relay_Log_File: zhilei-relay-bin.000007
Relay_Log_Pos: 385
Relay_Master_Log_File: zhilei-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
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: 221
Relay_Log_Space: 723
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
Master_UUID: 4032745b-c760-11e6-a6f4-000c29c263bb
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql>
如果以下两个都为yes的话说明成功:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果不成功的话:
stop slave;
change master to master_host='10.187.114.6 ',master_user='slave',master_password='kkk',master_log_file='mysql-bin.000018', master_log_pos=120;
start slave;
change master to master_host='10.187.114.6',master_user='slave',master_password='jdjk',master_log_file='mysql-bin.000001', master_log_pos=963;
10.187.114.84 #从库 从库帐号:slave 密码:jdjk
10.187.114.6 #主入口 web容器+主库
10.187.143.40 #次入口 web容器->数据库指向10.187.114.6
10.187.143.41 #次入口 web容器->数据库指向10.187.114.6