MySQL主從複製

一:複製過程mysql

    Mysql主服務器接受客戶端的更新數據請求,在本地數據庫中保存下數據的更改,而後將更新操做記錄在本地的二進制日誌中,記錄爲更改事件.每當主服務器接受備用服務器的請求後,會經過3306端口,將其所請求的二進制事件文件,發送給備用服務器.備用服務器接受到二進制日誌文件,保存到本身的中繼日誌中.而後備用服務器的SQL線程從新回放全部的中繼日誌.進行sql重演.保證和主服務器數據一致.整個複製的過程是一個異步操做過程.sql

二:工做線程數據庫

主服務器:IO  Thread緩存

從服務器:IO  Thread ,SQL Threadbash


三:部署案例服務器

對於部署數據庫主從複製來說,分爲新安裝和添加從.下面先接受新安裝,而後再介紹添加從.dom

新安裝部署異步

步驟:ide

(1).主庫建立複製帳號
(2).配置主庫和從庫
(3).通知備庫鏈接到主庫進行數據複製

1.服務器規劃
測試

192.168.0.138  master
192.168.0.137  slave01

2.安裝mysql-2.6.27

http://yujianglei.blog.51cto.com/7215578/1725585


3.主庫建立複製帳號

mysql> grant  replication slave,replication client  on *.* to rep@'192.168.0.%'  identified by 'rep123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush  privileges;
Query OK, 0 rows affected (0.00 sec)


4.配置主庫和備庫

主庫:(192.168.0.138)

[mysqld]
server-id=1                             #惟一服務器ID       
log_bin=/mydata/bin_log/mysql-bin       #二進制日誌的文件位置和命名方式
#binlog_format=ROW
binlog_format=STATEMENT                 #二進制日誌格式
sync_binlog=1                           #
max_binlog_size=512M                    #最大二進制文件
binlog_cache_size=2M                    #二進制日誌緩存
expire_logs_days = 7                    #二進制日誌緩存時間

備庫:(192.168.0.137)

[mysqld]
server-id=2                             #惟一服務器ID
relay_log=/mydata/relay_log/mysql-relay #中繼日誌的文件位置和命名方式
log_slave_updates=1                     #容許備庫將重放的事件記錄到自身的二進制日誌中去
read_only=1                             #阻止任何沒有特權的線程修改數據

5.啓動複製,告訴從庫如何鏈接主庫,配置操做在從庫(192.168.0.137)中配置

(1)執行change操做

mysql> change  master  to master_host='192.168.0.138',master_user='rep',master_password='rep123',master_log_file='mysql-bin.000001',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.34 sec)

(2)開啓slave模式

mysql> start  slave;
Query OK, 0 rows affected (0.04 sec)

(3)查看slave狀態

mysql> show  slave  status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.138
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 120
               Relay_Log_File: mysql-relay.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-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: 120
              Relay_Log_Space: 452
              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:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 085ed1f9-a87a-11e5-9822-b8ca3af274df
             Master_Info_File: /mydata/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
1 row in set (0.00 sec)

6.查看主庫和從庫的工做線程

(1)查看slave服務器上得IO和SQL兩個線程

mysql> show  processlist;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    5
Current database: *** NONE ***
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+-------------------+
| Id | User        | Host      | db   | Command | Time | State                                                                       | Info              |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+-------------------+
|  3 | system user |           | NULL | Connect |  146 | Waiting for master to send event                                            | NULL              |
|  4 | system user |           | NULL | Connect |  146 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL              |
|  5 | root        | localhost | NULL | Query   |    0 | init                                                                        | show  processlist |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+-------------------+
3 rows in set (0.00 sec)

(2)查看slave服務器上的IO一個線程

mysql> show  processlist;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    3
Current database: mysql
+----+------+---------------------+-------+-------------+------+-----------------------------------------------------------------------+-------------------+
| Id | User | Host                | db    | Command     | Time | State                                                                 | Info              |
+----+------+---------------------+-------+-------------+------+-----------------------------------------------------------------------+-------------------+
|  2 | rep  | 192.168.0.137:32524 | NULL  | Binlog Dump |  546 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL              |
|  3 | root | localhost           | mysql | Query       |    0 | init                                                                  | show  processlist |
+----+------+---------------------+-------+-------------+------+-----------------------------------------------------------------------+-------------------+
2 rows in set (0.01 sec)

7.測試主從複製

主庫:

mysql> create database red_packet;
Query OK, 1 row affected (0.02 sec)
mysql> show  create  database  red_packet;
+------------+---------------------------------------------------------------------+
| Database   | Create Database                                                     |
+------------+---------------------------------------------------------------------+
| red_packet | CREATE DATABASE `red_packet` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+------------+---------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> grant  all   privileges  on red_packet.*  to red_packet@'localhost' identified  by  '123456';
Query OK, 0 rows affected (0.03 sec)

mysql> select  User,Host,Password  from  mysql.user;
+------------+-----------------------+-------------------------------------------+
| User       | Host                  | Password                                  |
+------------+-----------------------+-------------------------------------------+
| root       | localhost             | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root       | localhost.localdomain | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root       | 127.0.0.1             | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root       | ::1                   | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root       | %                     | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| rep        | 192.168.0.%           | *08F5BB4670F148DB0376A1EE646F0C67EAE138CE |
| red_packet | localhost             | *6081F775BC114C5673018A1B14DFB40CDB9B4AE2 |
+------------+-----------------------+-------------------------------------------+

從庫:

mysql> show  databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| red_packet         |
+--------------------+
4 rows in set (0.00 sec)
mysql> select  User,Host,Password  from  mysql.user;
+------------+-----------------------+-------------------------------------------+
| User       | Host                  | Password                                  |
+------------+-----------------------+-------------------------------------------+
| root       | localhost             | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root       | localhost.localdomain | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root       | 127.0.0.1             | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root       | ::1                   | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| root       | %                     | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| red_packet | localhost             | *6081F775BC114C5673018A1B14DFB40CDB9B4AE2 |
+------------+-----------------------+-------------------------------------------+
6 rows in set (0.00 sec)

結論:在主庫上建立的書庫和用戶在從庫上均可以查到,MYSQL主從複製OK!
相關文章
相關標籤/搜索