MySQL主從介紹、準備工作、配置主、配置從、測試主從同步

來源:互聯網
上載者:User

標籤:creat   bin   ffffff   alt   鎖表   http   code   roo   server   

MySQL主從介紹

配置主

mysql安裝後修改my.cnf設定檔,修改my.cnf,增加server-id=130和log_bin=dashuo1

[[email protected] ~]# vim /etc/my.cnf

修改完設定檔後,啟動或者重啟mysqld服務

[[email protected] ~]# /etc/init.d/mysqld restart

重啟完成後/data/mysql下會出現幾個檔案

把mysql資料庫的zrlog庫備份並恢複成hanshuo庫,作為測試資料

[[email protected] mysql]# mysqldump -uroot -phanshuo1 zrlog > /tmp/zrlog.sql     ##備份db1庫
[[email protected] mysql]# mysql -uroot -phanshuo1 -e "create database hanshuo"   ##建立hanshuo庫
[[email protected] mysql]# mysql -uroot -phanshuo1 hanshuo < /tmp/zrlog.sql   ##把備份zrlog的資料庫恢複成hanshuo資料庫

進入資料庫建立使用者,用作主從同步資料

[[email protected] mysql]# mysql -uroot -phanshuo1    ##進入資料庫
mysql> grant replication slave on *.* to ‘repl‘@‘192.168.63.104‘ identified by ‘www692517111‘;     ##建立使用者

鎖表目的是不讓繼續寫

mysql> flush tables with read lock;
mysql> show master status;

為了測試把所有的庫做下備份,用作同步

配置從

從上邊已經安裝了mysql設定檔裡加上server-id

[[email protected] ~]# vim /etc/my.cnf

修改完設定檔後,啟動或者重啟mysqld服務

[[email protected] ~]# /etc/init.d/mysqld restart

把主上aming庫同步到從上
可以先建立aming庫,然後把主上的/tmp/mysql.sql拷貝到從上,然後匯入aming庫

[[email protected] ~]# scp 192.168.63.100:/tmp/*.sql /tmp/

建立庫

mysql> create database hanshuomysql> create database db1;mysql> create database zrlog;mysql> create database mysql2

恢複資料庫

保持主從資料庫一致

實現主從

登入從的資料庫

[[email protected] ~]# mysql -uroot -phanshuo1
mysql> stop slave;

主從同步非常重要的語句

mysql> change master to master_host=‘192.168.63.100‘, master_user=‘repl‘, masster_password=‘www692517111‘, master_log_file=‘dashuo1.000001‘, master_log_poos=16821;
mysql> start slave;

判斷主從是否配置成功

**在從上登入mysql執行show slave status\G

到主上恢複操作

查看主從同步是否正常

測試主從同步

MySQL主從介紹、準備工作、配置主、配置從、測試主從同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.