MySQL Replication配置

1、MySQL Replication介紹

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

MySQL Replication配置

2、配置MySQL服務

配置主從須要兩臺機器,或者再搭建一個MySQL,使用3307端口或者其餘。這裏使用另外一臺機器搭建MySQL
配置MySQL可參考以前博客:
https://blog.51cto.com/3069201/2073238
主IP:192.168.242.128
從IP:192.168.242.129linux

3、配置主

一、修改配置文件

[root@zlinux ~]# vim /etc/my.cnf          //按下面修改

[mysqld]
#skip-grant
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as requiredii.
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 128               #可自定義,這裏就使用了ip末尾
log_bin=zlinux01             #指定log前綴
socket = /tmp/mysql.sock

二、查看是否生效

[root@zlinux ~]# /etc/init.d/mysqld restart                //重啓mysql
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@zlinux ~]# ls -lt /data/mysql/             // 重啓後生成兩個前綴爲zlinu01的文件,必須有這兩個文件
總用量 110756
-rw-rw----. 1 mysql mysql 50331648 4月   2 19:47 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 4月   2 19:47 ibdata1
-rw-rw----. 1 mysql mysql   128079 4月   2 19:47 zlinux.err
-rw-rw----. 1 mysql mysql        5 4月   2 19:47 zlinux.pid
-rw-rw----. 1 mysql mysql      120 4月   2 19:47 zlinux01.000001
-rw-rw----. 1 mysql mysql       18 4月   2 19:47 zlinux01.index
drwx------. 2 mysql mysql     4096 3月  30 17:25 zrlog
drwx------. 2 mysql mysql       96 3月  23 20:54 db1
drwx------. 2 mysql mysql     4096 3月  23 20:49 db2
drwx------. 2 mysql mysql     4096 3月  15 21:09 mysql
drwx------. 2 mysql mysql     4096 3月  15 21:09 performance_schema
-rw-rw----. 1 mysql mysql     1802 3月  15 21:03 localhost.localdomain.err
-rw-rw----. 1 mysql mysql       56 3月  15 21:03 auto.cnf
-rw-rw----. 1 mysql mysql 50331648 3月  15 21:03 ib_logfile1
drwx------. 2 mysql mysql        6 3月  15 21:03 test

三、創建新數據庫

[root@zlinux ~]# mysqldump -uroot -pzlinux123456 mysql > /tmp/mysql.sql                 //備份一個數據庫
Warning: Using a password on the command line interface can be insecure.
[root@zlinux ~]# mysql -uroot -pzlinux123456 -e "create database rpeltest"                 //創建新數據庫
Warning: Using a password on the command line interface can be insecure.
[root@zlinux ~]# mysql -uroot -pzlinux123456 rpeltest < /tmp/mysql.sql                        //將備份的數據庫導入新數據庫
Warning: Using a password on the command line interface can be insecure.

四、創建用戶

[root@zlinux ~]# mysql -uroot -pzlinux123456
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> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| mysql              |
| performance_schema |
| rpeltest           |
| test               |
| zrlog              |
+--------------------+
8 rows in set (0.01 sec)

mysql> grant replication slave on *.* to 'repl'@'192.168.242.129' identified by 'zlinux123456';            #建立用戶(IP爲「從」的IP)
Query OK, 0 rows affected (0.00 sec)

mysql> flush tables with read lock;              #鎖定數據表(目的是暫時使其不能繼續寫,保持現有狀態用於同步)
Query OK, 0 rows affected (0.01 sec)

mysql> show master status;                        #記住file和position(設置主從同步時會使用)
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| zlinux01.000003 |   120       |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4、配置從

一、修改配置文件

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

basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 129
log_bin=zlinux02
socket = /tmp/mysql.sock

[root@zlinux02 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

二、同步數據

[root@zlinux02 ~]# scp 192.168.242.128:/tmp/*.sql /tmp/                     //遠程複製主上的備份數據庫到從上
The authenticity of host '192.168.242.128 (192.168.242.128)' can't be established.
ECDSA key fingerprint is 22:fb:63:5d:8c:78:4e:74:99:f7:b1:b3:a3:70:8d:d3.
Are you sure you want to continue connecting (yes/no)? yes   
Warning: Permanently added '192.168.242.128' (ECDSA) to the list of known hosts.
root@192.168.242.128's password: 
mysql.sql                                                             100%  648KB 647.7KB/s   00:00    
[root@zlinux02 ~]# ls /tmp/
mysql.sock  mysql.sql  systemd-private-54b5c27d65e84794bceab836e24705c4-vmtoolsd.service-90ptAK
[root@zlinux02 ~]# mysql -uroot -pzlinux123456 -e "create database rpeltest"            //在從上面建立和主同樣的數據庫
Warning: Using a password on the command line interface can be insecure.
[root@zlinux02 ~]# mysql -uroot -pzlinux123456 rpeltest < /tmp/mysql.sql              //將備份數據庫導入從的數據庫
Warning: Using a password on the command line interface can be insecure.
#該過程要保證主從數據庫內容的一致

三、實現主從同步(在從上)

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

mysql> change master to master_host='192.168.242.128',master_user='repl',master_password='zlinux123456',master_log_file='zlinux01.000003',master_log_pos=120;   //IP爲主的IP
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> show slave status\G               //如下兩個YES說明配置成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

而後在主上解鎖表:sql

mysql> unlock tables;

5、測試主從

[root@zlinux ~]# mysql -uroot -pzlinux123456          //主上
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> use rpeltest;
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_rpeltest        |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

mysql> create table testuser(`id` int(4));                 //主上建立testuser表
Query OK, 0 rows affected (0.04 sec)

mysql> show tables;
+---------------------------+
| Tables_in_rpeltest        |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| testuser                  |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
29 rows in set (0.00 sec)

在從上查看:數據庫

mysql> show tables;
+---------------------------+
| Tables_in_rpeltest        |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
28 rows in set (0.00 sec)

mysql> show tables;               //多出一個表
+---------------------------+
| Tables_in_rpeltest        |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| testuser                  |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
29 rows in set (0.00 sec)
相關文章
相關標籤/搜索