打开 MySQL 官网地址:https://dev.mysql.com/downloads/mysql/,选择面安装版本。
解压 mysql-8.0.22-macos10.15-x86_64.tar.gz 三份,分别命名文件夹为 master、slave1、slave2
在 master 文件夹下的 support-files 文件夹下面新建 my.conf 配置文件
[client]
default-character-set=utf8mb4
port=3307
[mysqld]
character-set-server=utf8mb4
port=3307
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
pid-file=/Library/WebServer/Documents/data_backup/logs/mysql/master/mysql.pid
basedir=/Library/WebServer/Documents/mysql/master
datadir=/Library/WebServer/Documents/mysql/master/data
socket=/Library/WebServer/Documents/data_backup/logs/mysql/master/mysql.sock
mysqlx_socket=/Library/WebServer/Documents/data_backup/logs/mysql/master/mysqlx.sock
log-bin=/Library/WebServer/Documents/data_backup/logs/mysql/master/mysql-bin
binlog_format=mixed
server-id=1
log-error = /Library/WebServer/Documents/data_backup/logs/mysql/master.log
#慢日志设置
slow_query_log=1
long_query_time=2
slow_query_log_file=/Library/WebServer/Documents/data_backup/logs/mysql/master/slow_query.log
[mysqldump]
[mysql]
default-character-set=utf8mb4
[myisamchk]
[mysqlhotcopy]
在 slave1 文件夹下的 support-files 文件夹下面新建 my.conf 配置文件
[client]
default-character-set=utf8mb4
port=3308
[mysqld]
character-set-server=utf8mb4
port=3308
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
pid-file=/Library/WebServer/Documents/data_backup/logs/mysql/slave1/mysql.pid
basedir=/Library/WebServer/Documents/mysql/slave1
datadir=/Library/WebServer/Documents/mysql/slave1/data
socket=/Library/WebServer/Documents/data_backup/logs/mysql/slave1/mysql.sock
mysqlx_socket=/Library/WebServer/Documents/data_backup/logs/mysql/slave1/mysqlx.sock
log-bin=/Library/WebServer/Documents/data_backup/logs/mysql/slave1/mysql-bin
binlog_format=mixed
server-id=2
log-error = /Library/WebServer/Documents/data_backup/logs/mysql/slave1.log
#慢日志设置
slow_query_log=1
long_query_time=2
slow_query_log_file=/Library/WebServer/Documents/data_backup/logs/mysql/slave1/slow_query.log
[mysqldump]
[mysql]
default-character-set=utf8mb4
[myisamchk]
[mysqlhotcopy]
MySQL8没有初始的data目录,需要在使用前进行初始化。
数据库初始化
mysqld --defaults-file=master/support-files/my.cnf --initialize
mysqld --defaults-file=slave1/support-files/my.cnf --initialize
启动数据库
mysqld --defaults-file=master/support-files/my.cnf
mysqld --defaults-file=slave1/support-files/my.cnf
数据库初始化后随机密码在日志文件中,需要使用该密码登录并修改密码。
alter user "root"@"localhost" identified by "你的新密码";
创建用户
# 进入主 master 库
./mysql -h 127.0.0.1 -uroot -p -P3307
# 创建用户(此处使用随机生成密码,需要记住密码)
# create user 'mengsen'@'%' identified by '123456';
create user 'mengsen'@'%' IDENTIFIED BY RANDOM PASSWORD;
# 授权远程同步
grant replication slave on *.* to 'mengsen'@'%';
# 保存刷新
flush privileges;
# 查看主库的状态
show master status\G;
查看主库的状态
从库连接主库
# 在从库使用刚才主库创建的账号,连接一次,看否账号可用(使用刚才创建账号对应的密码)
./mysql -h 127.0.0.1 -umeng -p -P3307
# 使用命令 (注意此处替换自己创建数据库账号,以及修改对应 master_log_file 名称以及偏移位置)
change master to master_host='127.0.0.1',master_port=3307,master_user='mengsen',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=156;
# 启动 slave1 同步
start slave;
# 查看同步状态
show salve status;
注意观察 Slave_IO_Running 和 Slave_SQL_Running 的状态值,只有都为 Yes 的时候才表明同步 ok,同理 Slave2 也是相同的操作步骤。