mysql主从
1. 主从简介
1.1 主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
1.2 主从形式
- 一主一从
- 主主复制
- 一主多从—扩展系统读取的性能,因为读是在从库读取的 (适合读操作比较多的时候)
- 多主一从—5.7开始支持 (适合写操作比较多的时候)
- 联级复制
2. 主从复制原理
主从复制步骤:
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog(binary log,二进制日志,记录了所有的写操作:增、删、改)日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程 I/O线程去请求主库的binlog,并将得到的binlog日志写到relay
log(中继日志) 文件中 SQL线程,会读取relay
log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
3. 主从复制配置
主从复制配置步骤:
- 确保从数据库与主数据库里的数据一样
- 在主数据库里创建一个同步账号授权给从数据库使用 (主从同步同步的是主从复制配置完以后生成的数据)
- 配置主数据库(修改配置文件)
- 配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
部署主从的两种情况:
- 主数据库原先就有数据
- 新部署的主从
3.1 mysql安装(方案一:新部署的主从)
3.2.1 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库
[root@master ~]# mysql -uroot -p1 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
//再查看从库有哪些库
[root@slave ~]# mysql -uroot -p1 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
3.2.2 在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication slave on *.* to 'repl'@'192.168.47.131' identified by 'repl123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
[root@slave ~]# mysql -urepl -prepl123 -h192.168.47.130
3.2.3 配置主数据库
[root@master ~]# vim /etc/my.cnf
//在[mysqld]这段的后面加上如下内容
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysqld.pid
skip-name-resolve
# replication slave config
log-bin=mysql-bin //启用binlog日志
server-id=10 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
//重启mysql服务
[root@master ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
//查看主库的状态
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)
3.2.4 配置从数据库
[root@slave ~]# vim /etc/my.cnf
//添加如下内容
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysqld.pid
skip-name-resolve
# replication slave config
server-id=20 //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log=mysql_relay //启用中继日志relay-log
//重启从库的mysql服务
[root@slave ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
//配置并启动主从复制
mysql> change master to
-> master_host='192.168.47.130',
-> master_user='repl',
-> master_password='repl1',
-> master_log_file='mysql_bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
//查看从服务器状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.47.130
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql_relay.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes //此处必须为Yes
Slave_SQL_Running: Yes //此处必须为Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
3.2.5 测试验证
在主服务器上创建一个wjj数据库:
mysql> create database wjj;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wjj |
+--------------------+
5 rows in set (0.00 sec)
在从slave查看是否有wjj这个库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wjj |
+--------------------+
5 rows in set (0.00 sec)
4.1 mysql安装(方案二:主数据库原先就有数据)
3.2.1 确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库
[root@master ~]# mysql -uroot -p123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wjj |
+--------------------+
//再查看从库有哪些库
[root@slave ~]# mysql -uroot -p123 -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.01 sec)
//此锁表的终端必须在备份完成以后才能退出
//查看此时主库的状态
mysql> show processlist;
+----+------+-----------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+----------+------------------+
| 6 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+------+-----------+------+---------+------+----------+------------------+
1 row in set (0.00 sec)
//备份主库并将备份文件传送到从库
[root@master ~]# mysqldump -uroot -p123 --all-databases > all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# scp all.sql 192.168.47.131:/root/
root@192.168.47.131's password:
all.sql 100% 853KB 133.6MB/s 00:00
[root@master ~]#
[root@slave ~]# ls
公共 视频 文档 音乐 all.sql initial-setup-ks.cfg
模板 图片 下载 桌面 anaconda-ks.cfg pass
3.2.2 在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication slave on *.* to 'repl'@'192.168.47.131' identified by 'repl123!';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
3.2.3 配置主数据库
[root@master ~]# vim /etc/my.cnf
//在[mysqld]这段的后面加上如下内容
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysqld.pid
skip-name-resolve
log-bin=mysql-bin //启用binlog日志
server-id=10 //数据库服务器唯一标识符,主库的server-id值必须比从库的大
//重启mysql服务
[root@master ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
//查看主库的状态
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)
3.2.4 配置从数据库
[root@slave ~]# vim /etc/my.cnf
//添加如下内容
[mysqld]
port = 3306
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
pid-file = /opt/data/mysqld.pid
skip-name-resolve
server-id=20 //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log=mysql-relay-bin //启用中继日志relay-log
//重启从库的mysql服务
[root@slave ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
//配置并启动主从复制
mysql> change master to
-> master_host='192.168.47.130',
-> master_user='repl',
-> master_password='repl123!',
-> master_log_file='mysql_bin.000001',
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
//查看从服务器状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.47.130
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: mysql_relay.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes //此处必须为Yes
Slave_SQL_Running: Yes //此处必须为Yes
3.2.5 测试验证
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 10 |
| 2 | jerry | 12 |
| 3 | lisi | 15 |
+----+-------+------+
3 rows in set (0.01 sec)
mysql> insert student(name,age) value('zhangsan',20);
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 10 |
| 2 | jerry | 12 |
| 3 | lisi | 15 |
| 4 | zhangsan | 20 |
+----+----------+------+
4 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
mysql> use wjj;
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 | 10 |
| 2 | jerry | 12 |
| 3 | lisi | 15 |
| 4 | zhangsan | 20 |
+----+----------+------+
4 rows in set (0.00 sec)