mysql主从
mysql主从
主从简介
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
- 用一台数据库存放数据,若此数据库服务器宕机了导致数据丢失怎么办?
- 业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?
主从作用
- 实时灾备,用于故障切换
- 读写分离,提供查询服务
- 备份,避免影响业务
主从形式
- 一主一从
- 主主复制
- 一主多从---扩展系统读取的性能,因为读是在从库读取的
- 多主一从---5.7开始支持
- 联级复制
主从复制原理
主从复制步骤:
- 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
- 从库生成两个线程,一个I/O线程,一个SQL线程
- I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
- SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的
主从复制配置
主从复制配置步骤:
1.确保从数据库与主数据库里的数据一样
2.在主数据库里创建一个同步账号授权给从数据库使用
3.配置主数据库(修改配置文件)
4.配置从数据库(修改配置文件)
需求:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 172.16.12.128 | centos8/redhat8mysql-5.7 | 有数据 |
从数据库 | 172.16.12.129 | centos8/redhat8mysql-5.7 | 无数据 |
mysql安装
分别在主从两台服务器上安装mysql-5.7版本,此处略过安装步骤,若有疑问请参考《mysql基础》与《mysql进阶》两篇文章。
mysql主从配置
确保从数据库与主数据库里的数据一样
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
查看主库有哪些库
[root@localhost ~]# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| crouce |
| ljl0 |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.14 sec)
查看从库有哪些库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| crouce |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
全备主库
全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> flush tables with read lock; 此锁表的终端必须在备份完成以后才能退出
备份主库并将备份文件传送到从库
[root@localhost ~]# mysqldump -uroot -p123456 --all-databases > /opt/all-databases-$(date '+%F-%H-%M-%S').sql
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
all-databases-2022-07-31-12-19-54.sql data
[root@localhost opt]# scp /opt/all-databases-2022-07-31-12-19-54.sql root@192.168.203.136:/opt/
The authenticity of host '192.168.203.136 (192.168.203.136)' can't be established.
ECDSA key fingerprint is SHA256:kkx5iMZVCVXStmj9rLXfZHTkfTOajbp7CXETBAHAYBg.
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '192.168.203.136' (ECDSA) to the list of known hosts.
解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@localhost ~]# mysql -uroot -p123456 < /opt/all-databases-2022-07-31-12-19-54.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| crouce |
| ljl0 |
| mysql |
| performance_schema |
| sys |
+--------------------+
在主数据库里创建一个同步账号授权给从数据库使用
mysql> CREATE USER 'repl'@'192.168.203.136' IDENTIFIED BY 'repl123';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'repl'@'192.168.203.136';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
配置主数据库
在主服务器上配置主从复制,开启二进制日志,设置服务id
[root@localhost opt]# vim /etc/my.cnf
log-bin=mysql_bin //开启二进制日志
server_id=1 // 设置从库的唯一标识符,主库的server-id值必须小于从库的该值
重启从库的mysql服务
[root@localhost opt]# systemctl restart mysqld
[root@localhost opt]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
查看主库的状态
[root@localhost opt]# mysql -uroot -p123456 -e 'show master status;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000007 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
配置从数据库
[root@localhost ~]# vim /etc/my.cnf
server-id=2 //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log=mysql-relay-bin //启用中继日志relay-log
重启从库的mysql服务
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
配置并启动主从复制
mysql> change master to
-> master_host='192.168.203.133',
-> master_user='repl',
-> master_password='repl123',
-> master_log_file='mysql_bin.000007',
-> 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.203.133
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000007
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql_bin.000007
Slave_IO_Running: yes /I/O必须yes
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: 154
Relay_Log_Space: 154
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2003
Last_IO_Error: error connecting to master 'root@192.168.203.133:3306' - retry-time: 60 retries: 1
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
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: 220731 12:47:43
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)
测试验证
在主服务器的ljl0库的students表中插入数据
mysql> select * from students;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | dany | 25 | 男 | 160 | 1 |
| 2 | green | 23 | 男 | 158 | 2 |
| 3 | henry | 23 | 女 | 185 | 1 |
| 4 | jane | 22 | 男 | 162 | 3 |
| 5 | jim | 24 | 女 | 175 | 2 |
| 6 | john | 21 | 男 | 172 | 4 |
| 7 | lily | 22 | 男 | 165 | 4 |
| 8 | susan | 23 | 男 | 170 | 5 |
| 9 | thomas | 22 | 女 | 178 | 5 |
| 10 | tom | 23 | 女 | 165 | 5 |
| 11 | liming | 19 | 男 | 150 | 8 |
+----+--------+------+------+--------+-----------+
11 rows in set (0.00 sec)
mysql> insert students(id,name,age,sex,height,course_id) values (12,'ljl',22,'男',1882,6),(13,'qqq',23,'男',111,6);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from students;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | dany | 25 | 男 | 160 | 1 |
| 2 | green | 23 | 男 | 158 | 2 |
| 3 | henry | 23 | 女 | 185 | 1 |
| 4 | jane | 22 | 男 | 162 | 3 |
| 5 | jim | 24 | 女 | 175 | 2 |
| 6 | john | 21 | 男 | 172 | 4 |
| 7 | lily | 22 | 男 | 165 | 4 |
| 8 | susan | 23 | 男 | 170 | 5 |
| 9 | thomas | 22 | 女 | 178 | 5 |
| 10 | tom | 23 | 女 | 165 | 5 |
| 11 | liming | 19 | 男 | 150 | 8 |
| 12 | ljl | 22 | 男 | 182 | 6 |
| 13 | qqq | 23 | 男 | 111 | 6 |
+----+--------+------+------+--------+-----------+
13 rows in set (0.00 sec)
在从数据库中查看数据是否同步
mysql> use ljl0;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
————————————————
版权声明:本文为CSDN博主「Grieved-林」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_53388991/article/details/126075723
mysql> select * from students;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | dany | 25 | 男 | 160 | 1 |
| 2 | green | 23 | 男 | 158 | 2 |
| 3 | henry | 23 | 女 | 185 | 1 |
| 4 | jane | 22 | 男 | 162 | 3 |
| 5 | jim | 24 | 女 | 175 | 2 |
| 6 | john | 21 | 男 | 172 | 4 |
| 7 | lily | 22 | 男 | 165 | 4 |
| 8 | susan | 23 | 男 | 170 | 5 |
| 9 | thomas | 22 | 女 | 178 | 5 |
| 10 | tom | 23 | 女 | 165 | 5 |
| 11 | liming | 19 | 男 | 150 | 8 |
| 12 | ljl | 22 | 男 | 182 | 6 |
| 13 | qqq | 23 | 男 | 111 | 6 |
+----+--------+------+------+--------+-----------+
13 rows in set (0.00 sec
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律