標籤:mysql 主從複製 單向同步 雙向同步
一.主從複製的工作過程:
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/94/BF/wKiom1kNSECCsVYlAACo-eVyDvs057.png" title="QQ20170506115112.png" alt="wKiom1kNSECCsVYlAACo-eVyDvs057.png" width="500" height="350" border="0" hspace="0" vspace="0" style="width:500px;height:350px;" />
二.MySQL複製類型
基於SQL語句的複製
基於行的複製
混合複製
三.實驗環境
OS:CentOS 6.5 x64
master:192.168.0.134
slave:192.168.0.135
三.配置主從複製
1.配置時間同步
master:配置為時間伺服器
[[email protected] ~]# yum install ntp編輯/etc/ntp.conf 添加如下兩行:server 127.127.1.0fudge 127.127.1.0 stratm 8service ntpd start
slave:同步master時間
[[email protected] ~]# yum install ntpdate[[email protected] ~]# ntpdate 192.168.0.134 6 May 06:37:58 ntpdate[6653]: adjust time server 192.168.0.134 offset -0.469705 sec
2.安裝MySQL
slave 和 master:
[[email protected] ~]# yum install mysql-server mysql -y[[email protected] ~]# /etc/rc.d/init.d/mysqld start[[email protected] ~]# chkconfig mysqld on[[email protected] ~]# mysqladmin -u root password "123.com"
3.編輯設定檔
master:
編輯/etc/my.conf添加如下幾行server-id=134 #設定id,主從不同log-bin=master-bin #開啟二進位日誌log-slave-update=true重啟MySQL服務[[email protected]~]# service mysqld restart
slave:
編輯/etc/my.conf添加如下幾行server-id=135relay-log=relay-log-binrelay-log-index=slave-relay-bin.indexread-only=1 #這裡可以設定mysql為僅讀,不對root生效重啟MySQL服務[[email protected] ~]# service mysqld restart
3.登入mysql,給slave授權
master:
mysql> grant replication slave on *.* to ‘slave‘@‘192.168.0.%‘ identified by ‘123456‘;Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> show master status;+-------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+-------------------+----------+--------------+------------------+| master-bin.000001 | 181 | | |+-------------------+----------+--------------+------------------+1 row in set (0.00 sec)
# File:日誌名 Position:位移量
4.登入MySQL,配置同步
slave:
mysql> change master to master_host=‘192.168.0.134‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘master-bin.000001‘,master_log_pos=181;Query OK, 0 rows affected (0.12 sec)mysql> start slave;Query OK, 0 rows affected (0.00 sec)mysql> show slave status \G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.134 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 181 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 252 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 181 Relay_Log_Space: 405 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec)
#查看同步狀態Slave_IO和Slave_SQL是YES說明主從同步成功。
四.測試
1.在master上面建立一個資料庫
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || test |+--------------------+3 rows in set (0.00 sec)mysql> create database guoxh charset ‘utf8‘;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || guoxh || mysql || test |+--------------------+4 rows in set (0.00 sec)
2.在slave上面查看資料庫
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || guoxh || mysql || test |+--------------------+4 rows in set (0.00 sec)
#在slave上面可以看到剛才建立的資料庫,則說明主從複製配置成功。
本文出自 “隔壁老大哥” 部落格,請務必保留此出處http://guoxh.blog.51cto.com/10976315/1922643
MySQL 主從複製原理及搭建