MySQL Replication配置

MySQL Replication配置

1、MySQL主從介紹

  • MySQL主從又叫作Replication、AB複製。簡單講就是A和B兩臺機器作主從後,在A上寫數據,另一臺B也會跟着寫數據,二者數據實時同步;
  • MySQL主從是基於binlog的,主上須開啓binlog才能進行主從;
  • 主從過程大體有3個步驟:
    • 主將更改操做記錄到binlog裏
    • 從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裏
    • 從根據relaylog裏面的sql語句按順序執行
  • 主(master)上有一個log dump線程,用來和從的I/O線程傳遞binlong;
  • 從(slave)上有兩個線程,其中I/O線程用來同步主的binlog並生成relaylog,另一個sql線程用來把relaylog裏面的sql語句落地。

2、準備工做

作實驗須要兩臺機器,且都須要mysql;php

  • 主機1 ying01 192.168.112.136;
  • 主機2 ying02 192.168.112.138 ;

第一步:先查看ying02是否安裝mysqlmysql

[root@ying02 ~]# ls /usr/local/mysql
ls: 沒法訪問/usr/local/mysql: 沒有那個文件或目錄
[root@ying02 ~]# ps aux |grep mysql
root      1270  0.0  0.0 112676   980 pts/0    S+   11:50   0:00 grep --color=auto mysql
[root@ying02 ~]# cd /usr/local/src/
[root@ying02 src]# ls

第二步;把ying01上的mysql 5.6的安裝包,複製到ying02機器上;linux

利用xftp軟件,先把ying01上的mysql 5.6安裝包複製到桌面;web

接着再從桌面移到/usr/local/src/sql

第三步:此時,解壓,建立用戶,編譯等(見LNMP 筆記)數據庫

[root@ying02 src]# ls
mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
[root@ying02 src]# tar zxf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz 
[root@ying02 src]# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
[root@ying02 src]# ls /usr/local/mysql/
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
[root@ying02 src]# cd /usr/local/mysql/
[root@ying02 mysql]# useradd mysql

[root@ying02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

編譯的時候出現錯誤,須要安裝 perl perl-devel庫vim

[root@ying02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
-bash: ./scripts/mysql_install_db: /usr/bin/perl: 壞的解釋器: 沒有那個文件或目錄
[root@ying02 mysql]# yum -y install perl perl-devel

繼續安裝 perl-Data-Dumperbash

[root@ying02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
[root@ying02 mysql]# yum install -y perl-Data-Dumper

繼續按提示安裝:libaio服務器

[root@ying02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[root@ying02 mysql]# yum install -y libaio

此時,編譯成功,用echo $? 檢測,爲0正確socket

[root@ying02 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

[root@ying02 mysql]# echo $?
0

第四步:按下面,配置my.cnf文件

[root@ying02 mysql]# vi /etc/my.cnf

[mysqld]
datadir=/data/mysql          //指定數據目錄
socket=/tmp/mysql.sock       //指定sock位置

第五步:把mysql腳本,複製到/etc/init.d下,並更名爲mysqld;並配置腳本文件;

[root@ying02 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@ying02 mysql]# vi /etc/init.d/mysqld   //腳本文件,按下面配置

basedir=/usr/local/mysql     
datadir=/data/mysql

第六步:啓動mysql腳本服務;

[root@ying02 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/ying02.err'.
. SUCCESS!

3、配置主

如今把ying01做爲主(master),把ying02做爲從(slave)

按下面提示,編輯主配置文件;

[root@ying01 src]# vim /etc/my.cnf


[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=136                   //須要添加,id任意數字
log_bin=ying01                  //前綴

重啓mysql服務;查看數據目錄有 ying01.000001和ying.index兩個文件;

[root@ying01 src]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
`
[root@ying01 src]# cd /data/mysql/       //切換到mysql數據目錄下
[root@ying01 mysql]# ls -l
總用量 176308
-rwxr-xr-x 1 mysql mysql    16384 6月  27 09:58 aria_log.00000001
-rwxr-xr-x 1 mysql mysql       52 6月  27 09:58 aria_log_control
-rwxr-xr-x 1 mysql mysql       56 6月  24 08:45 auto.cnf
-rwxr-xr-x 1 mysql mysql     2747 6月  27 09:58 ib_buffer_pool
-rwxr-xr-x 1 mysql mysql 79691776 7月  19 13:26 ibdata1
-rw-rw---- 1 mysql mysql 50331648 7月  19 13:26 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 7月  19 13:22 ib_logfile1
-rwxr-xr-x 1 mysql mysql        0 6月  24 17:37 multi-master.info
drwxr-xr-x 2 mysql mysql     4096 7月  11 22:48 mysql
drwx------ 2 mysql mysql     4096 7月  11 23:28 mysql2
drwxr-xr-x 2 mysql mysql     4096 6月  24 07:45 performance_schema
drwxr-xr-x 2 mysql mysql        6 6月  24 07:45 test
-rw-rw---- 1 mysql mysql      120 7月  19 13:26 ying01.000001      //新生成的
-rw-rw---- 1 mysql mysql    89233 7月  19 13:26 ying01.err  
-rw-rw---- 1 mysql mysql       16 7月  19 13:26 ying01.index       //新生成的索引頁
-rw-rw---- 1 mysql mysql        5 7月  19 13:26 ying01.pid
drwx------ 2 mysql mysql      324 7月  16 23:59 zrlog

先把zrlog數據庫作備份,並更名爲 yingying數據庫;

[root@ying01 mysql]# mysqldump -uroot -pyinglinux zrlog > /tmp/zrlog.sql  //先備份
Warning: Using a password on the command line interface can be insecure.
[root@ying01 mysql]# du -sh /tmp/zrlog.sql
12K	/tmp/zrlog.sql
[root@ying01 mysql]# mysql -uroot -pyinglinux -e "create database yingying" //建立yingying數據庫
Warning: Using a password on the command line interface can be insecure.
[root@ying01 mysql]# mysql -uroot -pyinglinux yingying < /tmp/zrlog.sql     //再把備份的寫入到yingying
Warning: Using a password on the command line interface can be insecure.
[root@ying01 mysql]# ls -lt
總用量 176276
-rw-rw---- 1 mysql mysql 50331648 7月  19 13:30 ib_logfile0
-rwxr-xr-x 1 mysql mysql 79691776 7月  19 13:30 ibdata1
-rw-rw---- 1 mysql mysql    10999 7月  19 13:30 ying01.000001
drwx------ 2 mysql mysql      324 7月  19 13:30 yingying               //備份的數據庫,yingying
-rw-rw---- 1 mysql mysql    89233 7月  19 13:26 ying01.err
-rw-rw---- 1 mysql mysql        5 7月  19 13:26 ying01.pid
-rw-rw---- 1 mysql mysql       16 7月  19 13:26 ying01.index
-rw-rw---- 1 mysql mysql 50331648 7月  19 13:22 ib_logfile1
drwx------ 2 mysql mysql      324 7月  16 23:59 zrlog
drwx------ 2 mysql mysql     4096 7月  11 23:28 mysql2
drwxr-xr-x 2 mysql mysql     4096 7月  11 22:48 mysql
-rwxr-xr-x 1 mysql mysql    16384 6月  27 09:58 aria_log.00000001
-rwxr-xr-x 1 mysql mysql       52 6月  27 09:58 aria_log_control
-rwxr-xr-x 1 mysql mysql     2747 6月  27 09:58 ib_buffer_pool
-rwxr-xr-x 1 mysql mysql        0 6月  24 17:37 multi-master.info
-rwxr-xr-x 1 mysql mysql       56 6月  24 08:45 auto.cnf
drwxr-xr-x 2 mysql mysql     4096 6月  24 07:45 performance_schema
drwxr-xr-x 2 mysql mysql        6 6月  24 07:45 test
[root@ying01 mysql]#

進入mysql;

[root@ying01 mysql]# mysql -uroot -pyinglinux
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 4
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>

授予repl用戶(所屬IP)從複製權限,針對全部的數據庫及表,以及密碼

mysql> grant replication slave on *.* to 'repl'@'192.168.112.138' identified by 'yinglinux111';
Query OK, 0 rows affected (0.00 sec)

grant 權限 on 數據庫對象 to 用戶

名詞 解釋
replication slave 從複製權限
*.* 數據庫.表
repl 主從複製的用戶
IP slave的IP

先鎖定數據庫以防止寫入數據;

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

查看master狀態

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

mysql> quit
Bye

把須要主從複製的數據庫都備份

[root@ying01 mysql]# ls /tmp/
1212.txt         php_errors.log-20180704                                                   test.com.log
hsperfdata_root  php-fcgi.sock                                                             user.sql
mysql_all.sql    systemd-private-0b22f6f6d29c4dd98b413eb07065989f-chronyd.service-k7zu0o   ying.sock
mysqlbak.sql     systemd-private-0b22f6f6d29c4dd98b413eb07065989f-vgauthd.service-2DBZkO   zrlog.sql
mysql.sock       systemd-private-0b22f6f6d29c4dd98b413eb07065989f-vmtoolsd.service-25Cn8e
[root@ying01 mysql]# mysqldump -uroot -pyinglinux mysql2 > /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.

4、配置從

如今在從端配置: ying02 192.168.112.138

編輯mysql配置文件;

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


[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=138                //id只要和主端不一樣便可

重啓mysql服務;並利用scp命令,複製ying01上備份文件到ying02上/tmp下;

[root@ying02 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@ying02 ~]# ls /data/mysql/
auto.cnf  ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema  test  ying02.err  ying02.pid
[root@ying02 ~]# scp 192.168.112.136:/tmp/my2.sql /tmp/
my2.sql                                                                                                                    100%  641KB  18.6MB/s   00:00    
[root@ying02 ~]# scp 192.168.112.136:/tmp/zrlog.sql /tmp/
zrlog.sql                                                                                                                  100%   10KB   2.1MB/s   00:00    
[root@ying02 ~]# ls /tmp/
1212.txt    systemd-private-a229089a97fe4894a0c6a4e777a7688a-chronyd.service-Jlqqo0   zrlog.sql
my2.sql     systemd-private-a229089a97fe4894a0c6a4e777a7688a-vgauthd.service-lVk5CW
mysql.sock  systemd-private-a229089a97fe4894a0c6a4e777a7688a-vmtoolsd.service-NuLEq4

因爲ying02沒有給mysql命令指定環境變量;所以此時須要設置別名,方便使用;

[root@ying02 ~]# mysql -uroot  //沒有設置環境變量
-bash: mysql: 未找到命令
[root@ying02 ~]# alias 'mysql=/usr/local/mysql/bin/mysql'   //別名
[root@ying02 ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'  //別名
[root@ying02 ~]# mysql -uroot                                 //如今可使用
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
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>

建立與ying01上相同的數據庫,並把/tmp下相應的備份數據庫,拷貝到剛建立的數據庫,須要一一對應;

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

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

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

mysql> quit
Bye
[root@ying02 ~]# mysql -uroot yingying < /tmp/zrlog.sql 
[root@ying02 ~]# mysql -uroot zrlog < /tmp/zrlog.sql 
[root@ying02 ~]# mysql -uroot mysql < /tmp/my2.sql 
ERROR 1556 (HY000) at line 180: You can't use locks with log tables.
[root@ying02 ~]# mysql -uroot mysql2 < /tmp/my2.sql 
[root@ying02 ~]# ls /data/mysql/
auto.cnf  ibdata1  ib_logfile0  ib_logfile1  mysql  mysql2  performance_schema  test  ying02.err  ying02.pid  yingying  zrlog
[root@ying02 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
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>

若slave io及sql線程已經啓動,須要先 stop slave

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

change master命令設置slave從機與master主機進行通訊;

mysql> change master to master_host='192.168.112.136',master_user='repl',master_password='yinglinux111',master_log_file='ying01.000001',master_log_pos=11211; 
Query OK, 0 rows affected, 2 warnings (0.03 sec)

語句釋義:

  • master_host 與 master_port:分別表明master主機名(或IP地址)及mysql實例端口號。端口默認爲3306,則能夠省略;
  • master_user 與 master_password:鏈接到master主機複製帳戶所對應的用戶名及密碼。
  • master_log_file 與 master_log_pos:肯定slave的io線程下次開始執行時從master開始讀取的位置座標,若是兩者都沒有指定,slave使用上次slave sql線程保存的位置。

注意:change master to能夠不帶參數,如:只改變了用於複製的用戶密碼,那麼change master to只需針對master_password選項做出修改便可。

開啓從機上slave io及sql線程;

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

mysql> show slave status\G

             Slave_IO_Running: Connecting         //有問題
            Slave_SQL_Running: Yes

出現 Slave_IO_Running: Connecting 問題

有問題 ,先中止slave;

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

在排查用戶名,密碼等配置信息後,發現ying01端的防火牆沒有關閉;

[root@ying01 mysql]# systemctl stop firewalld
[root@ying01 mysql]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

回到從端(ying02),從新開啓start slave

mysql> change master to master_host='192.168.112.136', master_user='repl', master_password='yinglinux111', master_log_file='ying01.000001', master_log_pos=112211;
Query OK, 0 rows affected, 2 warnings (0.00 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.112.136
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: ying01.000001
          Read_Master_Log_Pos: 11211
               Relay_Log_File: ying02-relay-bin.000002
                Relay_Log_Pos: 280
        Relay_Master_Log_File: ying01.000001
             Slave_IO_Running: Yes                    //配置成功
            Slave_SQL_Running: Yes                    //配置成功

配置成功後,而後到ying01上,解鎖數據表;

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

mysql>

5、測試主從同步

數據庫及表同步也能夠指定,主從上命令不一樣:

  • 主服務器

    • binglog-do-db=1,2 //僅同步指定的庫1和2
    • bing-ignore-db=3 //忽略指定的庫3
  • 從服務器

    • replicate-do-db=
    • replicate-ignore-db=
    • replicate-do-table
    • replicate-ignore-table=
    • replicate-wild-do-table= // 經常使用
    • replicate-wild-ignore-table= // 經常使用

如今分別在ying01和ying02機器上進行主從測試:

在主(ying01)上,進入yingying數據庫,查看其數據表狀況;

mysql> 
mysql> use yingying
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;          //常看當前數據庫的表
+--------------------+
| Tables_in_yingying |
+--------------------+
| comment            |
| link               |
| log                |
| lognav             |
| plugin             |
| tag                |
| type               |
| user               |
| website            |
+--------------------+
9 rows in set (0.00 sec)

mysql> select count(*) from website;  //查看websites表的行數
+----------+
| count(*) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)

mysql> truncate table website;        //清空ebsites表內容
Query OK, 0 rows affected (0.11 sec) 

mysql> select count(*) from website;  //此時此表內容爲0
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> drop table website;          //刪除 website表
Query OK, 0 rows affected (0.05 sec)

mysql>

從端(ying02)測試

mysql> use yingying  //進入yingying數據庫
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select count(*) from website;    //查看 website的行數,與主端一致
+----------+
| count(*) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from website;   //在主端清空 website表的內容,此時與主端一致
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> select *  from website;      //在主端刪除 website表,此時也與主端一致,此表已經不存在
ERROR 1146 (42S02): Table 'yingying.website' doesn't exist
mysql> select count(*) from website;
ERROR 1146 (42S02): Table 'yingying.website' doesn't exist
mysql>
相關文章
相關標籤/搜索