安装CentOS 8
一 安装MySQL 8.0.29
1 查看是否有mariadb,并卸载
rpm -qa|grep mariadb
rpm -qa|grep MariaDB
rpm -qa|grep mysql
#若有就执行下方命令删除,注意全称
rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
2 下载安装包
wget https://cdn.mysql.com/Downloads/MySQL-8.0/mysql-8.0.29-1.el8.x86_64.rpm-bundle.tar
3 解压下载的MySQL的tar包
tar -xvf mysql-8.0.29-1.el8.x86_64.rpm-bundle.tar
4 执行以下命令安装
rpm -ivh *.rpm
5 解决MySQL的依赖
根据实际情况,解决对应的依赖问题
yum install -y perl-JSON
yum install -y openssl-devel
yum install -y perl-Test-Simple
yum install -y net-tools
6 安装成功后对MySQL数据库进行初始化及相关配置
mysqld --initialize
chown -R mysql:mysql /var/lib/mysql
systemctl start mysqld
systemctl enable mysqld
7 查看默认root登录密码
cat /var/log/mysqld.log | grep password
8 登录修改密码
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass1234';
9 允许远程访问
create user 'root'@'%' identified with mysql_native_password by 'pass1234';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
禁用防火墙,或者将MySQL的端口添加到防火墙允许的端口,才能远程访问
二、配置MySQL多实例
1 创建目录
用于存放多个MySQL的实例数据
mkdir -p mysql/mysql{1,2,3}/data
2 创建 my.cnf 配置文件以及mysql.sock文件
touch mysql{1,2,3}/my.cnf
touch /tmp/mysql331{1,2,3}.sock
3 各 my.cnf 配置文件内容
- 主MySQL的my.cnf配置文件
[mysqld]
port = 3311
server-id = 1
datadir = /root/mysql/mysql1/data
socket = /tmp/mysql3311.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
log_bin=mysql-bin
binlog-format=Row
- 从MySQL 1的my.cnf配置文件
[mysqld]
port = 3312
server-id = 2
datadir = /root/mysql/mysql2/data
socket = /tmp/mysql3312.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
log_bin=mysql-bin
binlog-format=Row
- 从MySQL 2的my.cnf配置文件
[mysqld]
port = 3313
server-id = 3
datadir = /root/mysql/mysql3/data
socket = /tmp/mysql3313.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
log_bin=mysql-bin
binlog-format=Row
4 初始化,1主2从分别执行初始化
mysqld --defaults-file=my.cnf --initialize-insecure
5 启动数据库
mysqld --defaults-file=my.cnf --user=root
6 另打开命令行窗口登录主库
mysql -uroot -h127.0.0.1 -P3311
7 创建从库的同步用户及相关权限配置
CREATE USER 'repl'@'%' IDENTIFIED with mysql_native_password BY 'pass1234';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
flush privileges;
8 查看主库状态
show master status;
9 分别打开新的命令行登录从库1、2
mysql -uroot -h127.0.0.1 -P3312
mysql -uroot -h127.0.0.1 -P3313
10 将这两个实例都设置为从库,并根据8的数据修改bin_log文件名及位置
CHANGE MASTER TO
MASTER_HOST='localhost',
MASTER_PORT = 3311,
MASTER_USER='repl',
MASTER_PASSWORD='pass1234',
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=156;
##这样就把主从配置好了,实现了1主2从的配置
附:相关命令
#查看状态
show master status\G;
show slave status\G;
#启动从库
start slave;
#停止复制
stop slave;
#清除slave配置
reset slave all;
#关闭binlog,主库执行,不同步到从库
set SQL_LOG_BIN=0;
#执行结束后,打开记录日志
set SQL_LOG_BIN=1;