[svc]打通mysql主从同步

时间:2023-01-22 16:03:32

MySQL数据库设置主从同步

主从同步的特点:

逻辑的

异步的

主从同步的作用

1.备份

2.读写分离

主从同步的步骤

[svc]打通mysql主从同步

  • 准备环境

    1, mysql版本一致 5.5以上

    2, 主从数据库一致

主库master修改

1.修改my.cnf

# 打开主库binlog
log-bin = mysql-bin # 主数据库端ID号(区别于从库)
server-id = 1

2.重启mysql,创建用于同步的账号

grant replication slave on *.* to 'rep'@'%' identified by '123456'; 

flush privileges;

查看master状态

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000009 | 196 | | |
+------------------+----------+--------------+------------------+
1 row in set

注:执行完这个步骤后不要再操作主数据库了,防止主数据库状态值变化

从库slave的修改

1.修改mysql配置

# 从数据库端ID号
server-id =2

执行同步命令

# 执行同步命令,设置主数据库ip,同步帐号密码,同步位置
mysql>change master to master_host='192.168.1.2',master_user='rep',master_password='123456',master_log_file='mysql-bin.000009',master_log_pos=196; # 开启同步功能
mysql>start slave;

检查从库的状态

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.2
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 196
Relay_Log_File: vicky-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:

注:Slave_IO_Running及Slave_SQL_Running进程必须正常运行,即YES状态,否则说明同步失败。

到这里,主从数据库设置工作已经完成,自己可以新建数据库和表,插入和修改数据,测试一下是否成功

其他可能用到的相关参数

master端

# 不同步哪些数据库
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema # 只同步哪些数据库,除此之外,其他不同步
binlog-do-db = game # 日志保留时间
expire_logs_days = 10 # 控制binlog的写入频率。每执行多少次事务写入一次
# 这个参数性能消耗很大,但可减小MySQL崩溃造成的损失
sync_binlog = 5 # 日志格式,建议mixed
# statement 保存SQL语句
# row 保存影响记录数据
# mixed 前面两种的结合
binlog_format = mixed

slave端

# 停止主从同步
mysql> stop slave; # 连接断开时,重新连接超时时间
mysql> change master to master_connect_retry=50; # 开启主从同步
mysql> start slave;

以上连接超时设置,类似方式可用于设置主数据库ip,同步帐号密码,同步位置

拓展阅读:MySQL日志格式 binlog_format

[svc]打通mysql主从同步

binlog的常用功能

常用功能 语句
查看bin-log开关 show variables like "%log_bin%";
查看log格式 show variables like "%format%";
查看binlog show binary log;
刷新binlog reset master;
- -
查看版本 \s;
查看引擎 show variables like "%engine%";
查看字符集 show variables like "%char%";
查看slowlog show variables like "%slow%";
- -
建库 create database student;
建表 create table stu(id int,name char(40));
插入数据 insert into stu values (1,'maotai');
查看表字段 desc stu;
查看建表语句 show create table stu;
查询 select user,host from mysql.user;
查看错误日志 show variables like '%error%';
最大连接数(151默认) show variables like "%max_conn%";
在mysql>执行sh命令 system ls /opt
- 查看logbin开关
mysql> show variables like "%log_bin%";
+---------------------------------+---------------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------------+
| log_bin | ON |
| log_bin_basename | /usr/local/mysql/data/mysql-bin |
| log_bin_index | /usr/local/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+---------------------------------------+
6 rows in set (0.01 sec) - 查看日志格式
mysql> show variables like "%format%";
+--------------------------+-------------------+
| Variable_name | Value |
+--------------------------+-------------------+
| binlog_format | STATEMENT |
| date_format | %Y-%m-%d |
| datetime_format | %Y-%m-%d %H:%i:%s |
| default_week_format | 0 |
| innodb_file_format | Antelope |
| innodb_file_format_check | ON |
| innodb_file_format_max | Antelope |
| time_format | %H:%i:%s |
+--------------------------+-------------------+
8 rows in set (0.00 sec) - 查看二进制日志
mysql> show binary log;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'log' at line 1
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 120 |
+------------------+-----------+
1 row in set (0.00 sec) - 切割日志
mysql> reset master;
Query OK, 0 rows affected (0.09 sec) mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)