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語句落地
兩個場景:
1,從作爲數據備份
2,不僅僅備份,主上寫,從上讀,減輕主的壓力
17.2 準備工作
從機器上安裝 mysql:
[root@arslinux-02 src]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz [root@arslinux-02 src]# tar xvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz [root@arslinux-02 src]# mv mysql-5.6.43-linux-glibc2.12-x86_64/ /usr/local/mysql
[root@arslinux-02 mysql]# useradd mysql [root@arslinux-02 mysql]# mkdir /data/ [root@arslinux-02 mysql]# mkdir /data/mysql/
[root@arslinux-02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/
[root@arslinux-02 mysql]# vim /etc/my.cnf [mysqld] datadir=/data/mysql socket=/tmp/mysql.sock
[root@arslinux-02 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@arslinux-02 mysql]# vim /etc/init.d/mysqld basedir=/usr/local/mysql datadir=/data/mysql
[root@arslinux-02 mysql]# /etc/init.d/mysqld start Starting MySQL. SUCCESS! [root@arslinux-02 mysql]# ps aux|grep mysqld root 7488 0.1 0.1 113312 1616 pts/0 S 16:56 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/arslinux-02.pid mysql 7652 8.7 45.1 1302736 449792 pts/0 Sl 16:56 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql.log --pid-file=/data/mysql/arslinux-02.pid --socket=/tmp/mysql.sock root 7676 0.0 0.0 112724 984 pts/0 R+ 16:57 0:00 grep --color=auto mysqld [root@arslinux-02 mysql]# netstat -lntp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6926/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7061/master tcp6 0 0 :::3306 :::* LISTEN 7652/mysqld tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 6926/sshd tcp6 0 0 ::1:25 :::* LISTEN 7061/master
[root@arslinux-02 mysql]# chkconfig mysqld on [root@arslinux-02 mysql]# chkconfig --list 注:該輸出結果只顯示 SysV 服務,並不包含 原生 systemd 服務。SysV 配置數據 可能被原生 systemd 配置覆蓋。 要列出 systemd 服務,請執行 'systemctl list-unit-files'。 查看在具體 target 啓用的服務請執行 'systemctl list-dependencies [target]'。 mysqld 0:關1:關2:開3:開4:開5:開6:關 netconsole 0:關1:關2:關3:關4:關5:關6:關 network 0:關1:關2:開3:開4:開5:開6:關
安裝過程參考:https://blog.51cto.com/11530642/2390722
17.3 配置主
修改 my.cnf,增加 server-id(可以根據ip來定),log_bin(logbin的前綴)
[root@arslinux-01 mysql]# vim /etc/my.cnf server-id = 130 log_bin = arselinux
重啓 mysql
[root@arslinux-01 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL... SUCCESS! [root@arslinux-01 ~]# ll /data/mysql/ 總用量 110748 -rw-rw---- 1 mysql mysql 120 6月 1 17:35 arselinux.000001 -rw-rw---- 1 mysql mysql 19 6月 1 17:35 arselinux.index -rw-rw---- 1 mysql mysql 128324 6月 1 17:35 arslinux-01.err -rw-rw---- 1 mysql mysql 5 6月 1 17:35 arslinux-01.pid -rw-rw---- 1 mysql mysql 56 5月 13 21:55 auto.cnf -rw-rw---- 1 mysql mysql 12582912 6月 1 17:35 ibdata1 -rw-rw---- 1 mysql mysql 50331648 6月 1 17:35 ib_logfile0 -rw-rw---- 1 mysql mysql 50331648 5月 13 21:50 ib_logfile1 drwx------ 2 mysql mysql 4096 5月 21 22:49 mysql drwx------ 2 mysql mysql 4096 5月 21 22:43 mysql2 drwx------ 2 mysql mysql 4096 5月 13 21:50 performance_schema drwx------ 2 mysql mysql 6 5月 13 21:50 test drwx------ 2 mysql mysql 324 5月 28 23:03 zrlog
備份 mysql 庫,創建 arslinux 庫,將備份導入 arslinux 庫
[root@arslinux-01 ~]# mysqldump -uroot -parslinux mysql > /tmp/mysql.sql Warning: Using a password on the command line interface can be insecure. [root@arslinux-01 ~]# mysql -uroot -parslinux -e'create database arslinux' Warning: Using a password on the command line interface can be insecure. [root@arslinux-01 ~]# mysql -uroot -parslinux arslinux < /tmp/mysql.sql Warning: Using a password on the command line interface can be insecure.
創建用作主從同步的用戶
mysql> grant replication slave on *.* to 'repl'@192.168.194.132 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 | +------------------+----------+--------------+------------------+-------------------+ | arselinux.000001 | 655378 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
備份 /data/mysql 下需要主從同步的數據庫
[root@arslinux-01 ~]# mysqldump -uroot -parslinux mysql2 > /tmp/mysql2.sql Warning: Using a password on the command line interface can be insecure. [root@arslinux-01 ~]# mysqldump -uroot -parslinux zrlog > /tmp/zrlog.sql Warning: Using a password on the command line interface can be insecure. [root@arslinux-01 ~]# mysqldump -uroot -parslinux arslinux > /tmp/arslinux.sql Warning: Using a password on the command line interface can be insecure.
17.4 配置從
編輯配置 /etc/my.cnf,增加server-id(從不需要bin_log,只有主才需要)
[root@arslinux-02 mysql]# vim /etc/my.cnf server-id = 132
重啓 mysql
[root@arslinux-02 mysql]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL.. SUCCESS! [root@arslinux-02 mysql]# ll /data/mysql/ 總用量 110608 -rw-rw---- 1 mysql mysql 5 6月 1 18:19 arslinux-02.pid -rw-rw---- 1 mysql mysql 56 6月 1 16:56 auto.cnf -rw-rw---- 1 mysql mysql 12582912 6月 1 18:19 ibdata1 -rw-rw---- 1 mysql mysql 50331648 6月 1 18:19 ib_logfile0 -rw-rw---- 1 mysql mysql 50331648 6月 1 16:51 ib_logfile1 drwx------ 2 mysql mysql 4096 6月 1 16:51 mysql drwx------ 2 mysql mysql 4096 6月 1 16:51 performance_schema drwx------ 2 mysql mysql 6 6月 1 16:48 test
傳輸數據,將主上庫備份傳到從上
[root@arslinux-02 mysql]# scp 192.168.194.130:/tmp/*.sql /tmp/ arslinux.sql 100% 638KB 14.6MB/s 00:00 mysql2.sql 100% 632KB 14.2MB/s 00:00 mysql.sql 100% 638KB 13.6MB/s 00:00 zrlog.sql 100% 10KB 2.2MB/s 00:00
給 mysql 和 mysqldump 做 alias(因爲沒加入 PATH )
[root@arslinux-02 mysql]# alias 'mysql=/usr/local/mysql/bin/mysql' [root@arslinux-02 mysql]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
創建庫
[root@arslinux-02 mysql]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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 arslinux; 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@arslinux-02 mysql]# mysql -uroot arslinux < /tmp/arslinux.sql [root@arslinux-02 mysql]# mysql -uroot mysql2 < /tmp/mysql2.sql [root@arslinux-02 mysql]# mysql -uroot zrlog < /tmp/zrlog.sql [root@arslinux-02 mysql]# ll /data/mysql/ 總用量 226264 drwx------ 2 mysql mysql 4096 6月 1 18:28 arslinux -rw-rw---- 1 mysql mysql 5 6月 1 18:19 arslinux-02.pid -rw-rw---- 1 mysql mysql 56 6月 1 16:56 auto.cnf -rw-rw---- 1 mysql mysql 79691776 6月 1 18:29 ibdata1 -rw-rw---- 1 mysql mysql 50331648 6月 1 18:29 ib_logfile0 -rw-rw---- 1 mysql mysql 50331648 6月 1 16:51 ib_logfile1 drwx------ 2 mysql mysql 4096 6月 1 16:51 mysql drwx------ 2 mysql mysql 4096 6月 1 18:28 mysql2 drwx------ 2 mysql mysql 4096 6月 1 16:51 performance_schema drwx------ 2 mysql mysql 6 6月 1 16:48 test drwx------ 2 mysql mysql 324 6月 1 18:29 zrlog
實現主從同步
[root@arslinux-02 mysql]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.6.43 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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, 1 warning (0.00 sec) mysql> change master to master_host='192.168.194.130',master_user='repl',master_password='123456',master_log_file='arselinux.000001',master_log_pos=655378; Query OK, 0 rows affected, 2 warnings (0.04 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.194.130 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: arselinux.000001 Read_Master_Log_Pos: 655378 Relay_Log_File: arslinux-02-relay-bin.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: arselinux.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: 655378 Relay_Log_Space: 462 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: 130 Master_UUID: c164593f-7586-11e9-a13a-000c2924eaf2 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中 Slave_IO_Running 和 Slave_SQL_Running 如果兩個都是 Yes 表示配置主從成功
還需關注
Seconds_Behind_Master: 0 //爲主從延遲的時間
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
到主上將表恢復寫操作
[root@arslinux-01 ~]# mysql -uroot -parslinux 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 15 Server version: 5.6.43-log MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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= //如arslinux.%, 支持通配符%
replicate_wild_ignore_table=
最常用是最後兩條
驗證是否同步
主:
mysql> select count(*) user; +------+ | user | +------+ | 1 | +------+ 1 row in set (0.00 sec)
從:
mysql> select count(*) user; +------+ | user | +------+ | 1 | +------+ 1 row in set (0.00 sec)
主:
mysql> truncate table user; Query OK, 0 rows affected (0.00 sec) mysql> select * from user; Empty set (0.00 sec)
從:
mysql> select * from user; Empty set (0.01 sec)
主上直接刪除表
主:
mysql> drop table user; Query OK, 0 rows affected (0.00 sec)
從:
mysql> select * from user; ERROR 1146 (42S02): Table 'arslinux.user' doesn't exist
如果主從出現錯誤,可以嘗試從上 stop slave,再 start slave,如果還是有問題,那麼就要重新做主從了
測試主從:
主上 mysql -uroot arslinux
select count(*) from db;
truncate table db;
到從上 mysql -uroot arslinux
select count(*) from db;
主上繼續drop table db;
從上查看db表
有人遇到主從不能正常同步,提示uuid相同的錯誤。這是因爲克隆機器導致。
https://www.2cto.com/database/201412/364479.html
擴展部分
不停庫不鎖表在線主從配置
http://seanlook.com/2015/12/14/mysql-replicas/
主從不同步
http://www.rfyy.net/archives/2309.html
https://blog.51cto.com/storysky/259280
主主
關於 auto_increment https://blog.csdn.net/leshami/article/details/39779509
http://www.cnblogs.com/ygqygq2/p/6045279.html
mysql-proxy 實現讀寫分離
https://blog.51cto.com/zzclinux/1980487
mysql-proxy類似的產品有:
mycat 基於阿里的開源軟件cobar,官網 www.mycat.io
https://my.oschina.net/ruoli/blog/1789370
mycat實現分庫分表
https://www.cnblogs.com/joylee/p/7513038.html
atlas 出自於360,不維護不更新了 https://blog.csdn.net/AnPHPer/article/details/80566385
mysql環形主從
http://ask.apelearn.com/question/11437
mysql架構演變 http://www.aminglinux.com/bbs/thread-8025-1-1.html
MHA架構
https://blog.51cto.com/xiaoshuaigege/2060768
比較複雜的mysql集羣架構 http://ask.apelearn.com/question/17026
直播筆記
1.xtrabackup ——> innobackupex(支持myisam) 不鎖表備份
mariadb 備份和恢復
2.幾篇和mysql主從有關的文章 關鍵詞 GTID
https://blog.csdn.net/xiaoyi23000/article/details/80521423
https://blog.csdn.net/u013399093/article/details/70568837
https://www.cnblogs.com/abobo/p/4242417.html
3.瞭解幾個關鍵詞
讀寫分離 一主一從,一主多從,一主寫,從只讀,降低負載
分庫分表