传统主从配置
MySQL通过二进制文件写入和恢复数据
主服务器一定要打开二进制日志
必须两台服务器(或者是多个实例)
从服务器需要一次数据初始化
如果主从服务器都是新搭建的话,可以不做初始化
如果主服务器已经运行了很长时间了,可以通过备份将主库数据恢复到从库。
主库必须要有对从库复制请求的用户。
从库需要有relay-log设置,存放从主库传送过来的二进制日志 show variables like '%relay%';
在第一次的时候,从库需要change master to 去连接主库。
change master信息需要存放到 master.info 中 show variables like '%master_info%';
从库怎么知道,主库发生了新的变化?通过relay-log.info记录的已经应用过的relay-log信息。
在复制过程中涉及到的线程
从库会开启一个IO thread(线程),负责连接主库,请求binlog,接收binlog并写入relaylog。
从库会开启一个SQL thread(线程),负责执行relay-log中的事件。
主库会开启一个dump thrad(线程),负责响应从IO thread的请求。
主从部署环境
-
Master:192.168.1.128
-
Slave:192.168.1.132
-
Port:3306
#设置主机名查看ip地址
[root@node1 ~]# ip a | grep inet | grep ens33
inet 192.168.1.128/24 brd 192.168.1.255 scope global noprefixroute ens33
[root@node5 ~]# ip a | grep inet | grep ens33
inet 192.168.1.132/24 brd 192.168.1.255 scope global noprefixroute ens33
安装(主从执行)
yum install wget -y
wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
yum install mysql80-community-release-el7-7.noarch.rpm -y
yum -y install yum-utils
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
yum -y install mysql-community-server mysql
systemctl start mysqld
systemctl enable mysqld
查看密码登陆后修改密码
grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
修改密码策略
--设置首次密码查看规则名称 之后如果需要设置你想要的格式密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Rkun18.mysql';
mysql> select version();
+------------+
| version() |
+------------+
| 5.7.42-log |
+------------+
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
--通过设置类似于下方这样的格式去设置其他参数 获取需要的密码格式
--设置至少四位自定义密码
set global validate_password_policy=LOW;
set global validate_password_mixed_case_count=0;
set global validate_password_number_count=0;
set global validate_password_special_char_count=0;
set global validate_password_length=4;
--也可以直接关闭密码策略 这里不赘述
--修改一个自定义密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'admin';
--授权远程主机登录(可选)
GRANT ALL PRIVILEGES ON *.* TO 'slave'@'%';
FLUSH PRIVILEGES;
--
编辑配置文件 /etc/my.cnf
-
Master
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock default-storage-engine=INNODB symbolic-links=0 server-id=6 log-bin=/var/log/mysql/mysql-bin
-
Slave
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock default-storage-engine=INNODB symbolic-links=0 server-id=8 log-bin=/var/log/mysql/mysql-bin relay-log=/var/log/mysql/mysql-relay
mkdir /var/log/mysql
chown -R mysql:mysql /var/log/mysql/
创建主从同步账号
- 在主库创建一个专门用来复制的数据库用户,所有从库都用这个用户来连接主库,确保这个用户只有复制的权限
#查看mysql配置项
mysqld --verbose --help
[root@master ~]# mysql -uroot -p
Enter password:
CREATE USER 'slave'@'%' IDENTIFIED BY 'admin';
GRANT ALL PRIVILEGES ON *.* TO 'slave'@'%' IDENTIFIED BY 'admin' WITH GRANT OPTION;
FLUSH PRIVILEGES;
--
获取主库日志信息并生成主库数据镜像
--对主库上锁 停止修改
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.03 sec)
--获取主库日志信息 当前日志文件为 mysql-bin.000002 备份主库数据
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000012 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
[root@master ~]# mysqldump -u root -p'admin' --master-data --all-databases > master.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# ll | grep master
-rw-r--r--. 1 root root 888384 7月 3 11:29 master.sql
主库数据备份完毕后,释放主库锁,需要注意,在上锁这一段期间,无法对数据库进行写操作,如UPDATA,DELETE,INSERT
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
从库还原数据
-
将主库镜像拷贝到从库中,将数据还原到从库
[root@master ~]# scp master.sql 192.168.1.132:/root/ master.sql 100% 868KB 13.3MB/s 00:00
--从库恢复数据
[root@slave ~]# pwd
/root
[root@slave ~]# ll | grep master
-rw-r--r-- 1 root root 888384 7月 3 11:56 master.sql
[root@slave ~]# mysql -padmin
mysql> source master.sql;
从库配置同步
-在从库上建立复制关系,即从库指定主库的日志信息和链接信息
CHANGE MASTER TO
-> MASTER_HOST='192.168.1.128',
-> MASTER_PORT=3306,
-> MASTER_USER='slave',
-> MASTER_PASSWORD='admin',
-> MASTER_LOG_FILE='/var/log/mysql/mysql-bin.000012',
-> MASTER_LOG_POS=154;
从库启动复制进程
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.1.128
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000012
Slave_IO_Running: Yes --保证Running为Yes
Slave_SQL_Running: 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: 523
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: 6
Master_UUID: 06bc5ae2-1a0c-11ee-ae0a-000c2977e40f
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:
验证主从数据同步
[root@node1 ~]# mysql -padmin
mysql> create database mydb default character set =utf8;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON mydb.* TO 'slave'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
+--------------------+
-
查看从库数据库观察数据是否同步
[root@node5 ~]# mysql -padmin --同步成功 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.42-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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mysql | | performance_schema | | sys | +--------------------+