27 mysql 3(Mysql 主從配置)
17.1 MySQL主從介紹
Replication 實時同步
1. MySQL主從又叫做Replication、AB複製。簡單講就是A和B兩臺機器做主從後,在A上寫資料,另外一臺B也會跟著寫資料,兩者資料實時同步的
2. MySQL主從是基於binlog的,主上須開啟binlog才能進行主從。
3. 主從過程大致有3個步驟
1)主將更改操作記錄到binlog裡
2)從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裡(binlog為2進位制,cat無法檢視)
3)從根據relaylog裡面的sql語句按順序執行
4. 主上有一個log dump執行緒,用來和從的I/O執行緒傳遞binlog
5. 從上有兩個執行緒,其中I/O執行緒用來同步主的binlog並生成relaylog,另外一個SQL執行緒用來把relaylog裡面的sql語句落地
17.2 準備工作
1. 在兩個centos系統上安裝mysql,安裝方法見 :
2. hao1機器和hao2機器都啟動mysql :
3. 兩個centos系統:設定hao1為主 hao2為從 :
hao1檢視是否啟動mysql :[[email protected] ~]# ps aux |grep mysql
hao2檢視是否啟動mysql :[[email protected] ~]# ps aux |grep mysql
17.3 配置主
1. 編輯/etc/my.cnf配置檔案 :[[email protected] ~]# vim /etc/my.cnf # 新增內容 :
server-id=130 #新增這行,id=128是獲取到的ip地址
log_bin=pyzc001 #伺服器設定的名稱
[mysqld]
server-id=130
log_bin=pyzc001
datadir=/data/mysql
socket=/tmp/mysql.sock
symbolic-links=0
2. 重啟mysql :[[email protected] ~]# /etc/init.d/mysqld restart
3. 檢視/data/mysql/目錄下,生成以(log_bin)pyzc001開頭的檔案 :[[email protected] ~]# ls /data/mysql/pyzc001.*
4. 進入 /data/mysql/目錄 :[[email protected] ~]# cd /data/mysql/
5. 建立一個新的庫,庫名為pyzctest001:
[[email protected] mysql]# mysql -uroot -pmima1 -e "CREATE DATABASE pyzctest001;" #把ceshiku庫備份並恢復成haozc庫,作為測試資料
6. 備份mysql資料庫,做測試 :[[email protected] mysql]# mysqldump -uroot -pmima1 pyzctest001 > /tmp/pyzctest001.sql
7. 把備份的ceshiku.sql庫檔案,恢復到haozc庫 :[[email protected] mysql]# mysql -uroot -phaomima haozc < /tmp/ceshiku.sql
或建立新庫新表如下:
[[email protected] mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.....
mysql> set password=PASSWORD('mima1');
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE DATABASE pyzctest001;
Query OK, 1 row affected (0.01 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pyzctest001 |
| test |
+--------------------+
5 rows in set (0.02 sec)
mysql> CREATE DATABASE IF NOT EXISTS my_db default charset utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.01 sec)
mysql> CREATE TABLE 主從複製01 (姓名 VARCHAR(20), 屬性 CHAR(1));
Query OK, 0 rows affected (0.07 sec)
mysql> INSERT INTO 主從複製01 VALUES ("panyu","M");
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO 主從複製01 VALUES ("lingling","W");
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO 主從複製01 VALUES ("panhunuo","c");
Query OK, 1 row affected (0.01 sec)
mysql> SHOW COLUMNS FROM 主從複製01;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| 姓名 | varchar(20) | YES | | NULL | |
| 屬性 | char(1) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
8. hao1機器主 所有mysql資料庫備份到/tmp/下 :
[[email protected] mysql]# mysqldump -uroot -pmima1 pyzctest001 > /tmp/pyzctest001.sql
9. 進入mysql(root使用者下):[[email protected] mysql]# mysql -uroot -pmima1
10. 建立repl使用者,針對ip是hao2從ip :mysql> grant replication slave on *.* to 'repl'@'192.168.106.131' identified by 'mima2';
11. 表狀態鎖上,暫時不能再寫入資料 :mysql> flush tables with read lock;
12. 檢視紅框中兩個數值,hao2機器從上需要用這兩個數值 :mysql> show master status;
mysql> grant replication slave on *.* to 'repl'@'192.168.106.131' identified by 'mima2';
Query OK, 0 rows affected (0.01 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.04 sec)
mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| pyzc001.000006 | 120 | | | |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
17.4 配置從
1. 編輯/etc/my.cnf配置檔案 :[[email protected] ~]# vim /etc/my.cnf #新增內容:
server-id=131
2. 重啟mysql :[[email protected] ~]# /etc/init.d/mysqld restart #service mysql restart
3. 用scp命令複製hao1主/tmp/下所有以.sql備份的mysql資料庫檔案,貼上到本機hao2從的/tmp/目錄下 :
[[email protected] ~]# scp 192.168.106.130:/tmp/*.sql /tmp/ # yum install -y openssh-clients 可以使用scp
[[email protected] tmp]# scp 192.168.106.130:/tmp/*.sql /tmp/
[email protected]'s password:
pyzctest001.sql 100% 1961 331.0KB/s 00:00
4. 如果執行找不到mysql -uroot命令,執行下面命令 :
[[email protected] ~]# alias 'mysql=/usr/local/mysql/bin/mysql'
[[email protected] ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
[[email protected] ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin
5. 登入mysql(登入root使用者) :[[email protected] ~]# mysql -uroot
6. hao2從上建立和hao1主一樣的mysql使用者haozc :
mysql> create database pyzctest001;
7. 把hao1機器從 同步的.sql 庫檔案,恢復到建立對應名稱庫下 :
[[email protected] mysql]# mysql -uroot pyzctest001 < /tmp/pyzctest001.sql
8. 登入mysql,執行下面命令 :
(master_password='mima2', master_log_file='pyzc001.000003',見17.3章12節檢視 )
[[email protected] ~]# mysql -uroot
mysql> use pyzctest001;
mysql> stop slave;
mysql> change master to master_host='192.168.106.130',master_user='repl', master_password='mima2', master_log_file='pyzc001.000006', master_log_pos=120;
mysql> start slave;
mysql> change master to master_host='192.168.106.130',master_user='repl',master_password='mima2',master_log_file='pyzc001.000003',master_log_pos=1822;
Query OK, 0 rows affected, 2 warnings (0.07 sec)
9. 檢視主從配置是否成功 ?mysql> show slave status\G #兩個都是Yes才成功
10. 在hao1機器主上,把之前鎖定的表解鎖(恢復寫入的操作) :[[email protected] ~]# mysql -uroot -pmima1
mysql> unlock tables;
17.5 測試主從同步
配置引數
1. 主伺服器上 :
binlog-do-db= //僅同步指定的庫(其他庫不同步)
binlog-ignore-db= //忽略指定庫(其他庫都同步)
2. 從伺服器上 :
replicate_do_db= //(不常用)
replicate_ignore_db= //(不常用)
replicate_do_table= //(不常用)
replicate_ignore_table= //(不常用)
replicate_wild_do_table= //如aming.%, (支援萬用字元%)
replicate_wild_ignore_table=
測試主從:
1. hao1機器主上操作 :[[email protected] ~]# mysql -uroot -pmima1
切換資料庫 :mysql> use pyzctest001;
檢視資料庫下所有表 :mysql> show tables;
2. hao2機器從上操作 :
[[email protected] ~]# mysql -uroot
切換資料庫 :mysql> use pyzctest001;
檢視資料庫下所有表 :mysql> show tables;
3. hao1機器主 刪除表(刪除表結構,慎重使用):mysql> drop table biao1;
4. hao2機器從 檢視所有表,是否還有主上刪除的表 ?mysql> show tables;
注意:到主從不能正常同步,提示uuid相同的錯誤。這是因為克隆機器導致。https://www.2cto.com/database/201412/364479.html
主
mysql> use pyzctest001;
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_pyzctest001 |
+-----------------------+
| 主從複製01 |
+-----------------------+
1 row in set (0.04 sec)
mysql> describe 主從複製01;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| 姓名 | varchar(20) | YES | | NULL | |
| 屬性 | char(1) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.06 sec)
mysql> insert into 主從複製01 (姓名,屬性)
-> values ('test','t');
Query OK, 1 row affected (0.19 sec)
mysql> select * from 主從複製01;
+----------+--------+
| 姓名 | 屬性 |
+----------+--------+
| panyu | M |
| lingling | W |
| panhunuo | c |
| test | t |
| test01 | t |
| test02 | t |
| test04 | t |
| test03 | q |
+----------+--------+
8 rows in set (0.01 sec)
mysql> insert into 主從複製01 (姓名,屬性) values ('test55','i');
Query OK, 1 row affected (0.03 sec)
從
mysql> use pyzctest001;
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 主從複製01;
+----------+--------+
| 姓名 | 屬性 |
+----------+--------+
| panyu | M |
| lingling | W |
| panhunuo | c |
| test | t |
| test01 | t |
| test02 | t |
| test04 | t |
| test03 | q |
| test55 | i |
+----------+--------+
9 rows in set (0.01 sec)