mysql 主从复制搭建
服务器配置表
机器 | ip | 操作系统 |
---|---|---|
主机 | 192.168.31.25 | 华为openEuler-22.03-LTS-SP3 |
从机 | 192.168.31.184 | 华为openEuler-22.03-LTS-SP3 |
从机 | 192.168.31.228 | 华为openEuler-22.03-LTS-SP3 |
1、在3台机器上安装独立的 mysql
1.1 创建myql文件夹用来存放mysql包
mkdir -p /opt/mysql
1.2 上传并解压 mysql.tar包
tar -zxf mysql-8.3.0-linux-glibc2.28-x86_64.tar.xz
1.3 重命名解压后的mysql文件夹
mv mysql-8.3.0-linux-glibc2.28-x86_64 mysql8.3
1.4 添加环境变量
添加Path环境变量,命令如下:export PATH=$PATH:/opt/mysql/mysql8.3/bin
1.5 创建用户组合用户
(创建一个用户组:mysql)groupadd mysql (创建一个系统用户:mysql并且指定用户组为mysql)useradd -r -g mysql mysql 不创建此用户安装过程会报错
1.6 添加数据目录
1、创建数据目录:mkdir -p /opt/mysql/mysql8.3/data
2、更改属主和数组:chown -R mysql:mysql /opt/mysql/mysql8.3/data
3、更该模式:chmod -R 750 /opt/mysql/mysql8.3/data
1.7 进入mysql8.3 并创建 my.cnf 文件 并写入以下内容
vi mysql8.3/my.cnf
[mysql]
# 默认字符集
default-character-set=utf8mb4
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
server-id=1
user=mysql
socket =/tmp/mysql.sock
# 安装目录
basedir=/opt/mysql/mysql8.3
# 数据存放目录
datadir=/opt/mysql/mysql8.3/data/mysql
log-bin=/opt/mysql/mysql8.3/data/mysql/mysql-bin
innodb_data_home_dir=/opt/mysql/mysql8.3/data/mysql
innodb_log_group_home_dir=/opt/mysql/mysql8.3/data/mysql
#日志及进程数据的存放目录
log-error =/opt/mysql/mysql8.3/data/mysql/mysql.log
pid-file =/opt/mysql/mysql8.3/data/mysql/mysql.pid
# 服务端使用的字符集默认为8比特编码
character-set-server=utf8mb4
lower_case_table_names=1
autocommit =1
##################以上要修改的########################
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
#query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
binlog_format=mixed
binlog_expire_logs_seconds =864000
# 创建新表时将使用的默认存储引擎
default_storage_engine = InnoDB
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 4M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
特别注意:以上配置中 server-id 3台机器不能一样
1.8 初始化MySQL
bin/mysqld --defaults-file=/opt/mysql/mysql8.3/my.cnf --basedir=/opt/mysql/mysql8.3 --datadir=/opt/mysql/mysql8.3/data/mysql --user=root --initialize-insecure
1.9 启动mysql
/opt/mysql/mysql8.3/bin/mysqld_safe --defaults-file=/opt/mysql/mysql8.3/my.cnf &
1.10 登录(首次免密)
/bin/mysql -u root --skip-password
1.11 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
1.12 刷新权限
FLUSH PRIVILEGES;
1.13 设置允许远程登录访问(需要登录到MySQL服务中进行设置)
mysql> use mysql
mysql> update user set user.Host='%'where user.User='root';
mysql> flush privileges;
mysql> quit
2、设置主从复制
2.1 修改主从机器的 my.cnf 配置文件 并添加以下内容,若之前配置有则可以忽略
vi /opt/mysql/mysql8.3/my.cnf
#主节点
## 开启二进制日志并指定 server-id
log-bin=mysql-bin
binlog_format=mixed
server-id=1
vi /opt/mysql/mysql8.3/my.cnf
#从节点
## 开启中继日志并指定 server-id 和 只读
# log-bin=mysql-bin
# binlog_format = mixed
relay-log=mysql-relay-bin
server-id=2
read-only=on
2.2 重启mysql
service mysql restart
或者
ps aux | grep mysql
找到进程id
kill -9 pid
然后重新启动mysql
/opt/mysql/mysql8.3/bin/mysqld_safe --defaults-file=/opt/mysql/mysql8.3/my.cnf &
2.3 设置主从主节点配置
2.3.1 修改 my.cnf 配置文件
vi my.cnf
#若之前有配置且一样 现在不用配置
## 开启二进制日志并指定 server-id
log-bin=mysql-bin
binlog_format=mixed
server-id=1
2.3.2 重启 mysql
service mysql restart
或
ps aux | grep mysql 查询mysql 进程pid 然后 kill -9 pid
然后运行
/opt/mysql/mysql8.3/bin/mysqld_safe --defaults-file=/opt/mysql/mysql8.3/my.cnf &
2.3.3 创建具有复制权限的用户 ,此处 Mysql 5.7 和 Mysql 8.0 有些区别
========================================= Mysql 5.7 ============================================
## 使用 root 账户,其他账户会有权限不足的情况
mysql -uroot -p
## 这句话意思是,主机允许 slave 用户,通过密码 123456,在 192.168.1.2 这台服务器上访问当前这台 master
mysql> grant replication slave on *.* to slave@192.168.1.2 identified by '123456';
## 刷新
flush privileges;
===============================================================================================
========================================= Mysql 8.0 ============================================
## 使用 root 账户,其他账户会有权限不足的情况
mysql -uroot -p
## mysql8.0 授权用户需要先创建,创建和授权同一条语句的话会报错
mysql> create user 'slave'@'192.168.31.184' identified by '123456';
mysql> create user 'slave'@'192.168.31.228' identified by '123456';
## 再授权
mysql> grant replication slave on *.* to 'slave'@'192.168.31.184' with grant option;
mysql> grant replication slave on *.* to 'slave'@'192.168.31.228' with grant option;
## 刷新
flush privileges;
===============================================================================================
2.3.4 查看 master 信息
mysql> show master status;
2.4 从机配置(2台从机完全一样特别注意server-id 3台机器都不能一样)
2.4.1 修改 my.cnf 配置文件
vim my.cnf
## 开启中继日志并指定 server-id 和 只读
# log-bin=mysql-bin
# binlog_format = mixed
relay-log=mysql-relay-bin
server-id=2
read-only=on
2.4.2 重启 mysql
service mysql restart
或
ps aux | grep mysql 查询mysql 进程pid 然后 kill -9 pid
然后运行
/opt/mysql/mysql8.3/bin/mysqld_safe --defaults-file=/opt/mysql/mysql8.3/my.cnf &
2.4.3 让从机成为 Slave
mysql -uroot -p
mysql> stop slave;
## 设置从机监听主机:master_log_file 和 master_log_pos 与主机 master 信息一致 与上图中的信息一致
mysql> change master to master_host='192.168.31.25',master_user='slave',master_password='123456',master_log_file='mysql-bin.000005', master_log_pos=3150;
mysql> start slave;
2.4.4 查看 Slave 状态
mysql> show slave status\G;
## 等待主机发送事件
Slave_IO_State : Waiting for master to send event
## 若它为 No,检查下主机防火墙是否已关闭
Slave_IO_Running: Yes
## 这俩项都是 yes 则配置成功
Slave_SQL_Running: Yes
2.5 踩坑
上图中 io 状态为 no,查看mysql日志
登录mysql后
SHOW VARIABLES LIKE '%log_error%';
开日志并找到原因
大模型给出的解释是
简单的理解就是要在 master 节点设置以下信息
ALTER USER 'slave'@'192.168.31.184' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'slave'@'192.168.31.228' IDENTIFIED WITH mysql_native_password BY '123456';
然后重复 2.4.2 和 2.4.3 这2步 之后恢复正常