1. 程式人生 > >2018-4-2 15周1次課 MySQL主從配置

2018-4-2 15周1次課 MySQL主從配置

MySQL主從配置

17.1 MySQL主從介紹


·MySQL主從又叫做Replication、AB復制。簡單講就是A和B兩臺機器做主從後,在A上寫數據,另外一臺B也會跟著寫數據,兩者數據實時同步的

·MySQL主從是基於binlog的,主上須開啟binlog才能進行主從。

·主從過程大致有3個步驟

1)主將更改操作記錄到binlog裏

2)從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裏

3)從根據relaylog裏面的sql語句按順序執行

·主上有一個log dump線程,用來和從的I/O線程傳遞binlog

·從上有兩個線程,其中I/O線程用來同步主的binlog並生成relaylog,另外一個SQL線程用來把relaylog裏面的sql語句落地

技術分享圖片


備份庫

讀庫,減輕主庫的壓力





17.2 準備工作


安裝mysql

簡單步驟:

·wget 免編譯安裝包

·tar 解壓壓縮包

·mv 解壓出的目錄到/usr/local/mysql下 確保/usr/local/mysql空

·./scripts/mysql_install_db --user=mysql --datadir=/data/mysql 初始化並定義用戶和datadir(提前創建)

·編輯 /etc/my.cnf,定義datadir和socket位置

·cp support-files/mysql.server /etc/init.d/mysqld 復制啟動腳本到/etc/init.d下名稱為mysqld

·在mysqld中定義basedir,datadir

·啟動程序/etc/init.d/mysqld start

·chkconfig mysqld on 可以添加到開機啟動

確保進行主從的機器中,mysqld服務均已啟運行


安裝過程參考:http://blog.51cto.com/11530642/2073264





17.3 配置主


修改my.cnf,增加server-id=128(可以根據ip來定)和log_bin=alex(logbin的前綴)

技術分享圖片

[root@localhost mysql]# /etc/init.d/mysqld restart                ##重啟mysqld
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!



把mysql庫備份並恢復成aming庫,作為測試數據

[root@localhost mysql]# mysqldump -uroot -p123456 mysql > /tmp/mysql.sql        ##備份數據庫
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]# mysql -uroot -p123456 -e'create database a4l'           ##創建新庫
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]# mysql -uroot -p123456 a4l < /tmp/mysql.sql             ##導入數據庫
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]# ll
總用量 177292
drwx------ 2 mysql mysql     4096 3月  31 20:51 a4l
-rw-rw---- 1 mysql mysql   663420 3月  31 20:51 alex.000001
-rw-rw---- 1 mysql mysql       14 3月  31 20:34 alex.index
-rw-rw---- 1 mysql mysql       56 3月  12 21:59 auto.cnf
-rw-rw---- 1 mysql mysql 79691776 3月  31 20:51 ibdata1
-rw-rw---- 1 mysql mysql 50331648 3月  31 20:51 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 3月  12 21:47 ib_logfile1
-rw-rw---- 1 mysql mysql   111979 3月  31 20:34 localhost.localdomain.err
-rw-rw---- 1 mysql mysql        5 3月  31 20:34 localhost.localdomain.pid
drwx------ 2 mysql mysql     4096 3月  18 22:52 mysql
drwx------ 2 mysql mysql     4096 3月  18 22:52 mysql2
drwx------ 2 mysql mysql     4096 3月  12 21:47 performance_schema
drwx------ 2 mysql mysql        6 3月  12 21:47 test
drwx------ 2 mysql mysql      324 3月  30 22:48 zrlog


創建用作主從同步的用戶:

[root@localhost mysql]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant replication slave on *.* to 'repl'@192.168.65.129 identified by '123456';
Query OK, 0 rows affected (0.00 sec)                        ## 創建用作同步數據的用戶

mysql> flush tables with read lock;                        ##鎖表,防止數據變動
Query OK, 0 rows affected (0.01 sec)

mysql> show master status;
+-------------+----------+--------------+------------------+-------------------+
| File        | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------+----------+--------------+------------------+-------------------+
| alex.000001 |   663631 |              |                  |                   |
+-------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)



備份需要同步的數據庫:

[root@localhost mysql]# mysqldump -uroot -p123456 a4l > /tmp/a4l.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]# mysqldump -uroot -p123456 mysql2 > /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]# mysqldump -uroot -p123456 zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.

(mysql中有許多用戶名和密碼,沒辦法把權限全部復制,所以不需要同步)





17.4 配置從


安裝mysql

[root@localhost ~]# vim /etc/my.cnf

技術分享圖片

(從不需要bin_log,只有主才需要)


[root@localhost ~]# /etc/init.d/mysqld restart                    ##重啟mysqld
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@localhost ~]# ls /data/mysql/
auto.cnf  ib_logfile0  localhost.localdomain.err  mysql   performance_schema
ibdata1   ib_logfile1  localhost.localdomain.pid  mysql2  test

[root@localhost ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database a4l;                                    ##創建數據庫
Query OK, 1 row affected (0.00 sec)

mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)

mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)

mysql> quit
Bye                                                            ##導入數據

[root@localhost ~]# mysql -uroot -p123456 a4l < /tmp/a4l.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p123456 mysql2 < /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p123456 zrlog < /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.


傳輸數據:

[root@localhost ~]# scp 192.168.65.128:/tmp/*.sql /tmp/
The authenticity of host '192.168.65.128 (192.168.65.128)' can't be established.
ECDSA key fingerprint is SHA256:PXl0tJsOm3464x52jTxX7L+ToKm1Hb6AUMq6lh4ASX0.
ECDSA key fingerprint is MD5:2e:9a:f9:25:b3:80:43:05:a4:3d:3c:9b:48:6e:a0:0e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.65.128' (ECDSA) to the list of known hosts.
[email protected]'s password:
a4l.sql                                                   100%  648KB  23.2MB/s   00:00
my2.sql                                                   100%  642KB  27.3MB/s   00:00
zrlog.sql                                                  100%   10KB   3.1MB/s   00:00

技術分享圖片技術分享圖片

技術分享圖片

技術分享圖片

[root@localhost ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_host='192.168.65.128', master_user='repl', master_password='123456', master_log_file='alex.000001', master_log_pos=663631;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.65.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: alex.000002
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 278
Relay_Master_Log_File: alex.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: 120
Relay_Log_Space: 613
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: 128
Master_UUID: bc3bf10d-34ed-11e8-b6a9-000c297e8b1b
Master_Info_File: /data/mysql/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)

status中兩個yes才是成功:

技術分享圖片


回到主服務器上,unlock tables

[root@localhost mysql]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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


錯誤匯總:

1,Slave_IO_Running: No

查看錯誤問題:

Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

查看主從服務器兩臺機器/data/mysql/auto.cnf的UUID是否一樣,如果一樣,刪除其中一個,並重啟該服務器,再次進行從服務器mysql的change master操作就能成功了。





17.5 測試主從同步


在my.cnf中定義:可以在主上,也可以在從上

主服務器上

binlog-do-db= //僅同步指定的庫,多個可以用 , 去分割

binlog-ignore-db= //忽略指定庫

從服務器上

replicate_do_db=

replicate_ignore_db=

replicate_do_table=

replicate_ignore_table=

replicate_wild_do_table= //如alex.%, 支持通配符%

replicate_wild_ignore_table=

(最長用是最後兩條)

從上刪除comment表中的信息,主上也沒了

技術分享圖片

技術分享圖片

主上刪除了comment表,從上comment表也不存在了

技術分享圖片

技術分享圖片


主上 mysql -uroot aming

select count(*) from db;

truncate table db;

到從上 mysql -uroot aming

select count(*) from db;

主上繼續drop table db;

從上查看db表


2018-4-2 15周1次課 MySQL主從配置