linux下MySQL的安装
本文是在公司服务器环境下部署mysql时所记录,以下所有的命令, 都是使用prouser用户执行的。
-
-
二进制版解压安装.
cd /opt/app tar -zxvf mysql-5.7.31-el7-x86_64.tar.gz ln -s /opt/app/mysql-5.7.31-el7-x86_64 /opt/app/mysql export PATH=/opt/app/mysql/bin:$PATH # 此命令可写在 .bash_profile中
创建实例目录
mkdir -p /opt/app/MYINSTANCES/3360/{logs,data,tmp} # /opt/app/MYINSTANCES目录下可以放多个实例目录 # 实例子目录以实例启用端口做区别 # logs目录存放实例相关日志 # data目录存放实例数据文件 # tmp目录存放实例启动是Unix socket文件
mysqld --initialize-insecure \ # 非安全初始化, 初始化后MySQL的root可无密码登录, 之后需求手动修改 --datadir=/opt/app/MYINSTANCES/3360/data # 实例数据目录 --basedir=/opt/app/mysql # mysql软件目录 --user=prouser # 运行实例账户
[mysqld] server_id = 180 # 同一个集群中的实例的server_id必须唯一 binlog_format = row # binlog格式化方式, 开启gtid主从复制的必要条件 expire_logs_days = 14 # binlog日志的过期天数 max_binlog_size = 300M # 单binlog日志最大值 gtid_mode =ON # 打开gtid模式 enforce_gtid_consistency = ON # 强制主从复制中, gtid同步 binlog_checksum = CRC32 # binlog检查算法 master_verify_checksum = 1 # 为每条sql加入校验值 log_bin = /opt/app/MYINSTANCES/3360/logs/3360-bin # binlog路径 log_bin_index = /opt/app/MYINSTANCES/3360/logs/3360-bin.index # binlog索引路径 log_slave_updates = ON # binlog更新时, 同步更新relay_log, 开启级联复制的必要选项. datadir = /opt/app/MYINSTANCES/3360/data # 数据目录 basedir = /opt/app/mysql # 软件目录 port = 3360 # 实例开始端口 socket = /opt/app/MYINSTANCES/3360/tmp/mysql.sock # 实例打开的Unix socket文件的路径 innodb_flush_log_at_trx_commit = 1 # MySQL双一原则之一, 数据变更后立即sync至硬盘 sync_binlog = 1 # MySQL双一原则之一, 数据变更后, 变更记录立即写入binlog
mysqld_safe --defaults-file=/opt/app/MYINSTANCES/3360//my.cnf > /opt/app/MYINSTANCES/3360/logs/sys.log 2>&1 &
mysql -S /opt/app/MYINSTANCES/3360/tmp/mysql.sock -uroot mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'XXXX'; 如果出现ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'%' 错误, 执行SQL查看: use mysql; select user,host from user; +---------------+----------------+ | user | host | +---------------+----------------+ | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+----------------+ 注意我的root,host是'localhost',将SQL修改为: mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'XXXX';
mysql -S /opt/app/MYINSTANCES/3360/tmp/mysql.sock -uroot -pxxxx mysql> grant replication slave on *.* TO 'repl'@'从服务器ip或ip段' identified by 'xxxxx';
主从备份
从实例安全及数据初始化
与主实例相同
[mysqld] server_id = 181 # server_id与主实例不同 binlog_format = row expire_logs_days = 14 max_binlog_size = 300M gtid_mode =ON enforce_gtid_consistency = ON binlog_checksum = CRC32 read_only = ON # 从实例设置为只读 slave_sql_verify_checksum = 1 # 从服务执行备份sql时实施校验 log_bin = /opt/app/MYINSTANCES/3360/logs/3360-bin log_bin_index = /opt/app/MYINSTANCES/3360/logs/3360-bin.index log_slave_updates = ON relay_log = /opt/app/MYINSTANCES/3360/logs/3360-relay # relay日志路径 relay_log_index = /opt/app/MYINSTANCES/3360/logs/3360-relay.index # relay日志索引路径 relay_log_info_file = /opt/app/MYINSTANCES/3360/logs/3360-relay.info # relay日志记录文件路径 master-info-repository = table # 主实例信息记录形式, 此处为记录在系统表中 relay-log-info-repository = table # relay日志记录复制信息形式, 此处为记录在系统表中. datadir = /opt/app/MYINSTANCES/3360/data basedir = /opt/app/mysql port = 3360 socket = /opt/app/MYINSTANCES/3360/tmp/mysql.sock innodb_flush_log_at_trx_commit = 1 sync_binlog = 1
mysqld_safe --defaults-file=/opt/app/MYINSTANCES/3360//my.cnf > /opt/app/MYINSTANCES/3360/logs/sys.log 2>&1 & mysql -S /opt/app/MYINSTANCES/3360/tmp/mysql.sock -uroot mysql> reset master; # 重置主从复制信息, 主要是为了重置gtid mysql> change master to master_host='xxxx', # 主实例IP master_port=3360, # 主实例端口 master_user='repl', # 之前在主实例创建的专门用于主从复制的账户 master_password='xxxx', # 主从复制账户密码 master_auto_position=1; # 开启gtid复制 mysql> start slave;
mysql> show slave status\G
输出中Slave_IO_Running和Slave_SQL_Running对应值必须都为YES, 否则说明主从复制有问题.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
GTID模式下, 复制状态异常处理
主从复制的原理其实就是从实例通过解析主实例的binlog, 获取执行sql, 而gtid即单事务的ID值.
当主从复制异常, 说明从实例在执行主实例的sql时出现了问题, 往往是由于在从库执行已经创建过的表之类的语句导致.
mysql> show slave status\G # 获取复制状态, 主要是为了获取gtid .... Retrieved_Gtid_Set: 4e5e7538-19b3-11eb-976d-005056bc9158:1-36 # 需要执行的gtid集合 Executed_Gtid_Set: 4e5e7538-19b3-11eb-976d-005056bc9158:1-35, # 已执行的gtid集合 .... # 从上面的信息可以知道36号gtid对应的sql执行出问题. mysql> stop slave; # 停止主从复制 mysql> begin; mysql> set gtid_next='4e5e7538-19b3-11eb-976d-005056bc9158:36'; mysql> commit; # 提交一个空事务, 该事务的gtid为需要跳过的值 mysql> set gtid_next='AUTOMATIC'; # 将gtid值重设为自动 mysql> start slave; # 开启主从复制