mysql主從

1mysql主從介紹

1 MySQL主從又叫做Replication、AB複製。簡單講就是A和B兩臺機器做主從後,在A上寫數據,另外一臺B也會跟着寫數據,兩者數據實時同步的
2 MySQL主從是基於binlog的,主上須開啓binlog才能進行主從

如下是介紹主從過程:
1)主將更改操作記錄到binlog裏
2)從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog(中繼日誌)裏
3)從根據relaylog裏面的sql語句按順序執行
主上有一個log dump線程,用來和從的I/O線程傳遞binlog
從上有兩個線程,其中I/O線程用 來同步主的binlog並生成relaylog,另外一個SQL線程用來把relaylog裏面的sql語句執行

mysql主從的應用場景:(1)數據備份-主執行操作,備機-只備份(不執行其它操作)
(2)備機備份-但是還需要在從的服務器上都數據(不能寫數據)

2主從的準備工作

主機器的操作:

首先是下安裝mysql-解壓mysql-給mysql用戶-移動mysql到/usr/local/mysql/下(移動之前需要注意這個目錄下是否還有mysql這個目錄確保這個目錄下只有一個mysql目錄)-創建mysql數據庫-初始化mysql-更改my.cnf-拷貝啓動腳本-設置開機啓動(基礎的就已經完成)
[root@chy01 ~]# ps aux |grep mysql
root       2411  0.0  0.1 115376  1680 ?        S    22:15   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/chy01.pid
mysql      2659  1.8 29.7 1038628 454696 ?      Sl   22:15   0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/chy01.err --pid-file=/data/mysql/chy01.pid --socket=/tmp/mysql.sock
root       2889  0.0  0.0 112664   976 pts/0    R+   22:17   0:00 grep --color=auto mysql
(查看mysql已經啓動)
同樣在從服務器上也是需要同樣的步驟完成基本的Mysql操作之後啓動mysql

3配置主服務器

[root@chy01 src]# vim /etc/my.cnf
(更改主的配置文件)
[mysqld]裏面增加如下兩項
server-id = 11
log_bin=chylinux
其中server-id = 11 (11指的是主機ip的最後一位的數字,log_bin=chylinux定義log的前綴。)
[root@chy01 src]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
(重啓mysql)
[root@chy01 mysql]# ls
auto.cnf          chy01-bin.000004  chy01.err        db1          mysql               xtrabackup_binlog_pos_innodb
chy01-bin.000001  chy01-bin.000005  chy01.pid        ibdata1      mysql2              xtrabackup_info
chy01-bin.000002  chy01-bin.000006  chylinux.000001  ib_logfile0  performance_schema  zrlog
chy01-bin.000003  chy01-bin.index   chylinux.index   ib_logfile1  test
(查看mysql,
[root@chy01 mysql]# mysql -uroot -paminglinux -e "create database zhucong"
Warning: Using a password on the command line interface can be insecure.
(創建mysql主從庫)

mysql> grant replication slave on *.* to 'repl'@'192.168.212.10' identified by 'chylinux';
Query OK, 0 rows affected (0.00 sec)
(創建於從服務器備份時的用戶名與密碼,ip地址是從服務器的ip地址。)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
(鎖定表,目的是不讓此表在寫數據,備的服務器需要同步數據庫)
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| chylinux.000006 |      331 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
(查看主服務器的status)
[root@chy01 mysql]# mysqldump -uroot -paminglinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@chy01 mysql]# mysqldump -uroot -paminglinux db1 > /tmp/db1.sql
Warning: Using a password on the command line interface can be insecure.
(需要在主服務器上備份數據庫,然後在從服務器上進行同步)
[root@chy01 mysql]# ls /tmp/*sql
/tmp/db1.sql  /tmp/mysq_all.sql  /tmp/mysql2.sql  /tmp/mysqlbak.sql  /tmp/user.sql  /tmp/zrlog.sql
(查看主服務器上面的備份數據庫)

4 配置從服務器

[root@chy ~]# vim /etc/my.cnf
[mysqld]
server-id=10
(在從服務器上增加一個serverid,不需要配置binlog)
[root@chy ~]# /etc/init.d/mariadb restart
Restarting mariadb (via systemctl):                        [  確定  ]
(啓動mariadb數據庫)
[root@chy ~]# scp 192.168.212.11:/tmp/*.sql /tmp/
The authenticity of host '192.168.212.11 (192.168.212.11)' can't be established.
ECDSA key fingerprint is de:d2:32:86:e0:89:5c:2c:51:68:92:9b:7e:40:52:5c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.212.11' (ECDSA) to the list of known hosts.
[email protected]'s password: 
db1.sql                                                                              100% 1257     1.2KB/s   00:00    
mysq_all.sql                                                                         100% 1275KB   1.3MB/s   00:00    
mysql2.sql                                                                           100%   30KB  30.1KB/s   00:00    
mysqlbak.sql                                                                         100%  638KB 637.7KB/s   00:00    
user.sql                                                                             100% 6762     6.6KB/s   00:00    
zrlog.sql                                                                            100%   10KB   9.9KB/
(將主服務器/tmp/下的備份數據庫拷貝到從服務器上)
MariaDB [(none)]> create database db1;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> create database mysql2;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> create database zrlog;
Query OK, 1 row affected (0.00 sec)
(創建3個庫)
[root@chy ~]# mysql -uroot mysql2 < /tmp/mysql2.sql
[root@chy ~]# mysql -uroot zrlog < /tmp/zrlog.sql
[root@chy ~]# mysql -uroot db1 < /tmp/db1.sql
(恢復從服務器的三個數據庫)
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [(none)]> change master to master_host='192.168.212.11', master_user='repl', master_password='chylinux', master_log_file='chylinux.000001', master_log_pos=474566;
Query OK, 0 rows affected (0.03 sec)
(執行)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.212.11
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: chylinux.000001
          Read_Master_Log_Pos: 474566
               Relay_Log_File: chy-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: chylinux.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
(這裏面只有一個yes,IOrunning 是connecting,這裏出現了問題,這裏的問題是我之前在主服務器上授權時 grant replication slave on *.* to 'repl'@'192.168.212.10' identified by 'chylinux';這裏有個錯誤就是ip後面少了一個'號當重新授權時就變爲了MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.212.11
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: chylinux.000006
          Read_Master_Log_Pos: 474566
               Relay_Log_File: chy-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: chylinux.000006
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
no 這裏看到報了一個錯:Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from position > file size; the first event 'chylinux.000006' at 474566, the last event read from './chylinux.000006' at 4, the last byte read from './chylinux.000006' at 4.'
[root@chy ~]# tail -n5 /data/mariadb/chy.err 
2017-08-31 23:03:50 139775179691776 [Note] Slave SQL thread initialized, starting replication in log 'chylinux.000006' at position 474566, relay log './chy-relay-bin.000001' position: 4
2017-08-31 23:03:50 139775179441920 [Note] Slave I/O thread: connected to master '[email protected]:3306',replication started in log 'chylinux.000006' at position 474566
2017-08-31 23:03:50 139775179441920 [ERROR] Error reading packet from server: Client requested master to start replication from position > file size; the first event 'chylinux.000006' at 474566, the last event read from './chylinux.000006' at 4, the last byte read from './chylinux.000006' at 4. (server_errno=1236)
2017-08-31 23:03:50 139775179441920 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from position > file size; the first event 'chylinux.000006' at 474566, the last event read from './chylinux.000006' at 4, the last byte read from './chylinux.000006' at 4.', Internal MariaDB error code: 1236
2017-08-31 23:03:50 139775179441920 [Note] Slave I/O thread exiting, read up to log 'chylinux.000006', position 474566(查看錯誤日誌發現了Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from position > file size; the first event 'chylinux.000006' at 474566, the last event read from './chylinux.000006' at 4, the last byte read from './chylinux.000006' at 4.', Internal MariaDB error code: 1236這一段 
這時需要去主服務器上查看它的'./chylinux.000006' at 4的pos 具體操作如下[root@chy01 mysql]# mysqlbinlog /data/mysql/chy01-bin.000006 > /tmp/zhucong.txt(這是主服務器上面的操作,將這個文件重定向到zhucong.txt這樣方便查看
[root@chy01 mysql]# less /tmp/zhucong.txt
# at 4
#170830 22:15:52 server id 1  end_log_pos 120 CRC32 0xc388e931  Start: binlog v 4, server v 5.6.35-log created 170830 22:15:52 at startup
less查看時可以看到at 後面的pos 爲120
這時需要到從服務器上操作執行MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> change master to master_host='192.168.212.11', master_user='repl', master_password='chylinux', master_log_file='chylinux.000006', master_log_pos=120;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.212.11
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: chylinux.000006
          Read_Master_Log_Pos: 331
               Relay_Log_File: chy-relay-bin.000002
                Relay_Log_Pos: 629
        Relay_Master_Log_File: chylinux.000006
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
可以查看到沒有問題都是yes)
最後需要在主服務器上面恢復寫操作:
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
需要注意的是在同步時兩邊的數據需要一致。
[root@chy ~]# ls /tmp/*.sql
/tmp/db1.sql  /tmp/mysq_all.sql  /tmp/mysql2.sql  /tmp/mysqlbak.sql  /tmp/user.sql  /tmp/zhucong.sql  /tmp/zrlog.sql
(從服務器上的數據庫)
[root@chy01 mysql]# ls /tmp/*.sql
/tmp/db1.sql  /tmp/mysq_all.sql  /tmp/mysql2.sql  /tmp/mysqlbak.sql  /tmp/user.sql  /tmp/zhucong.sql  /tmp/zrlog.sql
(主的數據庫)

4測試主從同步

 主服務器上
 [root@chy01 ~]# vim /etc/my.cnf
 binlog-do-db=      //僅同步指定的庫
 binlog-ignore-db= //忽略指定庫
 從服務器上
 [root@chy ~]# vim /etc/my.cnf
 replicate_wild_do_table=   //如aming.%, 支持通配符% 
 replicate_wild_ignore_table=
 (如上要是可以同步(忽略)某個庫並且(忽略)某個表的操作)
 主服務器上的操作查看是否可以主從同步:
mysql> use zhucong;
Database changed
(首先到主服務器上面切換到zhucong的數據庫裏面,
mysql> show tables;
Empty set (0.00 sec)
(查看錶,裏面沒有任何表)
mysql> create table t1(`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.13 sec)

mysql> show tables;
+-------------------+
| Tables_in_zhucong |
+-------------------+
| t1                |
+-------------------+
1 row in set (0.00 sec)
(在主的服務器上執行操作可以看到有一個表t1)
如下是在從服務器上面的查看:
MariaDB [zhucong]> show tables;
+-------------------+
| Tables_in_zhucong |
+-------------------+
| t1                |
+-------------------+
1 row in set (0.00 sec)

MariaDB [zhucong]> 
(可以看到同步成功)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章