標籤: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效能調優” 部落格,轉載請與作者聯絡!