#关于MYSQL做数据集群主从配置的简易上手学习的方法
前言
由于前面出现了DDOS攻击,当时第一反应想要换服务器,可后来一想,数据库的数据完全没有同步的备份,也没办法弄。 所以后来就想,如果能做个主从备份,起码数据库的备份是能同步到外面来的,这样起码可以基于这个数据库再建立新的程序,虽然麻烦了些,也不失为一种策略,尽管对DDOS来说可能并不能从根本上解决问题。
由于前段时间紧锣密鼓的一些其他工作,包括公司安排的去岳阳县出差等等,也一直没能去弄这个。刚好这个周末有时间,就去搞了一下,还是有些值得注意的地方,也记录一下这方面的经验,避免今后走弯路。
相关的网络资源还是挺多的,不过没几个我满意的,因为好多地方似乎没说得很清楚,比如要什么样的条件能够比较顺利配出来,所以在摸索过程中,遇到问题的时候,也很无奈。尽量在这篇文章中说明白点。
操作过程
由于做从数据库的服务器已经有了个同名的数据库,结构也一致,只是数据不一样,所以一开始我的想法是,直接把这两个数据库做成主从同步就可以了。
于是按照下面的步骤,进行配置(我的服务器是LINUX,如果是WINDOWS请根据实际情况作相应的调整)
1 配置主服务器 配置 /etc/my.conf 设置server_id和log-bin
[mysqld]
server_id=1
log-bin=mysql-bin
配置完成后,重启mysql
[root@ecs-338069 log]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
2.新建用户 配置用于做主从复制数据同步的用户,名字可以根据自己的喜好来取,我这里采用slave作为用户名,来进行配置
[root@ecs-338069 log]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44-log MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> create user slave@'%' identified by 'Dbslave123';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to slave@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
创建完用户后,一定要授权(grant replication slave)
3 查看主数据库日志状态 接下来,要记住主服务器的日志状态信息(后面会用到)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 96301 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
4 配置备份服务器(从服务器)
切换到备份服务器(从服务器),修改/etc/my.cnf ,配置server_id
[mysqld]
server-id=2
replicate-do-db=mytable
千万要注意,server-id不要和主服务器重复了,也不要和其他的从服务器重复了。
配置完成后,对从数据库服务进行重启
[root@hcss-ecs-dd6b ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
5 启动主从复制
在从数据库执行启动主从备份命令
[root@hcss-ecs-dd6b ~]# 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.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.0.101',
-> master_port=3306,
-> master_user='slave',
-> master_password='DBslave123',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=96301;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
到这里,该做的配置已经做完了。然后我们看看状态,通过状态中的 Slave_IO_Running和 Slave_SQL_Running来确认是否已经正常运行了。
查看状态的命令为: show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.101
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 326740
Relay_Log_File: hcss-ecs-dd6b-relay-bin.000002
Relay_Log_Pos: 765
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1032
Last_Error: Could not execute Delete_rows event on table mydb.mytable; Can't find record in 'mytable', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000001, end_log_pos 98170
Skip_Counter: 0
Exec_Master_Log_Pos: 96746
Relay_Log_Space: 230974
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: 0
Last_IO_Error:
Last_SQL_Errno: 1032
Last_SQL_Error: Could not execute Delete_rows event on table mydb.mytable; Can't find record in 'mytable', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000001, end_log_pos 98170
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: d14c3edd-8fc7-11ef-8a90-fa163ecb25fe
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 241116 22:46:16
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)
这里我们可以看到:Slave_SQL_Running参数的值是No,报错是说主数据库存在的记录在从数据库中无法找到。
按照网上的说法,设置一个参数就可以了,但我试了,结果是不行,设置方法如下:
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> set global sql_slave_skip_counter=1;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
为了确保数据同步完全没问题,我干脆把主数据库备份出来,再在导入到从数据库
接着按照第3步的方法,重新执行,一直到第5步
查看日志信息状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 590553 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
配置从数据库的同步信息
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.0.101',
-> master_port=3306,
-> master_user='slave',
-> master_password='DBslave123',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=590553;
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.0.101
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 619563
Relay_Log_File: hcss-ecs-dd6b-relay-bin.000002
Relay_Log_Pos: 29330
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: mymall-v1
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: 619563
Relay_Log_Space: 29545
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: d14c3edd-8fc7-11ef-8a90-fa163ecb25fe
Master_Info_File: /var/lib/mysql/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)
到这,我们看到两个状态参数,都是YES,说明主从备份生效了。我也修改了一个无关紧要的表数据,也确实同步到了从数据库,说明这样是主从同步了。
这个中间遇到了一些问题,我也没时间去详细了解到底怎么去解决。简单弄了一下,也没搞定。但从报错分析来看,就是数据不一致引起的问题,如果非得要解决的话,可能还得再深入研究一下MYSQL主从复制的原理,再去做相应的调整修改。目前笔者还没那掌握这门技术,暂时也不深入探讨了。有兴趣的朋友可以多做些尝试,笔者时间有限,暂时弄到这。
结束语
万事开头难,我也是第一次做主从配置,一开始,我也怀疑我是否真的能搞定,前面看理论文章的时候,看到这些命令,似乎不像是MYSQL的写法,也感觉有些不确定性,不知道是否真正执行起来是否能顺利。
通过操作实践,再结合网上的文章讲解,我也明白了每个过程具体应该怎么弄。比如说change master to 那段指令,如果不换行的话,执行不起来,会报错,换行的话,就能执行成功,包括有些授权的指令,有时候没写对,也会报错。replicate-do-db 刚开始看成了replicate-to-db,总是重启不成功。等等,也通过这些错误,加深了对这些操作的认识。
最近观察微信公众号发文已经到了30+,阅读量峰值到了160+ 点赞转发虽然不多,但也总比没有的好。这些成绩就是我们前进的动力! 欢迎大家多多关注和支持! 我们一直会保持不定期更新,绝对不会放弃,让我们都遇到更好的自己!