MySQL 5.7主从复制实战篇

               MySQL 5.7主从复制实战篇

                                     作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

一.安装MySQL数据库并启动

1>.在MySQL官方下载相应的安装包(https://dev.mysql.com/downloads/mysql/)

2>.解压MySQL压缩文件至安装目录

[root@node106.yinzhengjie.org.cn ~]# ll
total 629752
-rw-r--r-- 1 root root 644862820 Jun 10 15:18 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# tar -zxf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /yinzhengjie/softwares/
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ln -s /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64 /yinzhengjie/softwares/mysql
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# mkdir /yinzhengjie/softwares/mysql/data
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/mysql
lrwxrwxrwx 1 root root 58 Jun 10 16:58 /yinzhengjie/softwares/mysql -> /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/mysql/
total 36
drwxr-xr-x  2 root root   4096 Jun 10 16:57 bin
-rw-r--r--  1 7161 31415 17987 Dec 21 18:39 COPYING
drwxr-xr-x  2 root root      6 Jun 10 16:58 data
drwxr-xr-x  2 root root     55 Jun 10 16:57 docs
drwxr-xr-x  3 root root   4096 Jun 10 16:57 include
drwxr-xr-x  5 root root    230 Jun 10 16:57 lib
drwxr-xr-x  4 root root     30 Jun 10 16:57 man
-rw-r--r--  1 7161 31415  2478 Dec 21 18:39 README
drwxr-xr-x 28 root root   4096 Jun 10 16:57 share
drwxr-xr-x  2 root root     90 Jun 10 16:57 support-files
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# vi ~/.bash_profile 
[root@node106.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# tail -2 ~/.bash_profile 
PATH=$PATH:$HOME/bin:/yinzhengjie/softwares/mysql/bin/
export PATH
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# source ~/.bash_profile 
[root@node106.yinzhengjie.org.cn ~]#

3>.创建mysql用户,并将解压的MySQL程序文件授权给mysql用户。

[root@node106.yinzhengjie.org.cn ~]# useradd -s /sbin/nologin mysql
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# id mysql
uid=1001(mysql) gid=1001(mysql) groups=1001(mysql)
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# chown mysql:mysql -R /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# chown mysql:mysql -R /yinzhengjie/softwares/mysql
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll -d /yinzhengjie/softwares/mysql
lrwxrwxrwx 1 mysql mysql 58 Jun 10 16:58 /yinzhengjie/softwares/mysql -> /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll -d /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64/
drwxr-xr-x 10 mysql mysql 141 Jun 10 16:58 /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64/
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]#

4>.初始化MySQL数据库

[root@node107.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/mysql/data/
total 0
[root@node107.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data &
[1] 18800
[root@node106.yinzhengjie.org.cn ~]# 2019-06-10T09:16:38.425725Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-10T09:16:39.122750Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-06-10T09:16:39.235538Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-10T09:16:39.304637Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 74227047-8b60-11e9-8cba-000c29985293.
2019-06-10T09:16:39.306459Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-06-10T09:16:39.312634Z 1 [Note] A temporary password is generated for root@localhost: .wIyMa&xf8BL      #注意,这里标红的为MySQL服务器的临时密码,建议大家先保存一下,一会登陆数据库时要用到,如果你忘记了临时密码重复该步骤即可。

[1]+  Done                    mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/mysql/data/                        #很显然,一旦我们初始化成功后,我们就会发现该目录下是有初始化数据生成的
total 110636
-rw-r----- 1 mysql mysql       56 Jun 10 17:16 auto.cnf
-rw-r----- 1 mysql mysql      419 Jun 10 17:16 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Jun 10 17:16 ibdata1
-rw-r----- 1 mysql mysql 50331648 Jun 10 17:16 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Jun 10 17:16 ib_logfile1
drwxr-x--- 2 mysql mysql     4096 Jun 10 17:16 mysql
drwxr-x--- 2 mysql mysql     8192 Jun 10 17:16 performance_schema
drwxr-x--- 2 mysql mysql     8192 Jun 10 17:16 sys
-rw-r----- 1 mysql mysql      177 Jun 10 17:16 yinzhengjie-mysql-bin.000001
-rw-r----- 1 mysql mysql       31 Jun 10 17:16 yinzhengjie-mysql-bin.index
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 

5>.修改MySQL的配置文件

[root@node106.yinzhengjie.org.cn ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/              
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=106
[root@node106.yinzhengjie.org.cn ~]# 

配置参数说明如下:
basedir
  该参数指定了安装 MySQL 的安装路径,填写全路径可以解决相对路径所造成的问题。

datadir   该参数指定了 MySQL 的数据库文件放在什么路径下。数据库文件即我们常说的 MySQL data 文件。
log
-bin   该参数只要配置就表示开启了MySQL的bin log日志功能,注意改参数的值是我们自定义的,我们自定义的值将作为bin log的名称的前缀信息哟,我们可以使用MySQL命令"show variables like '%log_bin%';"查看咱们的配置。
server
-id   该参数可以指定数据库服务器的唯一标识。在同一个复制组下的所有实例的server_id都必须是唯一的,而且取值必须是正整数,取值范围是1~(232)−1

6>.启动MySQL服务

[root@node106.yinzhengjie.org.cn ~]# cp /yinzhengjie/softwares/mysql/support-files/mysql.server /etc/init.d/        #拷贝MySQL的启动脚本到系统自启动路径下
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# /etc/init.d/mysql.server start
Starting MySQL.Logging to '/yinzhengjie/softwares/mysql/data/node106.yinzhengjie.org.cn.err'.
 SUCCESS! 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# /etc/init.d/mysql.server status
 SUCCESS! MySQL running (18997)
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ps -ef | grep mysql
root      18861      1  0 17:20 pts/0    00:00:00 /bin/sh /yinzhengjie/softwares/mysql//bin/mysqld_safe --datadir=/yinzhengjie/softwares/mysql/data/ --pid-file=/yinzhengjie/softwares/mysql/data//node106.yinzhengjie.org.cn.pid
mysql     18997  18861  4 17:20 pts/0    00:00:00 /yinzhengjie/softwares/mysql/bin/mysqld --basedir=/yinzhengjie/softwares/mysql/ --datadir=/yinzhengjie/softwares/mysql/data --plugin-dir=/yinzhengjie/softwares/mysql//lib/plugin --user=mysql --log-error=node106.yinzhengjie.org.cn.err --pid-file=/yinzhengjie/softwares/mysql/data//node106.yinzhengjie.org.cn.pid
root      19047  18609  0 17:20 pts/0    00:00:00 grep --color=auto mysql
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 

7>.登录MySQL数据库并初始化管理员密码

[root@node106.yinzhengjie.org.cn ~]# mysql -u root -p
Enter password:                                 #注意,这里使用的密码是我们在第4步骤初始化数据生成的临时密码。
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-log

Copyright (c) 2000, 2019, 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> 
mysql> alter user user() identified by 'yinzhengjie';          #由于临时密码不方便记忆,我这里改成比较好记的字符串。
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> quit
Bye
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# mysql -u root -pyinzhengjie    #我们使用刚刚修改后的密码登陆数据库
mysql: [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.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 variables like '%server_id%';                  #查看当前服务器的server_id
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id    | 106 |
| server_id_bits | 32 |
+----------------+-------+
2 rows in set (0.01 sec)

mysql>
mysql> quit
Bye
[root@node106.yinzhengjie.org.cn ~]# 

8>.在另一外一台服务器上重复上述操作(我们实验环境用node107.yinzhengjie.org.cn作备用的MySQL数据库)

 

二. MySQL复制概述

1>.MySQL复制介绍

  (1)MySQL复制允许将主实例(master)上的数据同步到一个或多个从实例(slave)上,默认情况下复制是异步进行的,从库也不需要一直连接主库来同步数据。
  (
2)MySQL复制的数据粒度可以是主实例上所有的数据库,也可以是指定的一个或多个数据库,也可以是一个数据库里的指定的表。
  (
3)MySQL复制带来的优势在于:       扩展能力:       通过复制可以将MySQL的性分到一个或多个slave上。这要求所有的写操作和修改操作都必须在Master上完成,而读操作可以被分配到一个或多个salve上。将读写分离到不同服务执行之后,MySQL的读写性能得到提升。       数据库备份:         由于从实例时同步主实例的数据,所以可以将备份作业部署到从库。       数据分析和报表:         同样,一些数据分析和报表的实现可以在从实例执行,以减少对主库的性能影响。       容灾能力:         可以在物理距离较远的另一个数据建立slave,保证在主实例所在地区遭遇灾难时,在另一个数据中心能快速恢复。

2>.MySQL复制有两种方法

  (1)传统方式
      基于主库的bin-log将日志事件和事件位置复制到从库,从库再加以应用来达到主从同步的目的。
  
(2)Gtid方式       global transaction identitifiers 是基于事物来复制数据,因此也就不依赖日志文件,同时又能更好的保证主从库数据一致性。

3>.MySQL复制有多种类型

1)异步复制
    一个主库,一个或多个从库,数据异步同步到从库。

(2)同步复制
    在MySQL cluster中特有的复制方式。

(3)半同步复制
    在异步复制的基础上,确保任何一个主库上的事物在提交之前至少有一个从库已经收到该事物并日志记录下来。

(4)延迟复制
    在异步复制的基础上,人为设定主库和从库的数据同步延迟时间,即保证数据延迟至少是这个参数。

4>.MySQL的复制原理

 如上图所示,MySQL复制的原理大致总结如下:
    (1)在Slave 服务器上执行sart slave命令开启主从复制开关,开始进行主从复制。
    (2)此时,Slave服务器的IO线程会通过在master上已经授权的复制用户权限请求连接master服务器,并请求从执行binlog日志文件的指定位置(日志文件名和位置就是在配置主从复制服务时执行change master命令指定的)之后开始发送binlog日志内容
    (3)Master服务器接收到来自Slave服务器的IO线程的请求后,二进制转储IO线程会根据Slave服务器的IO线程请求的信息分批读取指定binlog日志文件指定位置之后的binlog日志信息,然后返回给Slave端的IO线程。返回的信息中除了binlog日志内容外,还有在master服务器端记录的新的binlog文件名称,以及在新的binlog中的下一个指定更新位置。
    (4)当Slave服务器的IO线程获取到Master服务器上IO线程发送的日志内容、日志文件及位置点后,会将binlog日志内容依次写到Slave端自身的Relay Log(即中继日志)文件(MySQL-relay-bin.xxx)的最末端,并将新的binlog文件名和位置记录到master-info文件中,以便下一次读取master端新binlog日志时能告诉Master服务器从新binlog日志的指定文件及位置开始读取新的binlog日志内容
    (5)Slave服务器端的SQL线程会实时检测本地Relay Log 中IO线程新增的日志内容,然后及时把Relay LOG 文件中的内容解析成sql语句,并在自身Slave服务器上按解析SQL语句的位置顺序执行应用这样sql语句,并在relay-log.info中记录当前应用中继日志的文件名和位置点

5>.MySQL复制有三种核心格式

复制的工作原理是数据库修改记录到bin log日志并传递到slave,然后slave在本地还原的过程。而时间记录到bin log的格式会有所不同。

基于语句的复制(statement based replication):
  基于主库将SQL语句写入到bin log中完成复制。

基于行数据的复制(row based replication):
  基于主库将每一行数据变化的信息作为时间写入到bin log中完成日志。默认就是基于行级别的复制,因为它相对语句复制逻辑更为严谨。

混合复制(mixed based replication):
  上述两者的结合。默认情况下优先使用基于语句的复制,只有当部分语句如果基于语句复制不完全的情况下才会自动切换为基于行数据的复制。
[root@node106.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie
mysql: [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 6
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> SHOW VARIABLES LIKE '%BINLOG_FORMAT%';        #很显然,默认就是基于行复制的
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)

mysql> 
mysql> SET binlog_format='STATEMENT';            #我们可以修改基于语句复制
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> SHOW VARIABLES LIKE '%BINLOG_FORMAT%';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)

mysql> 
mysql> quit
Bye
[root@node106.yinzhengjie.org.cn ~]# 
修改默认的复制模式格式案例(推荐大家使用默认的基于ROW的复制方式)

 

三.配置MySQL基于bin-log主从同步

1>.编辑my.cnf配置文件

[root@node106.yinzhengjie.org.cn ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=106
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #主库
[root@node107.yinzhengjie.org.cn ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=107
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #从库

2>.在主库创建一个专门用来复制的数据库用户,这样所有从库都可以用这个用户来连接主库,也可以确保这个用户只有复制的权限 

[root@node106.yinzhengjie.org.cn ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> CREATE USER 'copy'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie';
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%';
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> quit
Bye
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# mysql -u copy -pyinzhengjie -P 3306 -h node106.yinzhengjie.org.cn
mysql: [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 20
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 |
+--------------------+
1 row in set (0.00 sec)

mysql> 
mysql> QUIT
Bye
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# mysql -u copy -pyinzhengjie -P 3306 -h node106.yinzhengjie.org.cn        #从slave数据库验证远程连接主库是否正常,一定要检查

3>.获取主库的日志信息并生成主库数据镜像

mysql> FLUSH TABLES WITH READ LOCK;                  #对主库上所有表加锁,停止修改,即在从库复制的过程中主库不能执行UPDATA,DELETE,INSERT语句!
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> SHOW MASTER STATUS;                           #获取主库的日志信息,file表示当前日志文件名称,position表示当前日志的位置
+------------------------------+----------+--------------+------------------+-------------------+
| File                         | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------------+----------+--------------+------------------+-------------------+
| yinzhengjie-mysql-bin.000002 |     4095 |              |                  |                   |
+------------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 
[root@node106.yinzhengjie.org.cn ~]#  mysqldump --all-databases --master-data -u root -pyinzhengjie -P 3306 > yinzhengjie-master.db       #另开一个终端生成镜像,在生成完成之前不要释放锁。
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll
total 776
-rw-r--r-- 1 root root 791962 Jun 10 19:24 yinzhengjie-master.db
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# mysqldump --all-databases --master-data -u root -pyinzhengjie -P 3306 > yinzhengjie-master.db       #另开一个终端生成镜像,在生成完成之前不要释放锁。
mysql>
mysql> UNLOCK TABLES;                #主库数据生成镜像完毕后,我们需要把主库的锁释放掉,需要注意的是,在上锁这一段期间,我们无法对数据库进行写操作,比如UPDATA,DELETE,INSERT。
Query OK, 0 rows affected (0.00 sec)

mysql> 

4>.将主库的镜像拷贝当从库中,让从库应用主库镜像

[root@node106.yinzhengjie.org.cn ~]# ll
total 776
-rw-r--r-- 1 root root 791962 Jun 10 19:24 yinzhengjie-master.db
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# scp yinzhengjie-master.db node107.yinzhengjie.org.cn:~
The authenticity of host 'node107.yinzhengjie.org.cn (172.30.1.107)' can't be established.
ECDSA key fingerprint is SHA256:BkN6bKO2q5zMgvremE/3rOIsCaq9eTPudgfU0lhbqGo.
ECDSA key fingerprint is MD5:75:bd:cb:be:35:f8:45:a2:ea:74:bc:aa:29:74:4d:0d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'node107.yinzhengjie.org.cn,172.30.1.107' (ECDSA) to the list of known hosts.
root@node107.yinzhengjie.org.cn's password: 
yinzhengjie-master.db                                                                                                                                                                                  100%  773KB  60.7MB/s   00:00    
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# scp yinzhengjie-master.db node107.yinzhengjie.org.cn:~
[root@node107.yinzhengjie.org.cn ~]# pwd
/root
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# ll
total 776
-rw-r--r-- 1 root root 791962 Jun 10 19:37 yinzhengjie-master.db
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie
mysql: [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 5
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> source yinzhengjie-master.db;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Database changed
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 41 rows affected (0.00 sec)
Records: 41  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 699 rows affected (0.01 sec)
Records: 699  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1413 rows affected (0.01 sec)
Records: 1413  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 643 rows affected (0.08 sec)
Records: 643  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> source yinzhengjie-master.db;              #在从库应用主库的数据镜像

5>.在从库上建立复制关系,即从库指定主库的日志信息和链接信息

mysql> CHANGE MASTER TO
    -> MASTER_HOST='node106.yinzhengjie.org.cn',
    -> MASTER_PORT=3306,
    -> MASTER_USER='copy',
    -> MASTER_PASSWORD='yinzhengjie',
    -> MASTER_LOG_FILE='yinzhengjie-mysql-bin.000002',
    -> MASTER_LOG_POS=4095;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql>

6>.从库启动复制进程

mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: node106.yinzhengjie.org.cn
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: yinzhengjie-mysql-bin.000002
          Read_Master_Log_Pos: 4095
               Relay_Log_File: node107-relay-bin.000002
                Relay_Log_Pos: 332
        Relay_Master_Log_File: yinzhengjie-mysql-bin.000002
             Slave_IO_Running: Yes        #观察IO进程是否为yes,如果为yes说明正常,如果长时间处于"Connecting"状态就得检查你的从库指定的主库的链接信息是否正确
            Slave_SQL_Running: Yes        #观察SQL进程是否为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: 4095
              Relay_Log_Space: 541
              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: 0            #该参数表示从库和主库有多少秒的延迟,咱们可以理解为再过多少秒数据和主库保持一致,如果为0表示当前从库和主库的数据是一致的,如果该数较大的话你得考虑它的合理性。需要注意下该参数的值。
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 106
                  Master_UUID: 74227047-8b60-11e9-8cba-000c29985293
             Master_Info_File: /yinzhengjie/softwares/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql> 

 

四.创建cdh数据库并验证从库是否有数据

1>.在主库中创建cdh数据库

[root@node106.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie
mysql: [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 22
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> CREATE DATABASE cdh DEFAULT CHARACTER SET = utf8;
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> GRANT ALL PRIVILEGES ON cdh.* TO 'cdh'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> 
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cdh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 
mysql> QUIT
Bye
[root@node106.yinzhengjie.org.cn ~]# 

2>.在从库的服务器观察数据是否同步

[root@node107.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie
mysql: [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 12
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cdh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> QUIT
Bye
[root@node107.yinzhengjie.org.cn ~]# 

 3>.测试完成后,记得要删除cdh库,否则我们在安装CM时会提示cdh库已经存在的报错

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cdh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 
mysql> drop database cdh;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

 

 

 

posted @ 2019-02-13 21:26  尹正杰  阅读(7050)  评论(0编辑  收藏  举报