MYSQL主从配置
mysql
MYSQL(mysql replication),主要用于mysql的实时备份或者读写分离。
mysql主从配置解决的问题
1.避免数据库服务器宕机导致数据丢失
2.防止因为业务量大,数据多,访问量大,导致数据库无法保证服务质量
主从作用
1.实时灾备,用于故障切换
2.读写分离,提供查询服务
3备份,避免影响业务
主从复制原理
1.主库将所有写操作记录到binlog日志文件中,并生成log dump线程,log dump线程将binlog日志文件传给从库的I/O线程。
2.从库生成两个线程。一个I/O线程,一个SQL线程。
I/O线程去请求主库的binlog日志文件,并将得到的binlog日志写到relay log(中继日志)日志文件中。
SQL线程读入relay log日志,并将解析成具体的操作,实现主从的操作一致,达成最终数据一致的目的。
mysql主从复制的实现原理大致如下(来源网络):
主从复制配置
环境说明
准备两台MYSQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作。
数据库角色 | IP | 应用与系统版本 |
---|---|---|
主数据库 | 192.168.228.23 | centos7 |
从数据库 | 192.168.228.30 | centos7 |
主库的mysql安装(二进制安装)
关闭防火墙和selinux
[root@yxr ~]# systemctl stop firewalld
[root@yxr ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@yxr ~]# setenforce 0
[root@yxr ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
安装依赖包
[root@yxr ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建用户和组
[root@yxr ~]# groupadd -r -g 306 mysql
[root@yxr ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql
下载二进制格式的mysql软件包
[root@yxr ~]# cd /usr/src/
[root@yxr src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
解压软件至/usr/local
[root@yxr src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local
[root@yxr src]# cd /usr/local/
[root@yxr local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.22-linux-glibc2.12-x86_64 share
[root@yxr local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@yxr local]# ll
total 0
drwxr-xr-x. 2 root root 6 Nov 5 2016 bin
drwxr-xr-x. 2 root root 6 Nov 5 2016 etc
drwxr-xr-x. 2 root root 6 Nov 5 2016 games
drwxr-xr-x. 2 root root 6 Nov 5 2016 include
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib
drwxr-xr-x. 2 root root 6 Nov 5 2016 lib64
drwxr-xr-x. 2 root root 6 Nov 5 2016 libexec
lrwxrwxrwx. 1 root root 36 Sep 7 12:12 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 Sep 7 12:10 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root 6 Nov 5 2016 sbin
drwxr-xr-x. 5 root root 49 Aug 10 14:26 share
drwxr-xr-x. 2 root root 6 Nov 5 2016 src
修改目录/usr/local/mysql的属主属组
[root@yxr local]# chown -R mysql.mysql /usr/local/mysql
[root@yxr local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Sep 7 12:12 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
添加环境变量
[root@yxr local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@yxr local]# . /etc/profile.d/mysql.sh
[root@yxr local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
建立数据存放目录
[root@yxr local]# mkdir /opt/data
[root@yxr local]# chown -R mysql.mysql /opt/data/
[root@yxr local]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Sep 7 12:15 data
初始化数据库
[root@yxr local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-09-07T04:16:21.264448Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-07T04:16:23.664475Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-07T04:16:24.124531Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-07T04:16:24.196085Z 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: c8480534-b254-11e8-b85d-000c29989243.
2018-09-07T04:16:24.199078Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-07T04:16:24.201714Z 1 [Note] A temporary password is generated for root@localhost: tQ8v9;7Qp>iY //临时密码
配置mysql
[root@yxr ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@yxr ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@yxr ~]# ldconfig -v
生成配置文件
[root@yxr ~]# cat > /etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF
配置服务启动脚本
[root@yxr ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld[root@yxr ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@yxr ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
启动mysql
[root@yxr ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/yxr.err'.
. SUCCESS!
[root@yxr ~]# ps -ef | grep mysql
root 15733 1 0 12:22 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 15911 15733 5 12:22 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=yxr.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 15943 15394 0 12:22 pts/1 00:00:00 grep --color=auto mysql
[root@yxr ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
修改密码,使用临时密码登录
[root@yxr ~]# 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.22
Copyright (c) 2000, 2018, 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> set password = password('yaoxiaorong!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye
从库服务器安装mysql-5.7版本,安装步骤与主库服务器相同,略。
mysql主从配置
在主库中创建几个数据库,并在其中一个数据库中创建表student,该表包含三个字段(id,nname,age),并2往新建的student表中插入数据。
创建数据库
mysql> create database yaoxiaorong;
Query OK, 1 row affected (0.00 sec)
mysql> create database yy;
Query OK, 1 row affected (0.00 sec)
mysql> create database xx;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xx |
| yaoxiaorong |
| yy |
+--------------------+
7 rows in set (0.01 sec)
进入yaoxiaorong这个数据库
mysql> use yaoxiaorong;
Database changed
创建student这张表
mysql> create table student(id int not null, name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.08 sec)
查看表是否建立成功
mysql> show tables;
+-----------------------+
| Tables_in_yaoxiaorong |
+-----------------------+
| student |
+-----------------------+
1 row in set (0.00 sec)
插入数据
mysql> insert into student(id,name,age)values(1,'tom',20),(2,'jerry',23),(3,'wangqing',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20);
Query OK, 6 rows affected (0.11 sec)
Records: 6 Duplicates: 0 Warnings: 0
查看下新建的表有无内容
mysql> select * from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
+----+-----------+------+
6 rows in set (0.00 sec)
mysql> exit
Bye
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中。
先查看主库有哪些库
[root@yxr ~]# mysql -uroot -pyaoxiaorong! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xx |
| yaoxiaorong |
| yy |
+--------------------+
在查看从库有哪些库
[root@arongya ~]# mysql -uroot -pyaoxiaorong! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
全备主库(全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一样)此锁表的终端必须在备份完成以后才能退出
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.03 sec)
备份主库并将别备份文件传送到从库
[root@yxr ~]# mysqldump -uroot -pyaoxiaorong! --all-databases > /opt/all-201809171422.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@yxr ~]# ls /opt/
all-201809171422.sql data
[root@yxr ~]# scp /opt/all-201809171422.sql root@192.168.228.30:/opt/
root@192.168.228.30's password:
all-201809171422.sql 100% 784KB 9.0MB/s 00:00
解锁主库的锁表状态,直接退出交互式界面即可
mysql > quit
Bye
在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
从库上配置
[root@arongya ~]# ls /opt/
all-201809171422.sql data
[root@arongya ~]# mysql -uroot -pyaoxiaorong! </opt/all-201809171422.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@arongya ~]# mysql -uroot -pyaoxiaorong! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| xx |
| yaoxiaorong |
| yy |
+--------------------+
在主数据库里创建一个同步账号授权给从数据库使用
一定要此操作,我在做的途中就是没此操作,导致I/O线程没启动
mysql> create user 'repl'@'192.168.228.30' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'repl'@'192.168.228.30';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
配置主数据库
[root@yxr ~]# vim /etc/my.cnf
[root@yxr ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
#添加以下内容
log-bin=mysql-bin //启动binlog日志
server-id=1 //数据库服务器唯一标识符,主库的server-id值必须比从库的大
symbolic-links=0 (可不要)
log-error=/var/log/mysqld.log (可不要)
重启mysql服务
[root@yxr ~]# service mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.... SUCCESS!
[root@yxr ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
查看主库的状态
[root@yxr ~]# 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.22-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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 master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置从数据库
[root@arongya ~]# vim /etc/my.cnf
[root@arongya ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
#添加以下内容
server-id=2 //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log=mysql-relay-bin //启用中继日志relay-log
#symbolic-links=0
#log-error=/var/log/mysqld.log
重启从库的mysql服务
[root@arongya ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL... SUCCESS!
[root@arongya ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
配置并启动主从复制
mysql> change master to
-> master_host='192.168.228.23',
-> master_user='repl',
-> master_password='123456',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.19 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
查看从服务器状态
mysql> mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.228.23
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000013
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes //此处必须为yse
Slave_SQL_Running: Yes //此处必须为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: 154
Relay_Log_Space: 740
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
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: 1
Master_UUID: c8480534-b254-11e8-b85d-000c29989243
Master_Info_File: /opt/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)
在主服务器的student的student插入数据:
mysql> use yaoxiaorong;
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> select * from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
+----+-----------+------+
6 rows in set (0.00 sec)
mysql> insert into student values (7,'wangwu',20),(8,'sea',23),(9,'wangliu',34);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | wangwu | 20 |
| 8 | sea | 23 |
| 9 | wangliu | 34 |
+----+-----------+------+
9 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
mysql> use yaoxiaorong;
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> select * from student;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | wangwu | 20 |
| 8 | sea | 23 |
| 9 | wangliu | 34 |
+----+-----------+------+
9 rows in set (0.00 sec)