mysql主從操作:
mysql數據庫讀寫分離,主從同步實現方法
https://blog.csdn.net/go_pkqL00/article/details/87974101
mysql數據庫的讀寫分離
https://www.jianshu.com/p/0eaacc73fa70
Mysql讀寫分離與主從數據庫設置方案
https://baijiahao.baidu.com/s?id=1613635845071808614&wfr=spider&for=pc
————————————————————————————————————————————————————
mysql binlog_do_db參數設置的坑(碰到的問題)
原文鏈接:https://blog.csdn.net/cug_jiang126com/article/details/51437400
現象
在配置文件中想當然地配置成binlog_do_db=test,xx,jj,以為是三個庫。結果無論什么操作都沒有binlog產生
原因
mysql內部將“test,xx,jj”當成一個數據庫了,結果因為我們沒有這個db,自然就啥binlog都沒寫入了。
處理方法
正確的配置方法應該是這樣
binlog_do_db=test
binlog_do_db=xx
binlog_do_db=jj
兩種設置方法在show master status結果顯示是完全一致的,也不報錯,其他的過濾參數設置方法應該差不多,這真是太坑了!
————————————————————————————————————————————————————
———————————————成功方案—————————————————————————————————
1. 在主從主機分別建立相同的數據庫,或者將主的數據備份導入到從主機還原
2.在主Mysql服務器中修改
vim /etc/my.cnf
主服務器master記錄數據庫操作日志到Binary log,
從服務器開啟i/o線程將二進制日志記錄的操作同步到relay log(存在從服務器的緩存中),
另外sql線程將relay log日志記錄的操作在從服務器執行
# Replication Master Server (default)
# binary logging is required for replication
###log-bin=mysql-bin
log-bin=master-bin #bin log日志文件名
log-bin-index=master-bin.index #bin log日志文件索引名
binlog-do-db=cloudcollect #需要記錄日志的數據庫,如果有多個可以重復寫多行
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1 #主服務Id 必須與從不同
3.重啟mysql服務
service mysql restart
4.檢查配置效果,進入主數據庫並執行
mysql> SHOW MASTER STATUS;
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5.配置從服務器的 my.cnf
在[mysqld]節點下面添加:
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
# binary logging format - mixed recommended
binlog_format=mixed
relay-log=slave-relay-bin #從log日志文件名
relay-log-index=slave-relay-bin.index #從log日志文件索引名
replicate-do-db=cloudcollect #需要同步的日志的數據庫,如果有多個可以重復寫多行
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 2 #從服務Id 必須與從不同
service mysql restart
6.接下來配置兩個數據庫的關聯
首先我們先建立一個操作主從同步的數據庫用戶,切換到主數據庫執行:
mysql> create user repl;
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'從xxx.xxx.xxx.xx' IDENTIFIED BY 'mysql';
mysql> flush privileges;
建立用戶repl 授予同步到從xxx.xxx.xxx.xxx的REPLICATION SLAVE權限,這個權限是針對主庫的所有表的,其中xxx就是從服務器的ip地址。
7.切換到從數據庫執行: master_log_file='master-bin.000001',master_log_pos=0;可能根據步驟4 SHOW MASTER STATUS;中的內容配置
mysql> change master to master_host='主xxx.xxx.xxx.xx',master_port=3306,master_user='repl',master_password='mysql',master_log_file='master-bin.000001',master_log_pos=0;
mysql> start slave;
停止主從同步的命令為:
mysql> stop slave;
1
查看狀態命令,\G表示換行查看
mysql> show slave status \G;
——————————————————————————————————————————————————————————