MySQL5.7基于binary log的主从复制
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
基于binary log 的复制是指主库将修改操作写入binary log 中,从库负责读取主库的binary log ,并且在本地复制一份,然后里面的操作在从库执行一遍。
每个从库会保存目前读取主库日志的文件名和日志位置。
主库和每个从库都必须有一个唯一ID,叫server-id配置在配置文件中。
一.部署mysql数据库
1>.操作系统环境(2台配置想用的虚拟机即可,配置如下)
[root@node101 ~]# hostname node101.yinzhengjie.org.cn [root@node101 ~]# [root@node101 ~]# [root@node101 ~]# cat /etc/hosts | grep yinzhengjie 172.30.1.101 node101.yinzhengjie.org.cn 172.30.1.102 node102.yinzhengjie.org.cn [root@node101 ~]# [root@node101 ~]# [root@node101 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@node101 ~]# [root@node101 ~]# [root@node101 ~]# free -h total used free shared buff/cache available Mem: 3.7G 127M 3.4G 8.5M 166M 3.4G Swap: 2.0G 0B 2.0G [root@node101 ~]# [root@node101 ~]# [root@node101 ~]# uname -r 3.10.0-327.el7.x86_64 [root@node101 ~]# [root@node101 ~]# [root@node101 ~]# uname -m x86_64 [root@node101 ~]# [root@node101 ~]#
2>.安装mysql数据库
如下图所示,下载MySQL的tar包,下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz。
[root@node101 ~]# mkdir -pv /yinzhengjie/softwares mkdir: created directory ‘/yinzhengjie’ mkdir: created directory ‘/yinzhengjie/softwares’ [root@node101 ~]# [root@node101 ~]# ll total 629752 -rw-r--r--. 1 root root 644862820 Mar 1 08:24 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz [root@node101 ~]# [root@node101 ~]# tar -zxf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /yinzhengjie/softwares/ [root@node101 ~]# [root@node101 ~]# ln -s /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64 /yinzhengjie/softwares/mysql [root@node101 ~]# [root@node101 ~]# mkdir /yinzhengjie/softwares/mysql/data [root@node101 ~]# [root@node101 ~]# ll /yinzhengjie/softwares/mysql/ total 40 drwxr-xr-x. 2 root root 4096 Mar 2 06:27 bin -rw-r--r--. 1 7161 31415 17987 Dec 21 02:39 COPYING drwxr-xr-x. 2 root root 6 Mar 2 06:27 data drwxr-xr-x. 2 root root 52 Mar 2 06:27 docs drwxr-xr-x. 3 root root 4096 Mar 2 06:27 include drwxr-xr-x. 5 root root 4096 Mar 2 06:27 lib drwxr-xr-x. 4 root root 28 Mar 2 06:27 man -rw-r--r--. 1 7161 31415 2478 Dec 21 02:39 README drwxr-xr-x. 28 root root 4096 Mar 2 06:27 share drwxr-xr-x. 2 root root 86 Mar 2 06:27 support-files [root@node101 ~]# [root@node101 ~]# useradd -s /sbin/nologin mysql [root@node101 ~]# [root@node101 ~]# id mysql uid=1001(mysql) gid=1001(mysql) groups=1001(mysql) [root@node101 ~]# [root@node101 ~]# [root@node101 ~]# chown mysql:mysql -R /yinzhengjie/softwares/mysql [root@node101 ~]# [root@node101 ~]# ll -d /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64/ drwxr-xr-x. 10 mysql mysql 4096 Mar 2 06:27 /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64/ [root@node101 ~]# [root@node101 ~]# cat ~/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin:/yinzhengjie/softwares/mysql/bin/ export PATH [root@node101 ~]# [root@node101 ~]# source ~/.bash_profile [root@node101 ~]# [root@node101 ~]# mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data & [1] 2626 [root@node101 ~]# 2019-03-02T14:37:23.217229Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-03-02T14:37:24.603930Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-03-02T14:37:24.794673Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-03-02T14:37:24.862231Z 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: b2127a6e-3cf8-11e9-ae0d-000c29fe9bef. 2019-03-02T14:37:24.863307Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-03-02T14:37:24.864126Z 1 [Note] A temporary password is generated for root@localhost: prxUpf-#P7su [1]+ Done mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data [root@node101 ~]# [root@node101 ~]# [root@node101 ~]# [root@node101 ~]# cat /etc/my.cnf [mysqld] basedir=/yinzhengjie/softwares/mysql/ datadir=/yinzhengjie/softwares/mysql/data/ [root@node101 ~]# [root@node101 ~]# /etc/init.d/mysql.server start Starting MySQL.Logging to '/yinzhengjie/softwares/mysql/data/node101.yinzhengjie.org.cn.err'. SUCCESS! [root@node101 ~]# [root@node101 ~]# /etc/init.d/mysql.server status SUCCESS! MySQL running (4494) [root@node101 ~]# [root@node101 ~]# /etc/init.d/mysql.server status SUCCESS! MySQL running (4494) [root@node101 ~]# [root@node101 ~]# [root@node101 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.25 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> alter user user() identified by 'yinzhengjie'; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [root@node101 ~]# [root@node101 ~]# 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 3 Server version: 5.7.25 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 | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> quit Bye [root@node101 ~]#