Mysql实战:主从同步
在兩臺(tái)機(jī)器上安裝MySQL.主機(jī)地址分別為:
Master:192.168.11.104
Slave:192.168.11.103
啟動(dòng)mysql服務(wù):/etc/init.d/mysql? start
修改主庫(kù)的配置文件 my.cnf? (屬性說(shuō)明自行百度)
修改從庫(kù)的配置文件 my.cnf?
進(jìn)入從庫(kù),配置主庫(kù),執(zhí)行:
change master to master_host="192.168.11.104", master_user = 'root',master_password = '123456', master_port = 3306,master_log_file='mysql-bin.000003', master_log_pos = 107,master_connect_retry = 30;可能出現(xiàn)錯(cuò)誤:解決方案?。
slave_io_running = yes
slave_sql_running = yes
則成功!
此時(shí)可以愉快的主從同步了。寫的比較簡(jiǎn)單,請(qǐng)大家看看就行。
關(guān)閉running中的從庫(kù):在從庫(kù),使用命令 stop slave
如果 slave這邊的IO_Running為NO的話;
刷新master的log: flush logs;
重新獲取
stop slave;
填入最新的數(shù)值,再次執(zhí)行
change master to master_host="192.168.11.104",
master_user = 'root',master_password = '123456',
master_port = 3306,master_log_file='mysql-bin.000003',
master_log_pos = 107,master_connect_retry = 30;
start slave;
至此解決!
如何將主庫(kù)在同步之前的數(shù)據(jù)加載到新開的從庫(kù)里呢?
首先將主庫(kù)上read lock,
flush tables with read lock;將數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)出到sql文件,
mysqldump -p3306 -uroot -p --add-drop-table test > /home/nginx--/chen/sql_tmp/user.sql;解鎖
unlock tables;?在從庫(kù)創(chuàng)建相應(yīng)的database,然后將sql文件注入到mysql中;
mysql -uroot -p test < /home/nginx--/chen/sql_tmp/user.sql注意:這里的主從同步是單向主從,還可以配置雙向主從,會(huì)有少許問題。一般情況下,單向主從就已經(jīng)能夠滿足業(yè)務(wù)需求了。
下一篇將寫寫讀寫分離,需要使用到數(shù)據(jù)庫(kù)中間件MyCat .
?
總結(jié)
以上是生活随笔為你收集整理的Mysql实战:主从同步的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络:TCP/UDP
- 下一篇: 数据库:MySQL索引总结