一、服务器准备
两台虚拟机:centos7
ip1:172.19.230.102
ip2:172.19.230.104
二、两台主机安装mysql
2.1 安装
#下载并安装 yum repostory
wget -i -c https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
yum -y install mysql80-community-release-el7-1.noarch.rpm
#Mysql的GPG升级了,需要重新获取
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
#yum安装mysql
yum -y install mysql-community-server
#lower_case_table_names=1
https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
2.2 mysql配置
#忽略大小写
#vi /etc/my.cnf
#添加 lower_case_table_names=1
#启动mysql
systemctl start mysqld.service
#查看mysql状态
systemctl status mysqld.service
#查看临时密码
grep "password" /var/log/mysqld.log
#登录mysql
mysql -uroot -p
#修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Autoai@123';
#修改密码级别(测试环境)
mysql> set global validate_password.policy=LOW; #mysql8.0
set global validate_password_policy=0; #mysql5.7
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'autoai123';
#开放外部访问
mysql> use mysql;
mysql> update user set host='%' where user='root';
mysql> flush privileges;
#修改mysql数据目录
mkdir -p /home/data/mysql-8.0.29
mv /var/lib/mysql /home/data/mysql-8.0.29/
ln -s /home/data/mysql-8.0.29/mysql/ /var/lib/
参考链接: Centos7搭建Mysql8.0.29集群:一主多备_wget mysql 8.0.29_wangzhongyudie的博客-CSDN博客
注意重启服务
#解决权限目录问题
cd /var/lib
setenforce 0 #临时
#重启
systemctl restart mysqld
三、配置主从节点
3.1 主节点配置
3.1.1 修改文件/etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
#开启binlog日志
log-bin=mysql-bin
server-id=201
#默认的认证方式改为nativate,兼容mysql5.7
default-authentication-plugin=mysql_native_password
#跳过SSL
skip_ssl
#数据库存放目录
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#创建函数时防止报错
log_bin_trust_function_creators=1
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#设置数据库表名称大小写不敏感
lower_case_table_names = 1
重启mysql服务
systemctl stop firewalld.service
systemctl disable firewalld.service
3.1.2 创建主从复制数据的用户,用于slave从master复制数据
mysql> create user 'copy'@'%' identified by 'autoai@copy';
Query OK, 0 rows affected (0.02 sec)
mysql> grant all privileges on *.* to 'copy'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.1.3 重置主节点,记录binlog的偏移量
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 157 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3.2 从节点配置
3.2.1 修改文件/etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
server-id=202
#默认的认证方式改为nativate,兼容mysql5.7
default-authentication-plugin=mysql_native_password
#跳过SSL
skip_ssl
#数据库存放目录
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
#创建函数时防止报错
log_bin_trust_function_creators=1
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
#设置数据库表名称大小写不敏感
lower_case_table_names = 1
Readonly=1
重启mysql服务
systemctl stop firewalld.service
systemctl disable firewalld.service
3.2.2 注册从节点
mysql> stop slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> reset slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='172.19.230.102',master_user='copy',master_port=3306,master_password='autoai@copy',master_log_file='mysql-bin.000001',master_log_pos=157;
Query OK, 0 rows affected, 9 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: k8s-node7
Master_User: copy
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 157
Relay_Log_File: k8s-node8-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_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: 157
Relay_Log_Space: 540
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: 201
Master_UUID: 98e64dfe-e6e3-11ec-b131-f01fafd2f4c3
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica 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:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)
四、结果
第一启动失败
通过查看主节点日志cat /var/log/mysqld.log
显示ip解析错误,猜测解析估计是DNS的问题,本地网络使用应该是需要禁用就可以
进入/etc 找到mysql的配置文件my.cnf
[mysqld]下面增加skip-name-resolve
重启从节点,果然成功!
验证一下:创建test库,从库果然可以同步,搞定!
常用命令备注
#修改密码
ALTER USER 'project'@'%' IDENTIFIED WITH mysql_native_password BY 'autoai123';
flush privileges;
#授权
grant select, insert, update, delete on an_admin.* to project@'%';
grant select, insert, update, delete on an_base.* to project@'%';