mysql主從複製案例

來源:互聯網
上載者:User

標籤:linux   mysql   主從複製   

前提條件:

mysql主從操作中關於mysql的安裝與基本配置請參考我的另一篇博文:linux下mysql的安裝以及多執行個體操作“http://ylcodes01.blog.51cto.com/5607366/1841686”


一、主庫操作


1、1查看主庫系統及mysql資訊

[[email protected] ~]# cat /etc/redhat-release CentOS release 6.7 (Final)[[email protected] ~]# uname -r2.6.32-573.el6.x86_64[[email protected] ~]# mysql --versionmysql  Ver 14.14 Distrib 5.5.32, for Linux (x86_64) using readline 5.1


1、2主庫設定server-id以及開啟binlog

[[email protected] backup]# pwd/server/backup[[email protected] backup]# egrep "log-bin|server-id" /etc/my.cnf

server-id       = 51#以IP結尾(只適用於單一實例),其他從庫的server-id不要跟主庫重複,當然所有從庫的也不要重複,修改完一定記得重啟庫

log-bin=mysql-bin#主庫開啟binlog


1、3登入主庫

[[email protected] ~]# mysql -uroot -poldboy123


1、4設定同步處理的使用者

mysql> grant replication slave on *.* to ‘rep‘@‘10.0.0.%‘ identified by ‘oldboy123‘;Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> select user,host from mysql.user;+------+-----------+| user | host      |+------+-----------+| rep  | 10.0.0.%  || root | 127.0.0.1 || root | ::1       ||      | db01      || root | db01      ||      | localhost || root | localhost |+------+-----------+7 rows in set (0.00 sec)mysql> show grants for [email protected]‘10.0.0.%‘;+-----------------------------------------------------------------------------------------------------------------------+| Grants for [email protected]%                                                                                               |+-----------------------------------------------------------------------------------------------------------------------+| GRANT REPLICATION SLAVE ON *.* TO ‘rep‘@‘10.0.0.%‘ IDENTIFIED BY PASSWORD ‘*FE28814B4A8B3309DAC6ED7D3237ADED6DA1E515‘ |+-----------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)


1、5主庫鎖表(此時不能執行交易處理)

mysql> flush table with read lock;Query OK, 0 rows affected (0.00 sec)


1、6查看主庫binlog資訊

mysql> show master status;+------------------+----------+--------------+------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000002 |      289 |              |                  |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               || testdb01           || testdb02           || testdb03           |+--------------------+7 rows in set (0.00 sec)


1、7另開視窗匯出主庫現有資料

[[email protected] backup]# mysqldump -uroot -poldboy123 --events -A -B|gzip>/server/backup/mysql_bak.2016-09-16.sql.gz [[email protected] backup]# lltotal 144-rw-r--r-- 1 root root 144465 Sep 16 17:38 mysql_bak.2016-09-16.sql.gz


1、8發送給從庫

[[email protected] backup]# scp mysql_bak.2016-09-16.sql.gz [email protected]:/server/backup/

1、9主庫關閉鎖表狀態

mysql> unlock tables;Query OK, 0 rows affected (0.00 sec)


二、從庫操作


2、1查看從庫系統及mysql資訊

[[email protected] ~]# cat /etc/redhat-release CentOS release 6.7 (Final)[[email protected] ~]# uname -r2.6.32-573.el6.x86_64[[email protected] backup]# mysql --versionmysql  Ver 14.14 Distrib 5.5.32, for Linux (x86_64) using readline 5.1[[email protected] backup]# pwd/server/backup


2、2設定從庫server-id

[[email protected] backup]# egrep "log-bin|server-id" /etc/my.cnf

server-id       = 52  #以IP結尾(只適用於單一實例),從庫的server-id一定不要跟主庫和其他庫重複,修改一定要重啟資料庫

#log-bin=mysql-bin    #從庫一定不要開啟binlog


2、3在從庫對應解壓主庫發送的Database Backup檔案

[[email protected] backup]# lltotal 144-rw-r--r-- 1 root root 144465 Sep 16 17:39 mysql_bak.2016-09-16.sql.gz[[email protected] backup]# gzip -d mysql_bak.2016-09-16.sql.gz [[email protected] backup]# lltotal 520-rw-r--r-- 1 root root 529834 Sep 16 17:39 mysql_bak.2016-09-16.sql


2、4從庫匯入主庫對應資料庫資料

[[email protected] backup]# mysql -uroot -poldboy123 < mysql_bak.2016-09-16.sql [[email protected] ~]# mysql -uroot -poldboy123


2、5從庫查看匯入主庫資料結果

mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               || testdb01           || testdb02           || testdb03           |+--------------------+7 rows in set (0.00 sec)


2、6設定從庫同步主庫資料命令(主從複製關鍵命令)

mysql> CHANGE MASTER TO MASTER_HOST=‘10.0.0.51‘,MASTER_PORT=3306,MASTER_LOG_FILE=‘mysql-bin.000002‘,MASTER_LOG_POS=289,MASTER_USER=‘rep‘,MASTER_PASSWORD=‘oldboy123‘;

Query OK, 0 rows affected (0.03 sec)

#注意這裡的MASTER_LOG_FILE=‘mysql-bin.000002‘和MASTER_LOG_POS=289是對應上述主庫在鎖表時查看主庫狀態資訊的內容,對應“1、6查看主庫binlog資訊”。


2、7一定要先執行上述change master再start slave

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: 10.0.0.51                  Master_User: rep                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000002          Read_Master_Log_Pos: 289               Relay_Log_File: db02-relay-bin.000002                Relay_Log_Pos: 253        Relay_Master_Log_File: mysql-bin.000002             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: 289              Relay_Log_Space: 408              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:   Replicate_Ignore_Server_Ids:              Master_Server_Id: 511 row in set (0.00 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               || testdb01           || testdb02           || testdb03           |+--------------------+7 rows in set (0.00 sec)


三、主庫和從庫測試


#測試主庫資料是否同步到了從庫

3、1主庫操作

mysql> create database testdb04;Query OK, 1 row affected (0.00 sec)mysql> show databases like ‘testdb04‘;+---------------------+| Database (testdb04) |+---------------------+| testdb04            |+---------------------+1 row in set (0.00 sec)


3、2從庫查看

mysql> show databases like ‘testdb04‘;+---------------------+| Database (testdb04) |+---------------------+| testdb04            |+---------------------+1 row in set (0.00 sec)


#現主從同步測試成功。


本文出自 “你可以選擇不平凡” 部落格,請務必保留此出處http://ylcodes01.blog.51cto.com/5607366/1853239

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.