MySQL Replication配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL Replication配置相关的知识,希望对你有一定的参考价值。

一、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,使用3307端口或者其他。这里使用另一台机器搭建MySQL
配置MySQL可参考之前博客:
http://blog.51cto.com/3069201/2073238
主IP:192.168.242.128
从IP:192.168.242.129

三、配置主

1、修改配置文件

[[email protected] ~]# 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

2、查看是否生效

[[email protected] ~]# /etc/init.d/mysqld restart                //重启mysql
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[[email protected] ~]# 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

3、建立新数据库

[[email protected] ~]# mysqldump -uroot -pzlinux123456 mysql > /tmp/mysql.sql                 //备份一个数据库
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot -pzlinux123456 -e "create database rpeltest"                 //建立新数据库
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot -pzlinux123456 rpeltest < /tmp/mysql.sql                        //将备份的数据库导入新数据库
Warning: Using a password on the command line interface can be insecure.

4、建立用户

[[email protected] ~]# 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)

四、配置从

1、修改配置文件

[[email protected] ~]# vim /etc/my.cnf

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

[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

2、同步数据

[[email protected] ~]# 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.
[email protected]‘s password: 
mysql.sql                                                             100%  648KB 647.7KB/s   00:00    
[[email protected] ~]# ls /tmp/
mysql.sock  mysql.sql  systemd-private-54b5c27d65e84794bceab836e24705c4-vmtoolsd.service-90ptAK
[[email protected] ~]# mysql -uroot -pzlinux123456 -e "create database rpeltest"            //在从上面创建和主一样的数据库
Warning: Using a password on the command line interface can be insecure.
[[email protected] ~]# mysql -uroot -pzlinux123456 rpeltest < /tmp/mysql.sql              //将备份数据库导入从的数据库
Warning: Using a password on the command line interface can be insecure.
#该过程要保证主从数据库内容的一致

3、实现主从同步(在从上)

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

然后在主上解锁表:

mysql> unlock tables;

五、测试主从

[[email protected] ~]# 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)

以上是关于MySQL Replication配置的主要内容,如果未能解决你的问题,请参考以下文章

MySQL Replication配置

如何配制MySql的Replication

第25章 MySQL replication(主从)配置及加入nagios监控

转载MySQL Replication 环境安装与配置

mysql5.6 semi replication 半同步复制配置

MySQL Replication的主从同步的常用2种配置方式