《MySQL系列-主从相关》Windows生产服务器和Linux备份服务器实现“主从备份功能“

Posted DATA数据猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《MySQL系列-主从相关》Windows生产服务器和Linux备份服务器实现“主从备份功能“相关的知识,希望对你有一定的参考价值。

Windows生产服务器和Linux备份服务器实现"主从备份功能"

经测试,Windows服务器和Linux服务器是可以实现主从备份的。为了实现对Windows数据库的备份功能,而目前只有Linux服务器了,所以在Linux服务器上部署从库,实现主从备份。

一、mysql从库安装

主库版本

C:\\Users\\admininster>mysql -V
mysql  Ver 14.14 Distrib 5.7.36, for Win64 (x86_64)

从库安装

1.准备操作

1.1下载指定版本

CSDN资源下载

1.2 检查是否安装

rpm -qa|grep mariadb
rpm -qa|grep -i mysql

1.3 验证MySQL安装包

# 上传到服务器上
[root@kafka01 ~]# ls | grep mysql
mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

# md5sum验证安装包是否与官网一致
[root@kafka01 ~]# md5sum mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
1748ec2c8b5ca1bcf8ba3b1f5e956139  mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

1.4 解压到指定目录

[root@kafka01 ~]# tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

[root@kafka01 local]# mv mysql-5.7.36-linux-glibc2.12-x86_64/ mysql-5.7.36

2.MySQL相关准备

2.1 创建用户及用户组

# 1.创建MySQL的group
[root@kafka01 local]# groupadd mysql
# 2.在MySQL组下创建一个MySQL用户
[root@kafka01 local]# useradd -r -g mysql mysql

2.2 创建数据目录

[root@kafka01 local]# cd mysql-5.7.36/
[root@kafka01 mysql-5.7.36]# mkdir data
[root@kafka01 mysql-5.7.36]# chown mysql:mysql ./data/

2.3 修改my.cnf

[root@kafka01 data]# vim /etc/my.cnf

[mysqld]
port=33060
user=mysql
basedir=/usr/local/mysql-5.7.36
datadir=/usr/local/mysql-5.7.36/data/
socket=/tmp/mysql.sock
log-error=/usr/local/mysql-5.7.36/data/mysql.err
pid-file=/usr/local/mysql-5.7.36/data/mysql.pid

symbolic-links=0
lower_case_table_names=1

3. 初始化MySQL

3.1 初始化

[root@kafka01 bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql-5.7.36/ --datadir=/usr/local/mysql-5.7.36/data/ --user=mysql --initialize

3.2 查看初始密码

root@localhost: CHAgltyCO0?>

[root@kafka01 bin]# cat /usr/local/mysql-5.7.36/data/mysql.err
......
root@localhost: CHAgltyCO0?>

3.3 复制mysql.server文件

[root@kafka01 bin]# cp /usr/local/mysql-5.7.36/support-files/mysql.server /etc/init.d/mysql

# 如果不执行这一步,会提示
[root@kafka01 bin]# service mysql start
Redirecting to /bin/systemctl start mysql.service
Failed to start mysql.service: Unit mysql.service not found.

4.启动MySQL

4.1 启动MySQL

[root@kafka01 bin]# service mysql start
Starting MySQL. SUCCESS!

4.2 登录

MySQL启动异常:error while loading shared libraries: libncurses.so.5: cannot open shared…

[root@kafka01 bin]# ./mysql -uroot -p
./mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

[root@kafka01 bin]# find / -name 'libncurses*'
/usr/lib/libncursesw.so.5.9
/usr/lib/libncurses++.so.5
/usr/lib/libncurses++.so.5.9
/usr/lib/libncurses++w.so.5
/usr/lib/libncurses++w.so.5.9
/usr/lib/libncurses.so.5
/usr/lib/libncurses.so.5.9
/usr/lib/libncursesw.so.5
/usr/lib64/libncurses.so.6
/usr/lib64/libncurses.so.6.1
/usr/lib64/libncursesw.so.6
/usr/lib64/libncursesw.so.6.1

[root@kafka01 bin]# cp /usr/lib64/libncurses.so.6 /usr/lib64/libncurses.so.5

4.3 修改密码

[root@kafka01 bin]# ./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 6
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, 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>

mysql> set password='Zxy@20230306';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

5.添加系统命令

添加系统命令后,可以在系统任意地方使用mysql命令登录

[root@kafka01 mysql-5.7.36]# chmod +x /etc/init.d/mysql
[root@kafka01 mysql-5.7.36]# chkconfig --add mysql
[root@kafka01 mysql-5.7.36]# ln -s /usr/local/mysql-5.7.36/bin/mysql /usr/bin

6.远程登陆

6.1 远程授权


mysql> grant all privileges on *.* to 'root'@'%' IDENTIFIED BY 'Zxy@20230306' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

6.2 远程登陆

二、MySQL主从复制

1 Windows主库开启binlog

1.1 查看数据目录(Windows主库)

mysql> show variables like 'datadir';
+---------------+---------------------------------------------+
| Variable_name | Value                                       |
+---------------+---------------------------------------------+
| datadir       | C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Data\\ |
+---------------+---------------------------------------------+
1 row in set, 1 warning (0.00 sec)

1.2 找到my.ini配置文件位置(Windows主库)

1.3 修改my.ini文件开启binlog(Windows主库)

log-bin = mysql-bin
# binlog格式
binlog_format = row
server-id = 1
# 每个表都开启一个表空间
innodb_file_per_table = ON
# 禁止dns解析
skip_name_resolve = ON
# 记录以下数据库binlog
binlog-do-db=db1
binlog-do-db=db2
binlog-do-db=db3
expire_logs_days=7

1.4 重启MySQL(Windows主库)

通过命令win + r,输入services.msc即可进入到如下页面,找到MySQL服务,重启即可

mysql> show variables like '%log_bin%';
+---------------------------------+-------------------------------+
| Variable_name                   | Value                         |
+---------------------------------+-------------------------------+
| log_bin                         | ON                            |
| log_bin_basename                | D:\\mysql\\data\\mysql-bin       |
| log_bin_index                   | D:\\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, 1 warning (0.00 sec)

2 全量同步一次数据

2.1 主库添加只读锁(Windows主库)

mysql> FLUSH TABLES WITH READ LOCK;
mysql> SET GLOBAL read_only = ON;

2.2 导出主库数据(Windows主库)

导出三个需要备份库的sql文件

C:\\ProgramData\\MySQL\\backup>mysqldump -uroot -p -h localhost --databases db1 db2 db2 > db1_db2_db3_2023-03-06.sql

将sql文件发送到目标服务器

scp db1_db2_db3_2023-03-06.sql root@112.41.113.128:/usr/local/mysql-5.7.36

2.3 从库导入数据(Linux从库)

mysql -uroot -p -h localhost < db1_db2_db3_2023-03-06.sql

3 主从配置

3.1 主库配置(Windows主库)

3.1.1 创建用户(Windows主库)

create user 'slave'@'localhost' IDENTIFIED by 'Zxy@20230306';

3.1.2 用户授权(Windows主库)

# replication slave:授予此权限,复制才能真正工作
# replication client:授予此权限,复制用户可以使用show master status,show slave status,show binary logs来确认复制状态
grant replication slave,replication client on *.* to 'slave'@'112.41.113.128' identified by 'Pd@20230306';

3.1.3 查看master信息(Windows主库)

# 通过show master status 查看MASTER_LOG_FILE,MASTER_LOG_POS等信息
mysql> show master status\\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 24510
     Binlog_Do_DB: db1,db2,d3
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

3.2 从库配置(Linux从库)

3.2.1 开启中继日志(Linux从库)

[root@kafka01 ~]# mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf
[root@kafka01 ~]# vim /etc/my.cnf
relay-log = relay-log
relay-log-index = relay-log.index
server-id = 2
innodb_file_per_table = ON
skip_name_resolve = ON

replicate_do_db = db1
replicate_do_db = db2
replicate_do_db = db3

master_info_repository  = table
relay_log_info_repository = table
#如果主从数据库名称相同
replication-do-db=数据库名
#如果主从数据库名称不同
replication-rewrite-db= 主数据库名 -> 从数据库名

3.2.2 重启从库(Linux从库)

[root@kafka01 ~]# service mysql restart

mysql> show variables like '%relay_log%';
+---------------------------+----------------------------------------------+
| Variable_name             | Value                                        |
+---------------------------+----------------------------------------------+
| max_relay_log_size        | 0                                            |
| relay_log                 | relay-log                                    |
| relay_log_basename        | /usr/local/mysql-5.7.36/data/relay-log       |
| relay_log_index           | /usr/local/mysql-5.7.36/data/relay-log.index |
| relay_log_info_file       | relay-log.info                               |
| relay_log_info_repository | TABLE                                        |
| relay_log_purge           | ON                                           |
| relay_log_recovery        | OFF                                          |
| relay_log_space_limit     | 0                                            |
| sync_relay_log            | 10000                                        |
| sync_relay_log_info       | 10000                                        |
+---------------------------+----------------------------------------------+
11 rows in set (0.00 sec)

3.2.3 设置master(Linux从库)

mysql> CHANGE MASTER TO MASTER_HOST='115.32.112.284' ,MASTER_PORT=3306,MASTER_USER='slave',MASTER_PASSWORD='Zxy@20230306',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=24510 for channel 'master_report';

4、启动主从

4.1 启动从库(Linux从库)

mysql> start slave

4.2 检查从库状态(Linux从库)

# 1.通过show slave status主要查看以下两个参数状态是否为Yes
mysql> show slave status\\G;

Slave_IO_Running: Yes/No
Slave_SQL_Running: Yes/No

# 2.如果以上两个参数为No,可以检查如下两个参数提示信息,并处理
Last_IO_Error:
Slave_SQL_Running_State:

4.3 关闭主库只读(Windows主库)

mysql> SET GLOBAL read_only = OFF;
mysql> UNLOCK TABLES;

5、查看主从连接

可以看到左侧从库的Read_master_Log_Pos和右侧master的Position是一致的

以上是关于《MySQL系列-主从相关》Windows生产服务器和Linux备份服务器实现“主从备份功能“的主要内容,如果未能解决你的问题,请参考以下文章

《MySQL系列-主从相关》Docker安装MySQL,实现主从复制

MySQL系列

生产环境配置mysql主从复制

mysql 之 主从加 keepalive 在生产系统应用(zabbix)

生产环境 MySQL主从复制(同步)

(5.9)mysql高可用系列——正常主从切换测试