MySQL Replication配置

[toc]java

MySQL Replication配置

1、MySQL主從介紹

MySQL主從又叫作Replication、AB複製。簡單講就是A和B兩臺機器作主從後,在A上寫數據,另一臺B也會跟着寫數據,二者數據實時同步;mysql

MySQL主從是基於binlog的,主上須開啓binlog才能進行主從;linux

主從過程大體有3個步驟:nginx

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

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

3)從根據relaylog裏面的sql語句按順序執行 主上有一個logvim

dump線程,用來和從的I/O線程傳遞binlong;bash

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

原理圖

mark

2、mysql編譯安裝:

cd /usr/local/src

wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz 

tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz

mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql

cd /usr/local/mysql

useradd mysql

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

運行出現兩個OK後

cp support-files/my-default.cnf  /etc/my.cnf


vim /etc/my.cnf
 basedir =/usr/local/mysql
 datadir =/data/mysql
 port =3306
 server_id =128
 socket =/tmp/mysql.sock
 log_bin=xavilinux01
# Remove leading # to set options mainly useful for reporting servers.
:wq 

cp support-files/mysql.server /etc/init.d/mysqld

正常操做是沒有問題,可是也發現了錯誤異步

[root@xavilinux mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
-bash: ./scripts/mysql_install_db: No such file or directory

這個問題處理了好久,沒有思路,左後想起查看下當前的mysql路徑下有哪些文件,才找到錯誤的地方是裏面的srcipts目錄,是本身建立的,裏面是空的

[root@xavilinux mysql]# ls
scripts
[root@xavilinux mysql]# cd /usr/local/src
[root@xavilinux src]# ls
mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz  nginx-1.12.1
[root@xavilinux src]# tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
tar (child): mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
[root@xavilinux src]# cd /usr/local/mysql
[root@xavilinux mysql]# ls
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files

再次報錯:

[root@xavilinux 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

緣由:缺乏libaio庫文件

解決方法:yum install libaio* -y

在兩臺服務器上完成了mysql的安裝和配置

MySQL支持單向、異步複製,複製過程當中一個服務器充當主服務器,而一個或多個其它服務器充當從服務器。主服務器將更新寫入二進制日誌文件,並維護日誌文件的一個索引以跟蹤日誌循環。當一個從服務器鏈接到主服務器時,它通知主服務器從服務器在日誌中讀取的最後一次成功更新的位置。從服務器接收從那時起發生的任何更新,而後封鎖並等待主服務器通知下一次更新。

爲何使用主從複製?

一、主服務器/從服務器設置增長了健壯性。主服務器出現問題時,你能夠切換到從服務器做爲備份。

二、經過在主服務器和從服務器之間切分處理客戶查詢的負荷,能夠獲得更好的客戶響應時間。可是不要同時在主從服務器上進行更新,這樣可能引發衝突。

三、使用複製的另外一個好處是可使用一個從服務器執行備份,而不會干擾主服務器。在備份過程當中主服務器能夠繼續處理更新。

MySQL使用3個線程來執行復制功能(其中1個在主服務器上,另兩個在從服務器上。當發出START SLAVE時,從服務器建立一個I/O線程,以鏈接主服務器並讓主服務器發送二進制日誌。主服務器建立一個線程將二進制日誌中的內容發送到從服務器。從服務器I/O線程讀取主服務器Binlog Dump線程發送的內容並將該數據拷貝到從服務器數據目錄中的本地文件中,即中繼日誌。第3個線程是SQL線程,從服務器使用此線程讀取中繼日誌並執行日誌中包含的更新。SHOW PROCESSLIST語句能夠查詢在主服務器上和從服務器上發生的關於複製的信息。

默認中繼日誌使用host_name-relay-bin.nnnnnn形式的文件名,其中host_name是從服務器主機名,nnnnnn是序列號。用連續序列號來建立連續中繼日誌文件,從000001開始。從服務器跟蹤中繼日誌索引文件來識別目前正使用的中繼日誌。默認中繼日誌索引文件名爲host_name-relay-bin.index。在默認狀況,這些文件在從服務器的數據目錄中被建立。中繼日誌與二進制日誌的格式相同,而且能夠用mysqlbinlog讀取。當SQL線程執行完中繼日誌中的全部事件後,中繼日誌將會被自動刪除。

從服務器在數據目錄中另外建立兩個狀態文件--master.info和relay-log.info。狀態文件保存在硬盤上,從服務器關閉時不會丟失。下次從服務器啓動時,讀取這些文件以肯定它已經從主服務器讀取了多少二進制日誌,以及處理本身的中繼日誌的程度。

vim /etc/my.cnf

mark

vim /etc/init.d/mysqld

mark

[root@xavilinux ~]# /etc/init.d/mysqld start
/etc/init.d/mysqld: line 46: /usr/local/mysql: Is a directory
/etc/init.d/mysqld: line 47: /data/mysql: Is a directory
Starting MySQL.Logging to '/data/mysql/xavilinux.err'.
                                                           [  OK  ]

出現mysql沒法啓動,報錯的狀況

1.有可能開了java別的服務佔用端口,lsof -i :80查看端口狀況,將佔用的服務關閉

[root@xavi ~]# lsof -i :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

2. 從錯誤日誌入手看下報錯緣由

[root@xavi ~]# /etc/init.d/mysqld start
Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/xavi.pid).
[root@xavi ~]# ls -l /data/mysql
總用量 110720
-rw-rw---- 1 mysql mysql       56 3月  13 22:17 auto.cnf
drwx------ 2 mysql mysql       20 3月  23 22:17 db2
-rw-rw---- 1 mysql mysql 12582912 4月   6 15:53 ibdata1
-rw-rw---- 1 mysql mysql 50331648 4月   6 15:53 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 3月  13 22:11 ib_logfile1
drwx------ 2 mysql mysql     4096 3月  13 22:11 mysql
drwx------ 2 mysql mysql     4096 3月  24 09:55 mysql2
drwx------ 2 mysql mysql     4096 3月  13 22:11 performance_schema
drwx------ 2 mysql mysql        6 3月  13 22:11 test
-rw-rw---- 1 mysql mysql   114094 4月   6 15:53 xavi.err
drwx------ 2 mysql mysql      324 3月  31 15:49 zrlog
  • 查看錯誤日誌
[root@xavi mysql]# tail xavi.err
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'CSV'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'MEMORY'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'MyISAM'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'MRG_MYISAM'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'sha256_password'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'mysql_old_password'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'mysql_native_password'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'binlog'
2018-04-06 16:08:43 7318 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete
[root@xavi mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

3、配置主機

主master:192.168.72.130 從slave:192.168.72.133

3.1 修改my.cnf,增長server-id=130和log_bin=xavilinux1

mark

[mysqld]
datadir=/data/mysql
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
symbolic-links=0

server_id =130
log_bin=xavilinux

3.2 修改完配置文件後,啓動或者重啓mysqld服務

[root@xavi ~]# vim /etc/my.cnf
[root@xavi ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
  • 生成了兩個重要文件,前綴名爲xavilinux1,這是實現主從的根本。000001是二進制日誌文件,index是索引。 mark

3.3 備份以前blog的全部的數據(測試用)

[root@xavi mysql]# mysqldump -uroot -pxavilinux blog > /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@xavi mysql]# du -sh /tmp/blog.sql
4.0K	/tmp/blog.sql

3.4 建立一個新庫把以前的備份數據恢復

[root@xavi mysql]# mysql -uroot -pxavilinux -e "create database xavi"
Warning: Using a password on the command line interface can be insecure.
[root@xavi mysql]# mysql -uroot -pxavilinux xavi < /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.

3.5 建立用做同步數據的用戶

[root@xavi mysql]# mysql -uroot -pxavilinux

mysql> grant replication slave on *.* to 'repl'@'192.168.72.133' identified by 'xavilinux111'; //密碼是主機登入密碼??
Query OK, 0 rows affected (0.00 sec)

把數據表鎖住,不在往裏面寫數據

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 |
+-------------------+----------+--------------+------------------+-------------------+
| xavilinux1.000001 |      732 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

3.6 備份下/data/mysql下的部分目錄,但mysql不須要備份,沒法主從同步,裏面包含了帳戶密碼等信息

cd /data/mysql
[root@xavi mysql]# ls
auto.cnf  db2      ib_logfile0  mysql   performance_schema  xavi      xavilinux1.000001  xavi.pid
blog      ibdata1  ib_logfile1  mysql2  test                xavi.err  xavilinux1.index   zrlog
[root@xavi mysql]# mysqldump -uroot -pxavilinux mysql2 > /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.
[root@xavi mysql]# mysqldump -uroot -pxavilinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.

4、配置從機

4.1 編輯配置文件,並重啓MySQL服務

mark

[root@xavilinux mysql]# vim /etc/my.cnf
 basedir =/usr/local/mysql
 datadir =/data/mysql
# port =3306
# server_id = .....
 socket =/tmp/mysql.sock
 server_id=133
 
 [root@xavi-002 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@xavi-002 mysql]# vim /etc/init.d/mysqld

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

[root@xavilinux mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                            [  OK  ]

4.2 查看從機下有哪些內容,把主機上全部mysql文件遠程複製過來scp

[root@xavi-002 ~]# ls /data/mysql
auto.cnf  ib_logfile0  mysql               test          xavi-002.pid
ibdata1   ib_logfile1  performance_schema  xavi-002.err
[root@xavi-002 ~]# scp 192.168.72.130:/tmp/*.sql /tmp/
root@192.168.72.130's password: 
Permission denied, please try again.
root@192.168.72.130's password: 
blog.sql                                      100% 1258     1.2KB/s   00:00    
my2.sql                                       100%  638KB 638.4KB/s   00:00    
zrlog.sql                                     100% 9880     9.7KB/s   00:00

4.3 把兩個命令mysql和mysqldump用alias設置

[root@xavi-002 ~]# alias 'mysql=/usr/local/mysql/bin/mysql'
[root@xavi-002 ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'

4.4 進入mysql,不設密碼,創建4個數據庫

[root@xavi-002 mysql]# mysql -uroot
mysql> create database xavi;
Query OK, 1 row affected (0.00 sec)

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

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

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

4.5 將新建的數據庫中恢復主機備份過來的數據

[root@xavi-002 ~]# mysql -uroot blog < /tmp/blog.sql
[root@xavi-002 ~]# mysql -uroot zrlog < /tmp/zrlog.sql

[root@xavi-002 ~]# mysql -uroot mysql2 < /tmp/my2.sql
[root@xavi-002 ~]# ls /data/mysql
auto.cnf  ib_logfile0  mysql2              xavi          zrlog
blog      ib_logfile1  performance_schema  xavi-002.err
ibdata1   mysql        test                xavi-002.pid

4.6 複製數據後,在slave上配置

[root@xavi-002 ~]# mysql -uroot
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.72.130',master_user='repl',master_password='xavilinux',master_log_file='xavilinux1.000001',master_log_pos=424;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

4.7 啓動 slave

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

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.72.130
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: xavilinux1.000001
          Read_Master_Log_Pos: 424
               Relay_Log_File: xavi-002-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: xavilinux1.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes

報錯

Last_IO_Error: error connecting to master 'repl@192.168.72.130:3306' - retry-time: 60  retries: 1

這是在3.5 小節測試時寫錯了從機的IP地址,

[root@xavi mysql]# mysql -uroot -pxavilinux
mysql>  grant replication slave on *.* to 'repl'@'192.168.72.133' identified by 'xavilinux';
Query OK, 0 rows affected (0.00 sec)

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 |
+------------------+----------+--------------+------------------+-------------------+
| xavilinux.000002 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

[root@xavi mysql]# ls
auto.cnf  db2      ib_logfile0  mysql   performance_schema  xavi      xavilinux.000002  xavi.pid
blog      ibdata1  ib_logfile1  mysql2  test                xavi.err  xavilinux.index   zrlog

報錯:

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

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.72.130
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: xavilinux1.000004
          Read_Master_Log_Pos: 331
               Relay_Log_File: xavi-002-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: xavilinux1.000004
             Slave_IO_Running: Connecting
            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: 331
              Relay_Log_Space: 120
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1045
                Last_IO_Error: error connecting to master 'repl@192.168.72.130:3306' - retry-time: 60  retries: 1
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             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: 180408 23:22:46
     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)

ERROR: 
No query specified

mysql> perror 1045;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'perror 1045' at line 1

查看主機受權

mysql> select user,host,password from mysql.user;
+-------+----------------+-------------------------------------------+
| user  | host           | password                                  |
+-------+----------------+-------------------------------------------+
| root  | localhost      | *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |
| root  | xavi           | *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |
| root  | 127.0.0.1      | *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |
| root  | ::1            | *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |
|       | localhost      |                                           |
|       | xavi           |                                           |
| user1 | 127.0.0.1      | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| user1 | localhost      | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| user2 | 192.168.133.1  | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |
| user2 | 192.168.133.2  | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |
| zrlog | 127.0.0.1      | *D66E108B48328249BB2779FCB0F0B03E3EDF92C1 |
| blog  | 127.0.0.1      | *D66E108B48328249BB2779FCB0F0B03E3EDF92C1 |
| repl  | 192.168.72.133 | *BF29B1459C13E117044049733F4E17D0D14AC7DB |
+-------+----------------+-------------------------------------------+
13 rows in set (0.00 sec)

mysql> show grants for 'repl'@'192.168.72.133';
+------------------------------------------------------------------------------------------------------------------------------+
| Grants for repl@192.168.72.133                                                                                               |
+------------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.72.133' IDENTIFIED BY PASSWORD '*BF29B1459C13E117044049733F4E17D0D14AC7DB' |
+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • 問題一直未找到,有多是mysql版本5.5和5.6的區別

一、在master mysql建立同步用戶

grant emuser slave,file on *.* to emuser@192.168.5.61 identified by 123456;
flush privileges;
修改master的my.cnf的配置
wait_timeout = 30000000
interactive-timeout = 30000000
binlog-do-db=cdn_view  #設置二進制日誌記錄的庫
log-bin=mysql-bin         #打開mysql二進制日誌
binlog-ignore-db=mysql   ##設置二進制日誌不記錄的庫
server-id = 12760         #設置mysql_id,主從不能相同
long_query_time = 5
expire_logs_days= 3
二、修改slave的my.cnf的配置
wait_timeout = 30000000
interactive-timeout = 30000000
log-bin=mysql-bin
server-id = 12761
replicate-do-db=cdn_view   #設置同步的庫
replicate-ignore-db=mysql  #設置不一樣步的庫
log-slave-updates         #同步後記錄二進制日誌
slave-skip-errors=all  
slave-net-timeout=60
sync_binlog=1
binlog_format=mixed

############################################################################
分別重啓主從mysqld服務,登陸主mysql,在主上執行flush tables with read lock;
而後將cdn_view數據庫的數據copy到從上,並記錄下主上show master statusG的結果:
mysql> show master statusG;
*************************** 1. row ***************************
            File: mysql-bin.000009
        Position: 341
    Binlog_Do_DB: cdn_view
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

而後執行unlock tables;
登陸從mysql,在從上執行:
stop slave;
change master to master_host=192.168.5.60,master_user=emuser,master_password=123456, master_log_file=mysql-bin.000009, master_log_pos=341;
start slave;
show slave statusG;

若是出現以下信息說明主從同步成功。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

5、測試主從

不成功

相關文章
相關標籤/搜索