前期规划
|-------------|-----------|-----------------|------------------------|-------------------------------------------|
| IP地址 | 角色 | 系统版本 | 内核 | 软件包名称 |
| 192.168.2.3 | Mysql主服务器 | CentOS Stream 9 | 5.14.0- 381.el9.x86_64 | mysql-8.2.0-linux-glibc2.17-x86_64.tar.xz |
| 192.168.2.4 | Mysql从服务器 | CentOS Stream 9 | 5.14.0- 381.el9.x86_64 | mysql-8.2.0-linux-glibc2.17-x86_64.tar.xz |
部署
部署脚本,较为粗制,大家谨慎使用。
bash
<<ldp
服务器配置为初始化状态,并且系统为centos9系统。
执行该脚本的话首先需要将软件包放到/root下面,并且为二进制的tar xf mysql-8.2.0-linux-glibc2.17-x86_64.tar.xz包,并且本机器上面没有部署mysql
版权为LJP
转发请咨询2043565830@qq.com
ldp
echo `cd /root/`
echo `tar xf mysql-8.2.0-linux-glibc2.17-x86_64.tar.xz`
echo `ln -sv /root/mysql-8.2.0-linux-glibc2.17-x86_64 /usr/local/mysql`
echo `groupadd mysql && useradd mysql -g mysql -s /sbin/nologin -M`
echo `mkdir -pv /data/mysql/mysql3316/{data,logs,tmp,conf,run}`
echo `chown -R mysql. /data/mysql/`
echo `chown -R mysql. /data/mysql/ /usr/local/mysql/`
echo `cat >/data/mysql/mysql3316/conf/my3316.cnf<<ldp
[client]
port = 3316
socket = /data/mysql/mysql3316/run/mysql3316.sock
default-character-set=utf8mb4
[mysql]
disable-auto-rehash
#允许通过 TAB 键提示
default-character-set = utf8mb4
connect-timeout = 10
[mysqld]
user=mysql
server-id = 3316
port = 3316
socket = /data/mysql/mysql3316/run/mysql3316.sock
pid-file = /data/mysql/mysql3316/run/mysql3316.pid
basedir = /usr/local/mysql
datadir = /data/mysql/mysql3316/data
#bind_address = 10.10.10.11
autocommit = 1
default_authentication_plugin=mysql_native_password
character-set-server=utf8mb4
explicit_defaults_for_timestamp=true
lower_case_table_names=1
back_log=103
max_connections=10000
max_connect_errors=100000
table_open_cache=512
external-locking=FALSE
max_allowed_packet=32M
sort_buffer_size=2M
join_buffer_size=2M
thread_cache_size=51
transaction_isolation=READ-COMMITTED
tmp_table_size=96M
max_heap_table_size=96M
###***logs
long_query_time = 10
slow_query_log = 1
slow_query_log_file=/data/mysql/mysql3316/logs/my3316-slow.log
#log-warnings = 1
log_error_verbosity=3
log-error = /data/mysql/mysql3316/logs/mysql3316-error.log
log_output = FILE
#参数 log_output 指定了慢查询输出的格式,默认为 FILE
#log-queries-not-using-indexes
#log-slow-slave-statements
max_binlog_size = 1G
#max_relay_log_size = 1G
# replication
log-bin=/data/mysql/mysql3316/logs/mysql-bin
#server-id=1
#binlog_format= ROW
#gtid_mode = on
#enforce_gtid_consistency = 1
#log_slave_updates = 1
#master-info-repository=TABLE
#relay-log-info-repository=TABLE
# innodb storage engine parameters
innodb_buffer_pool_size=500M
innodb_data_file_path=ibdata1:100M:autoextend:max:2G #redo
#innodb_data_file_path=ibdata1:100M:autoextend:max:5G #redo
#innodb_data_file_path=ibdata1:100M:autoextend:max:5G #redo
innodb_temp_data_file_path = ibtemp1:100M:autoextend:max:1G
#innodb_temp_data_file_path = ibtemp1:100M:autoextend:max:10G
#innodb_file_io_threads=4
#默认的是 4
innodb_log_buffer_size=16M
innodb_log_file_size=256M #undo
innodb_log_files_in_group=2
innodb_lock_wait_timeout=50
innodb_file_per_table=1
#独立表空间
ldp`
echo `chown -R mysql. /usr/local/mysql/ /data/mysql/`
echo `cd /usr/local/mysql/`
echo `/usr/local/mysql/bin/mysqld \
--defaults-file=/data/mysql/mysql3316/conf/my3316.cnf \
--datadir=/data/mysql/mysql3316/data --user=mysql \
--initialize-insecure --console ; echo $?`
echo `ln -sv /usr/local/mysql/bin/mysql /usr/bin/`
echo `yum -y install ncurses-devel`
echo `ln -sv /usr/lib64/libncurses.so.6.2 /usr/lib64/libncurses.so.5`
echo `ln -sv /usr/lib64/libtinfo.so.6.2 /usr/lib64/libtinfo.so.5`
#执行完成之后,需要重新启动
#cd /usr/local/mysql/
#进入到目录里面启动
# /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql3316/conf/my3316.cnf &
#mysql -S /data/mysql/mysql3316/run/mysql3316.sock
部署完成之后设置变量方便管理
vim /etc/profile
启动
alias my3316start='cd /usr/local/mysql && ./bin/mysqld_safe --defaults-file=/data/mysql/mysql3316/conf/my3316.cnf &'
关闭
alias my3316stop='cd /usr/local/mysql && ./bin/mysqladmin -S /data/mysql/mysql3316/run/mysql3316.sock shutdown'
进入
alias my3316jq='mysql -S /data/mysql/mysql3316/run/mysql3316.sock'
刷新
source /etc/profile
修改配置文件
主从服务器都须修改
50行log-bin=/data/mysql/mysql3316/logs/mysql-bin
开启二进制日志
51行server-id= 主从配置必须不同,按照自己规划进行配置,这里不做要求
53行gtid_mode = on
开启gtid模式
54行enforce_gtid_consistency = 1
强制gtid一致性,开启后对特定的create table不被支持
55行的log_slave_updates = 1
从库binlog才会记录主库同步的操作日志
log-bin-index = /data/mysql/mysql3316/logs/mysql-bin.index
skip-slave-start = 1
跳过slave复制线程
注意:修改完成配置文件之后如果此时mysql服务器开启则,重启mysql服务器。
注意:如果之前mysql数据库有重要的数据的话,首先将数据库的文件 备份,之后再进行数据同步。
192.168.2.3主机配置
创建repl用户并且允许192.168.2网段的主机登录,密码为repl@123
create user repl@'192.168.2.%' identified with 'mysql_native_password' by 'repl@123';
授予repl用户使用192.168.2网段登录时,有数据同步的权限。
grant replication slave on *.* to ++++'repl'@'++++ ++++192.168.2.%++++ ++++';++++
192.168.2.4主机配置
change master to
master_host='192.168.2.3', #指向ip地址
master_port=3316, #指向该主机的mysql端口
master_user='repl', #使用的用户
master_password='repl@123', #使用的密码
master_auto_position=1;
在mysql从服务器上开启同步
start slave;
查看同步的状态
show slave status\G
如图显示 双yes
并且查看一下Seconds_Behind_Master字段的信息,如果显示为0,代表同步完成。