[toc]java
MySQL主從又叫作Replication、AB複製。簡單講就是A和B兩臺機器作主從後,在A上寫數據,另一臺B也會跟着寫數據,二者數據實時同步;mysql
MySQL主從是基於binlog的,主上須開啓binlog才能進行主從;linux
主從過程大體有3個步驟:nginx
1)主將更改操做記錄到binlog裏sql
2)從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裏數據庫
3)從根據relaylog裏面的sql語句按順序執行
主上有一個logvimdump線程,用來和從的I/O線程傳遞binlong;bash
從上有兩個線程,其中I/O線程用來同步主的binlog並生成relaylog,另一個sql線程用來把relaylog裏面的sql語句落地。服務器
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
正常操做是沒有問題,可是也發現了錯誤socket
[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
[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 ]
[root@xavi ~]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
[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、配置主機 ### 3.1 修改my.cnf,增長server-id=130和log_bin=xavilinux1 
[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是索引。  ### 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服務 
[root@xavilinux mysql]# vim /etc/my.cnf
basedir =/usr/local/mysql
datadir =/data/mysql
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的區別 ## 5、測試主從 未完待續