MySQL 主從複製搭建

來源:互聯網
上載者:User

標籤:mysql   主從   


環境:

master ip: 192.168.10.10
salve  ip: 192.168.10.20


1)在兩台機器上面安裝好mysql

2)修改master my.cnf 添加如下幾行
server-id = 1
log-bin = /opt/mysql/log/mysql-bin.log
datadir = /opt/mysql/data
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1

3)修改slave my.cnf 添加如下行
server-id=2
relay-log-index=salve-relay-bin.index
relay-log=slave-relay-bin
datadir=/opt/mysql/data

4)重啟master 和 slave MySQL服務
service mysqld restart

5)在master上建立複製帳號
mysql> GRANT REPLICATION SLAVE ON *.* TO [email protected] IDENTIFIED BY ‘mysql‘;

6)查看master 狀態
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      739 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)


7)如果需要同步原有的庫,還需要做如下操作,否者調到第11步
mysql> FLUSH TABLES WITH READ LOCK;      
 
 在主機上執行FLUSH TABLES WITH READ LOCK語句來重新整理所有的表並阻斷寫操作
 
8) 使用mysqldump命令備份需要同步的資料庫
[[email protected] ~]$ mysqldump -u root dbname > /home/root/dbname.sql

9)在salve上恢複資料
先將備份的sql檔案複製到salve
[[email protected] ~]$ mysql -u root –e ‘create database dbname;‘
[[email protected] ~]$ mysql -u root dbname < /home/root/dbname.sql

10) salve恢複完成後重新開啟master的讀寫功能
mysql> UNLOCK TABLES;

11)在slave上執行如下命令開始同步資料
mysql> change master to master_host=‘192.168.10.10‘,
 master_user=‘slave‘,
 master_password=‘mysql‘,
 master_log_file=‘mysql-bin.000004‘,
 master_log_pos=1790;

mysql> start slave


12) 最後在master上建立表驗證

本文出自 “專註於Oracle效能調優” 部落格,轉載請與作者聯絡!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.