您好,登錄后才能下訂單哦!
一.主從復制的工作過程:
二.MySQL復制類型
基于SQL語句的復制
基于行的復制
混合復制
三.實驗環境
OS:CentOS 6.5 x64
master:192.168.0.134
slave:192.168.0.135
三.配置主從復制
1.配置時間同步
master:配置為時間服務器
[root@master ~]# yum install ntp -y 編輯/etc/ntp.conf 添加如下兩行: server 127.127.1.0 fudge 127.127.1.0 stratm 8 service ntpd start
slave:同步master時間
[root@slave ~]# yum install ntpdate -y [root@slave ~]# 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:
[root@master ~]# yum install mysql-server mysql -y [root@master ~]# /etc/rc.d/init.d/mysqld start [root@master ~]# chkconfig mysqld on [root@master ~]# mysqladmin -u root password "123.com"
3.編輯配置文件
master:
編輯/etc/my.conf添加如下幾行 server-id=134 #設置id,主從不同 log-bin=master-bin #開啟二進制日志 log-slave-update=true 重啟MySQL服務 [root@smaster~]# service mysqld restart
slave:
編輯/etc/my.conf添加如下幾行 server-id=135 relay-log=relay-log-bin relay-log-index=slave-relay-bin.index read-only=1 #這里可以設置mysql為僅讀,不對root生效 重啟MySQL服務 [root@slave ~]# 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: 0 Master_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上面可以看到剛才新建的數據庫,則說明主從復制配置成功。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。