mysql主從目錄
1.mysql主從複製數據原理
- 主庫將所有的寫操作記錄在bin-log日誌中,並生成一個bin dump線程將bin-log日誌傳輸給從庫的I/O進程。
- 從庫生成兩個線程,一個I/O,一個SQL線程
- I/O線程器請求主庫的bin-log,並將得到的bin-log日誌寫入relaylog(中繼日誌)文件中
- SQL線程,會讀取relay log文件中的日誌,並解析成具體操作,來實現主從的操作一致,達到最終數據一致的目的
2. mysql安裝
分別在主從兩臺服務器上安裝mysql服務
3.mysql主從配置
3.1 確保從數據庫與主數據庫裏的數據一樣
爲確保從數據庫與主數據庫裏的數據一樣,先全備主數據庫並還原到從數據庫中
//先查看主庫有哪些庫
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
| teacher |
+--------------------+
//再查看從庫有哪些庫
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
//全備主庫
//全備主庫時需要另開一個終端,給數據庫加上讀鎖,避免在備份期間有其他人在寫入導致數據不一致
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
//此鎖表的終端必須在備份完成以後才能退出
//備份主庫並將備份文件傳送到從庫
[root@localhost ~]# mysqldump -uroot -pwangqing123! --all-databases > /opt/all-201808191200.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ls /opt/
all-201808191200.sql
[root@localhost ~]# scp /opt/all-201808191200.sql [email protected]:/opt/
[email protected]'s password:
all-201808191200.sql 100% 786KB 10.6MB/s 00:00
//解除主庫的鎖表狀態,直接退出交互式界面即可
mysql> quit
Bye
//在從庫上恢復主庫的備份並查看從庫有哪些庫,確保與主庫一致
[root@localhost ~]# mysql -uroot -pwangqing123! < /opt/all-201808191200.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
| teacher |
+--------------------+
3.2 在主數據庫裏創建一個同步賬號授權給從數據庫使用
mysql> CREATE USER 'repl'@'172.16.12.129' IDENTIFIED BY 'repl123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.16.12.129';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.3 配置主數據庫
[root@localhost ~]# vim /etc/my.cnf
//在[mysqld]這段的後面加上如下內容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin //啓用binlog日誌
server-id=1 //數據庫服務器唯一標識符,主庫的server-id值必須比從庫的大
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
//重啓mysql服務
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//查看主庫的狀態
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
3.4 配置從數據庫
[root@localhost ~]# vim /etc/my.cnf
//添加如下內容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=2 //設置從庫的唯一標識符,從庫的server-id值必須小於主庫的該值
relay-log=mysql-relay-bin //啓用中繼日誌relay-log
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
//重啓從庫的mysql服務
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//配置並啓動主從複製
mysql> CHANGE MASTER TO
-> MASTER_HOST='172.16.12.128',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='repl123',
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.33 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: 172.16.12.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //此處必須爲Yes
Slave_SQL_Running: Yes //此處必須爲Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
3.5 測試驗證
在主服務器的student庫的bj2表中插入數據:
mysql> select * from bj2;
Empty set (0.00 sec)
mysql> insert into bj2 values (1,'sean',20),(2,'tom',23),(3,'jerry',30);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from bj2;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | sean | 20 |
| 2 | tom | 23 |
| 3 | jerry | 30 |
+----+-------+------+
3 rows in set (0.00 sec)
在從數據庫中查看數據是否同步:
mysql> use student;
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 * from bj2;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | sean | 20 |
| 2 | tom | 23 |
| 3 | jerry | 30 |
+----+-------+------+
3 rows in set (0.00 sec)