hj_mysql主从配置

发布时间 2023-06-06 10:10:41作者: 独孤~华剑

一主一从 mysql8.0.32版本;

podman pull mysql:8.0.32
# 创建对应目录,配置my.cnf文件.文件示例在后面~
# 然后启动容器
podman run -d --privileged=true \
--name mysql_8.0.32_3308 -p 3308:3308 \
-v /hj_files/mysql8.0.32_3308/data:/var/lib/mysql \
-v /hj_files/mysql8.0.32_3308/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 21a7ac543605
# 进入容器 登录mysql,配置账户
podman exec -it 容器ID /bin/bash
mysql -uroot -p
# 创建账户(主从复制专用账号~)
create user 'hjcast'@'%' identified with mysql_native_password by 'hjcast123456';
# 授予主从复制权限
grant replication slave on *.* to 'hjcast'@'%';
# 查看maser信息
 show master status;
# 看到的示例内容如下:
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      157 | hj_boot      | mysql,lingshi    |                   |
+------------------+----------+--------------+------------------+-------------------+


# 配置从库my.cnf文件 启动容器
podman run -d --privileged=true \
--name mysql_8.0.32_3309 -p 3309:3309 \
-v /hj_files/mysql8.0.32_3309/data:/var/lib/mysql \
-v /hj_files/mysql8.0.32_3309/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=123456 21a7ac543605
# 进入容器,登录mysql,配置主库信息
change replication source to source_host='101.33.250.220',source_port=3308,source_user='hjcast',source_password='hjcast123456',source_log_file='mysql-bin.000004',source_log_pos=157;
# 启动slave
start slave;
stop slave; #停止
show slave status\G; # 查看信息
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 101.33.250.220
                  Master_User: hjcast
                  Master_Port: 3308
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 157
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
........

master下的my.cnf配置文件示例如下:

# 修改后的配置文件
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html

[mysqld]
# 配置主从复制,当下这个作为master库
# 数据库唯一ID,确保唯一
server-id=1
# 是否只读,1代表只读 0代表读写
read-only=0
# 开启mysql的binlog日志功能
log-bin=mysql-bin
 #控制数据库的binlog刷到磁盘上去,0 不控制,性能最好,1 每次事物提交都会刷到日志文件中,性能最差,最安全
sync_binlog= 1
#binlog日志格式,mysql默认采用statement,建议使用mixed
binlog_format= mixed
 #binlog过期清理时间
expire_logs_days= 7
 #binlog每个日志文件大小                        
max_binlog_size= 100m
#binlog缓存大小             
binlog_cache_size= 4m    

#需要同步的数据库 这个可不配置
binlog-do-db= hj_boot

#最大binlog缓存大              
max_binlog_cache_size= 512m
#不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制这句话,写多行
binlog-ignore-db=mysql
#不需要同步的数据库
binlog-ignore-db=lingshi
# 自增值的偏移量
auto-increment-offset= 1
# 自增值的自增量  
auto-increment-increment= 1
#跳过从库错误
slave-skip-errors= all

#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
# 服务端使用的字符集默认为8比特编码的latin1字符集,改为utf8mb4
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
# 设置3308端口 默认是3306
port=3308
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid

# 允许最大连接数
max_connections=20
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[client]
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/
master--my.cnf

slave下的my.cnf配置文件示例如下:

# 修改后的配置文件
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
# 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= 2
# 只读,这个超管还是可读可写的 super-read-only=1 就都只能读
read-only=1
log-bin=mysql-bin
# 从主数据库同步的binlog会写入到该目录下
relay-log= mysql-relay-bin
#如果主从数据库名称不同
#replication-rewrite-db= 主数据库名 -> 从数据库名
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
# 服务端使用的字符集默认为8比特编码的latin1字符集,改为utf8mb4
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
# 设置3309端口 默认是3306
port=3309
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid

# 允许最大连接数
max_connections=20
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[client]
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/
View Code

好吧~一主一从 8.0.32版本大致到这啦~