mysql8.0.30主从配置

安装包下载地址:

https://downloads.mysql.com/archives/community/

1、 解压介质包:

# tar xf mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz
# mv mysql-8.0.30-linux-glibc2.12-x86_64 mysql
# mkdir mysql/{data,logs}

2、 创建mysql用户,并对目录授权

# groupadd mysql
# useradd -M -s /sbin/nologin -g mysql mysql
# chown -R mysql.mysql mysql

3、编辑主从节点配置文件

以下为我的服务配置,可参考

master my.cnf

[mysql]
default-character-set=utf8mb4
prompt=[\\u@\\h][\\d]>\\_

[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/data/mysql
datadir=/data/mysql/data
log-error=/data/mysql/data/mysql.err
socket=/tmp/mysql.sock
pid-file=/data/mysql/data/mysql.pid

server-id=1

innodb_buffer_pool_size = 8G    #通常建议将其设置为总可用内存的 50% 到 80%
innodb_log_file_size = 2G
max_allowed_packet=100M
transaction_isolation=READ-COMMITTED
skip-name-resolve

slow_query_log=ON
slow_query_log_file=/data/mysql/data/slow.log
long_query_time=1

gtid_mode=ON
enforce_gtid_consistency=ON
binlog-checksum = CRC32
master-verify-checksum = 1

binlog_format=row
expire_logs_days=7
max_binlog_size=512M
log-bin=/data/mysql/logs/mysql-bin
log_bin_index=/data/mysql/logs/mysql-bin.index
log-slave-updates = ON

plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled=1
rpl-semi-sync-slave-enabled=1

character_set_server = utf8mb4
collation_server = utf8mb4_general_ci
default_authentication_plugin=mysql_native_password
lower_case_table_names=1

slave my.cnf

[mysql]
default-character-set=utf8mb4
prompt=[\\u@\\h][\\d]>\\_

[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/data/mysql
datadir=/data/mysql/data/
log-error=/data/mysql/data/mysql.err
socket=/tmp/mysql.sock
pid-file=/data/mysql/data/mysql.pid

server-id=15

innodb_buffer_pool_size = 8G    #通常建议将其设置为总可用内存的 50% 到 80%
innodb_log_file_size = 2G
max_allowed_packet=100M
transaction_isolation=READ-COMMITTED
skip-name-resolve

slow_query_log=ON
slow_query_log_file=/data/mysql/data/slow.log
long_query_time=1

gtid_mode = ON
enforce_gtid_consistency = ON
log-slave-updates = ON
skip-slave-start = true
expire_logs_days = 7
max_binlog_size  = 512M
read_only = ON

log-bin=/data/mysql/logs/mysql-bin
log_bin_index = /data/mysql/logs/mysql-bin.index
relay-log = /data/mysql/logs/relay-log
relay-log-index = /data/mysql/logs/relay-log-index
relay-log-info-file = /data/mysql/logs/relay-log.info

master-info-repository = table
relay-log-info-repository = table
#symbolic-links=0

plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl-semi-sync-master-enabled=1
rpl-semi-sync-slave-enabled=1

character_set_server = utf8mb4
collation_server = utf8mb4_general_ci
default_authentication_plugin=mysql_native_password
lower_case_table_names=1

保存退出后记得修改权限

# chown mysql.mysql *

4、 初始化数据库

# cd /data/mysql
# ./bin/mysqld --defaults-file=/data/mysql/my.cnf --basedir=/data/mysql --datadir=/data/mysql/data --lower_case_table_names=1 --user=mysql --initialize

--lower_case_table_names=1   不区分大小写 MySQL8需要初始化时添加,初始化不添加的话,后期在配置文件中添加重启会报错

PS:mysql.err 日志中有初始化数据库密码

5、数据库启动

# ./bin/mysqld_safe --defaults-file=/data/mysql/my.cnf &

6、登录数据库修改初始密码,创建root@%账号并授权

mysql> alter user 'root'@'localhost' identified by 'Root~123';
mysql> create user 'root'@'%' identified WITH mysql_native_password by 'Root~123';
mysql> grant all privileges on *.* to 'root'@'%' with grant option;

7、 创建数据库同步账号并授权

mysql> create user 'repl'@'%' identified with mysql_native_password by 'repl';
mysql> grant replication slave on *.* to 'repl'@'%';
mysql> flush privileges;

8、配置主从同步,从库安装方法同上

PS:从库无需创建数据库root账号,会主动从主库同步

基于 gtid 同步——推荐

mysql> CHANGE MASTER TO MASTER_HOST='73.134.64.72',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_AUTO_POSITION=1;
mysql> start slave;
mysql> show slave status\G;

基于 binlog 方式同步

登录主库查询Master状态,并记录 File 和 Position 的值:

mysql> show master status;

登录从库操作:

mysql> CHANGE MASTER TO MASTER_HOST='73.134.64.72',MASTER_PORT=3306,MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=2229;
mysql> start slave;
mysql> show slave status\G;

查询主从状态(参数 Slave_IO_Running 和 Slave_SQL_Running 是否都为yes,则证明配置成功。若为no,则需要查看对应的 Last_IO_Error 或 Last_SQL_Error 的异常值)

PS:MySQL提示符配置,8.0配置文件中prompt 上述方法未生效,需配置环境变量:export MYSQL_PS1="[\u@\h][\d]> ",效果如下

 

posted @ 2023-01-16 15:44  太阳的阳ฅ  阅读(97)  评论(0编辑  收藏  举报