MySQL 5.7主从复制实战篇
MySQL 5.7主从复制实战篇
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.安装MySQL数据库并启动
1>.在MySQL官方下载相应的安装包(https://dev.mysql.com/downloads/mysql/)
2>.解压MySQL压缩文件至安装目录
[root@node106.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Jun : mysql-5.7.-linux-glibc2.-x86_64.tar.gz
[root@node106.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# tar -zxf mysql-5.7.-linux-glibc2.-x86_64.tar.gz -C /yinzhengjie/softwares/
[root@node106.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# ln -s /yinzhengjie/softwares/mysql-5.7.-linux-glibc2.-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 root root Jun : /yinzhengjie/softwares/mysql -> /yinzhengjie/softwares/mysql-5.7.-linux-glibc2.-x86_64
[root@node106.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/mysql/
total
drwxr-xr-x root root Jun : bin
-rw-r--r-- Dec : COPYING
drwxr-xr-x root root Jun : data
drwxr-xr-x root root Jun : docs
drwxr-xr-x root root Jun : include
drwxr-xr-x root root Jun : lib
drwxr-xr-x root root Jun : man
-rw-r--r-- Dec : README
drwxr-xr-x root root Jun : share
drwxr-xr-x root root Jun : 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 - ~/.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=(mysql) gid=(mysql) groups=(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.-linux-glibc2.-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 mysql mysql Jun : /yinzhengjie/softwares/mysql -> /yinzhengjie/softwares/mysql-5.7.-linux-glibc2.-x86_64
[root@node106.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# ll -d /yinzhengjie/softwares/mysql-5.7.-linux-glibc2.-x86_64/
drwxr-xr-x mysql mysql Jun : /yinzhengjie/softwares/mysql-5.7.-linux-glibc2.-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
[root@node107.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data &
[]
[root@node106.yinzhengjie.org.cn ~]# --10T09::.425725Z [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
--10T09::.122750Z [Warning] InnoDB: New log files created, LSN=
--10T09::.235538Z [Warning] InnoDB: Creating foreign key constraint system tables.
--10T09::.304637Z [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: -8b60-11e9-8cba-000c29985293.
--10T09::.306459Z [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
--10T09::.312634Z [Note] A temporary password is generated for root@localhost: .wIyMa&xf8BL #注意,这里标红的为MySQL服务器的临时密码,建议大家先保存一下,一会登陆数据库时要用到,如果你忘记了临时密码重复该步骤即可。 []+ 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
-rw-r----- mysql mysql Jun : auto.cnf
-rw-r----- mysql mysql Jun : ib_buffer_pool
-rw-r----- mysql mysql Jun : ibdata1
-rw-r----- mysql mysql Jun : ib_logfile0
-rw-r----- mysql mysql Jun : ib_logfile1
drwxr-x--- mysql mysql Jun : mysql
drwxr-x--- mysql mysql Jun : performance_schema
drwxr-x--- mysql mysql Jun : sys
-rw-r----- mysql mysql Jun : yinzhengjie-mysql-bin.
-rw-r----- mysql mysql Jun : 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=
[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~()−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 ()
[root@node106.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# ps -ef | grep mysql
root : pts/ :: /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 : pts/ :: /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 : pts/ :: 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
Server version: 5.7.-log Copyright (c) , , 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, 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
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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 | |
| server_id_bits | |
+----------------+-------+
rows in set (0.01 sec) mysql>
mysql> quit
Bye
[root@node106.yinzhengjie.org.cn ~]#
8>.在另一外一台服务器上重复上述操作(我们实验环境用node107.yinzhengjie.org.cn作备用的MySQL数据库)
二. MySQL复制概述
1>.MySQL复制介绍
()MySQL复制允许将主实例(master)上的数据同步到一个或多个从实例(slave)上,默认情况下复制是异步进行的,从库也不需要一直连接主库来同步数据。
()MySQL复制的数据粒度可以是主实例上所有的数据库,也可以是指定的一个或多个数据库,也可以是一个数据库里的指定的表。
()MySQL复制带来的优势在于:
扩展能力:
通过复制可以将MySQL的性分到一个或多个slave上。这要求所有的写操作和修改操作都必须在Master上完成,而读操作可以被分配到一个或多个salve上。将读写分离到不同服务执行之后,MySQL的读写性能得到提升。
数据库备份:
由于从实例时同步主实例的数据,所以可以将备份作业部署到从库。
数据分析和报表:
同样,一些数据分析和报表的实现可以在从实例执行,以减少对主库的性能影响。
容灾能力:
可以在物理距离较远的另一个数据建立slave,保证在主实例所在地区遭遇灾难时,在另一个数据中心能快速恢复。
2>.MySQL复制有两种方法
(1)传统方式
基于主库的bin-log将日志事件和事件位置复制到从库,从库再加以应用来达到主从同步的目的。
(2)Gtid方式
global transaction identitifiers 是基于事物来复制数据,因此也就不依赖日志文件,同时又能更好的保证主从库数据一致性。
3>.MySQL复制有多种类型
()异步复制
一个主库,一个或多个从库,数据异步同步到从库。 ()同步复制
在MySQL cluster中特有的复制方式。 ()半同步复制
在异步复制的基础上,确保任何一个主库上的事物在提交之前至少有一个从库已经收到该事物并日志记录下来。 ()延迟复制
在异步复制的基础上,人为设定主库和从库的数据同步延迟时间,即保证数据延迟至少是这个参数。
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
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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 |
+---------------+-------+
row in set (0.00 sec) mysql>
mysql> SET binlog_format='STATEMENT'; #我们可以修改基于语句复制
Query OK, rows affected (0.00 sec) mysql>
mysql> SHOW VARIABLES LIKE '%BINLOG_FORMAT%';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
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=
[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=
[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
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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, rows affected (0.01 sec) mysql>
mysql> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%';
Query OK, 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 -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
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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 |
+--------------------+
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, 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. | | | | |
+------------------------------+----------+--------------+------------------+-------------------+
row in set (0.00 sec) mysql>
[root@node106.yinzhengjie.org.cn ~]# mysqldump --all-databases --master-data -u root -pyinzhengjie -P > 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
-rw-r--r-- root root Jun : 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, rows affected (0.00 sec) mysql>
4>.将主库的镜像拷贝当从库中,让从库应用主库镜像
[root@node106.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Jun : 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::bd:cb:be::f8::a2:ea::bc:aa:::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 % 773KB .7MB/s :
[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
-rw-r--r-- root root Jun : 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
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected, warning (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, row affected (0.00 sec) Database changed
Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.08 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.01 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected, warning (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec) mysql>
mysql> source yinzhengjie-master.db; #在从库应用主库的数据镜像
5>.在从库上建立复制关系,即从库指定主库的日志信息和链接信息
mysql> CHANGE MASTER TO
-> MASTER_HOST='node106.yinzhengjie.org.cn',
-> MASTER_PORT=,
-> MASTER_USER='copy',
-> MASTER_PASSWORD='yinzhengjie',
-> MASTER_LOG_FILE='yinzhengjie-mysql-bin.000002',
-> MASTER_LOG_POS=;
Query OK, rows affected, warnings (0.01 sec) mysql>
6>.从库启动复制进程
mysql> START SLAVE;
Query OK, rows affected (0.00 sec) mysql>
mysql> SHOW SLAVE STATUS\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: node106.yinzhengjie.org.cn
Master_User: copy
Master_Port:
Connect_Retry:
Master_Log_File: yinzhengjie-mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: node107-relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: yinzhengjie-mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
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:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: -8b60-11e9-8cba-000c29985293
Master_Info_File: /yinzhengjie/softwares/mysql/data/master.info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
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
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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, 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, rows affected, warning (0.01 sec) mysql>
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cdh |
| mysql |
| performance_schema |
| sys |
+--------------------+
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
Server version: 5.7.-log MySQL Community Server (GPL) Copyright (c) , , 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 |
+--------------------+
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>
MySQL 5.7主从复制实战篇的更多相关文章
- MySQL的初次见面礼基础实战篇
[版权申明] http://blog.csdn.net/javazejian/article/details/61614366 出自[zejian的博客] 关联文章: MySQL的初次见面礼基础实战篇 ...
- NetCore项目实战篇08---Docker挂载mysql并连接.netCoreWeb
我们的项目之前在直接连接的mysql,今天我们将通过docker挂载mysql 并与我们开发的webapi项目连接. 1. 安装docker 下载地址: https://download.docker ...
- Mysql高手系列 - 第20篇:异常捕获及处理详解(实战经验)
Mysql系列的目标是:通过这个系列从入门到全面掌握一个高级开发所需要的全部技能. 这是Mysql系列第20篇. 环境:mysql5.7.25,cmd命令中进行演示. 代码中被[]包含的表示可选,|符 ...
- 洗礼灵魂,修炼python(82)--全栈项目实战篇(10)—— 信用卡+商城项目(模拟京东淘宝)
本次项目相当于对python基础做总结,常用语法,数组类型,函数,文本操作等等 本项目在博客园里其他开发者也做过,我是稍作修改来的,大体没变的 项目需求: 信用卡+商城: A.信用卡(类似白条/花呗) ...
- MySQL 5.7主从复制从零开始设置及全面详解——实现多线程并行同步,解决主从复制延迟问题!
MySQL 5.7主从复制从零开始设置及全面详解——实现多线程并行同步,解决主从复制延迟问题!2017年06月15日 19:59:44 蓝色-鸢尾 阅读数:2062版权声明:本文为博主原创文章,如需转 ...
- 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构
本篇承接上篇内容,如果你不小心点击进来,建议重新从第一篇开始完整阅读. 构建NetCore应用框架之实战篇索引 一.BitAdminCore框架简介 从前篇论述我们知道,我们接下来将要去做一个管理系统 ...
- 为什么mysql要做主从复制?
为什么MySQL要做主从复制(读写分离)? 通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低. 为了提升业务系统性能,优化用户体验,可以通过做主从复制(读写分离)来减轻主 ...
- 如何在Visual Studio 2017中使用C# 7+语法 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构 构建NetCore应用框架之实战篇系列 构建NetCore应用框架之实战篇(一):什么是框架,如何设计一个框架 NetCore入门篇:(十二)在IIS中部署Net Core程序
如何在Visual Studio 2017中使用C# 7+语法 前言 之前不知看过哪位前辈的博文有点印象C# 7控制台开始支持执行异步方法,然后闲来无事,搞着,搞着没搞出来,然后就写了这篇博文,不 ...
- JS-正则表达式实战篇(Angel著)
JS-正则表达式实战篇(Angel著) 大家会看到我最新的系列博客都是spring boot怎么突然来了一个js的呢,而且这个貌似对大家而言好像很简单的嘛,所以在写之前我说说我写这一篇文章的初衷.公司 ...
随机推荐
- matlab学习笔记4--多媒体文件的保存和读取
一起来学matlab-matlab学习笔记4 数据导入和导出_2 多媒体文件的保存和读取 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用&g ...
- 【linux学习笔记七】关机重启命令
shutdown命令 shutdown [选项] 时间 #-c 取消前一个关机命令 #-h 关机(慎用远程关机) #-r 重启 其它关机命令 halt poweroff init 0 其它重启命令 ...
- 大网扫描在发现APT组织方面的一个应用
如何发现CobalStike服务端? 答: 扫HTTP response header: "HTTP/1.1 404 Not Found" balaba-"Server& ...
- python json dumps datetime类型报错
# -*- coding: utf-8 -*- import json from datetime import date, datetime class MyEncoder(json.JSONEnc ...
- python gzip
通常用gzip压缩过的云端数据需要做解压处理,以下代码主要用python3实现对获取到的云端gzip压缩数据进行还原. # -*- coding: utf-8 -*- ""&quo ...
- php xml转array的方法
php xml转array的方法 <pre><?php $responseXml='<xml><appid>12</appid></xml& ...
- [转帖]WannaCry惊天大发现!疑似朝鲜黑客组织Lazarus所为
WannaCry惊天大发现!疑似朝鲜黑客组织Lazarus所为 Threatbook2017-05-16共588524人围观 ,发现 17 个不明物体系统安全 https://www.freebuf. ...
- Netty原理架构解析
Netty原理架构解析 转载自:http://www.sohu.com/a/272879207_463994本文转载关于Netty的原理架构解析,方便之后巩固复习 Netty是一个异步事件驱动的网络应 ...
- python基础 — random库
python中用于生成伪随机数的函数库是random 因为是标准库,使用时候只需要import random random库包含两类函数,常用的共8个 --基本随机函数: seed(), random ...
- SQL学习-SQL Server