虚拟机安装 Linux

参考文章{{VMware 安装 Linux 虚拟机}}

Linux 初始化及安装 MySQL

下载 mysql

  1. wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar

在虚拟机 vmware 上面创建 linux centos8 安装 mysql5.7 的 rpm 压缩包 mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar

  1. #安装git
  2. 3 yum install git
  3. 4 yum list | grep java
  4. #安装java
  5. 5 yum install -y java-11-openjdk.x86_64
  6. 6 mysql
  7. # 安装rz 、sz 上传下载
  8. 10 yum -y install lrzsz
  9. 15 mv mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar software/mysql/
  10. #解压 tar包
  11. 31 tar -xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
  12. #查看是否有mariadb,有则移除
  13. 33 rpm -qa | grep mariadb
  14. #rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
  15. #开始安装mysql 按照顺序
  16. 34 rpm -ivh mysql-community-common-5.7.28-1.el7.x86_64.rpm
  17. 35 rpm -ivh mysql-community-libs-5.7.28-1.el7.x86_64.rpm
  18. 36 rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm
  19. 37 #rpm -ivh mysql-community-libs-compat-5.7.28-1.el7.x86_64.rpm --nodeps --force
  20. 38 rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm
  21. 39 #rpm -ivh mysql-community-client-5.7.28-1.el7.x86_64.rpm --nodeps --force
  22. #这一步应该放在最前面
  23. 40 #dnf install ncurses-compat-libs
  24. #yum install net-tools -y
  25. 41 rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
  26. 42 rpm -ivh mysql-community-devel-5.7.28-1.el7.x86_64.rpm
  27. # mysql初始化,并新建一个用户
  28. 44 mysqld --initialize --user=mysql
  29. #查看mysql的临时密码# (HkA05W=i-Fl
  30. 45 cat /var/log/mysqld.log
  31. #设置开启自启动
  32. 46 systemctl start mysqld.service
  33. #查看开启自启动
  34. 47 systemctl status mysqld.service
  35. #登陆mysql
  36. 48 mysql -uroot -p
  37. # 在mysql命令端设置密码 set password = password('root')
  38. # mysql data信息位置 /var/lib/mysql
  39. # 关闭防火墙7之前
  40. 52 systemctl stop iptables
  41. # 关闭防火墙7之后
  42. 53 systemctl stop firewalld
  43. # 虚拟机设置防火墙不开机自启动
  44. 55 systemctl disable firewalld.service

MySQL 主从复制

基于主库的 binlog 进行异步复制到 从库 relaylog,从库执行

mysql 主库设置

修改 my.cnf

  1. vim /etc/my.cnf

添加下面的内容

  1. log_bin=mysql-bin
  2. server-id=1
  3. sync-binlog=1
  4. #指定哪些库不同步,其他库默认都同步
  5. binlog-ignore-db=information_schema
  6. binlog-ignore-db=performance_schema
  7. binlog-ignore-db=sys
  8. #指定哪些库同步
  9. #binlog-do-db=lanebin

重启 mysql

  1. systemctl restart mysqld

授权主库的 root 权限

  1. #进入mysql
  2. grant replication slave on *.* to 'root'@'%' identified by 'root';
  3. grant all privileges on *.* to 'root'@'%' identified by 'root';
  4. flush privileges;
  5. grant replication slave on *.* to 'mha'@'%' identified by '123123';
  6. grant all privileges on *.* to 'mha'@'%' identified by '123123';
  7. flush privileges;

查看下主库的状态

MySQL安装及主从异步、半同步、同步实现 - 图1

mysql 从库设置

my.cnf 修改

  1. # log_bin可以不开
  2. # log_bin=mysql-bin
  3. # 指定serverid
  4. server-id=2
  5. #指定中继日志名字,不指定默认
  6. relay_log =mysql-relay-bin
  7. #指定只读
  8. read_only =1

重启 mysql

  1. systemctl restart mysqld

进入 mysql 设置复制主库

  1. #查看是否以前有配置信息,若有则去除掉
  2. show slave status;
  3. #设置主库信息
  4. change master to master_host='172.16.94.5',master_port=3306,master_user='root',master_password='root',
  5. master_log_file='mysql-bin.000001',master_log_pos=869;
  6. #开启从库
  7. start slave;
  8. #再次查看从库配置信息
  9. show slave status;

MySQL安装及主从异步、半同步、同步实现 - 图2

具体查看下配置信息

MySQL安装及主从异步、半同步、同步实现 - 图3

发现出现错误,主从数据库的 uuid 必须不一样,因为是通过虚拟机进行复制的,所有 uuid 一致,必须修改才行

查找下 auto.cnf 文件的位置

  1. find / -name "auto.cnf"
  2. ./var/lib/mysql/auto.cnf

MySQL安装及主从异步、半同步、同步实现 - 图4

发现主库和从库都是

  1. [auto]
  2. server-uuid=356fc1ff-e339-11eb-bbea-000c29c18362

可以自己删除 auto.cnf,mysql 重启会自动生成,也可以直接修改后重启

重启之后再次查看从库信息正常了

MySQL安装及主从异步、半同步、同步实现 - 图5

验证主从复制效果

主库操作

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | sys |
  9. +--------------------+
  10. 4 rows in set (0.00 sec)
  11. mysql> create database lanebin;
  12. Query OK, 1 row affected (0.01 sec)
  13. mysql> use lanebin;
  14. Database changed
  15. mysql> create table dept (
  16. -> id int primary key auto_increment,
  17. -> name varchar(200) not null comment '名字',
  18. -> )engine =innodb charset=utf8;
  19. uery OK, 0 rows affected (0.01 sec)
  20. mysql> insert into dept values (1,'java');
  21. Query OK, 1 row affected (0.01 sec)
  22. mysql>

从库信息

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | lanebin |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)
  12. mysql> use lanebin
  13. Reading table information for completion of table and column names
  14. You can turn off this feature to get a quicker startup with -A
  15. Database changed
  16. mysql> select * from dept;
  17. +----+------+
  18. | id | name |
  19. +----+------+
  20. | 1 | java |
  21. +----+------+
  22. 1 row in set (0.00 sec)
  23. mysql>

MySQL 半同步复制

mysql 主从复制存在的问题:

主库宕机后,数据可能丢失

从库只有一个 SQL Thread,主库写压力大,复制很可能延时

解决方法:

半同步复制—-解决数据丢失的问题

实现原理:从库写入完成之后发送 ack 给主库,主库 commit;

并行复制——解决从库复制延迟的问题

实现原理:不同事务进行分组,互不影响的并行执行

主库设置

进入 mysql

  1. #查看是否支持插件安装
  2. mysql> select @@have_dynamic_loading;
  3. #查看插件
  4. mysql> show plugins;
  5. #安装插件semi master
  6. mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
  7. #查看semi是否开启
  8. mysql> show variables like '%semi%';
  9. +-------------------------------------------+------------+
  10. | Variable_name | Value |
  11. +-------------------------------------------+------------+
  12. | rpl_semi_sync_master_enabled | OFF |
  13. | rpl_semi_sync_master_timeout | 10000 |
  14. | rpl_semi_sync_master_trace_level | 32 |
  15. | rpl_semi_sync_master_wait_for_slave_count | 1 |
  16. | rpl_semi_sync_master_wait_no_slave | ON |
  17. | rpl_semi_sync_master_wait_point | AFTER_SYNC |
  18. +-------------------------------------------+------------+
  19. 6 rows in set (0.01 sec)
  20. #开启semi插件
  21. mysql> set global rpl_semi_sync_master_enabled =1;
  22. mysql> set global rpl_semi_sync_master_timeout= 1000;

从库设置

  1. #安装插件semi slave
  2. mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
  3. #查看semi是否开启
  4. mysql> show variables like '%semi%';
  5. +---------------------------------+-------+
  6. | Variable_name | Value |
  7. +---------------------------------+-------+
  8. | rpl_semi_sync_slave_enabled | OFF |
  9. | rpl_semi_sync_slave_trace_level | 32 |
  10. +---------------------------------+-------+
  11. 2 rows in set (0.00 sec)
  12. mysql> set global rpl_semi_sync_slave_enabled =1;
  13. #重启slave库
  14. mysql> stop slave;
  15. mysql> start slave;

查看是否半同步

  1. #主库
  2. insert into dept values(2,'h5');
  3. #从库
  4. mysql> select * from dept;
  5. +----+------+
  6. | id | name |
  7. +----+------+
  8. | 1 | java |
  9. | 2 | h5 |
  10. +----+------+c

查看日志信息

  1. cd /var/log
  2. cat mysqld.log

MySQL安装及主从异步、半同步、同步实现 - 图6

MySQL 同步复制

主库设置

进入 mysql

  1. mysql> show variables like '%binlog_group%';
  2. +-----------------------------------------+-------+
  3. | Variable_name | Value |
  4. +-----------------------------------------+-------+
  5. | binlog_group_commit_sync_delay | 0 |
  6. | binlog_group_commit_sync_no_delay_count | 0 |
  7. +-----------------------------------------+-------+
  8. # 设置延时时间
  9. mysql> set global binlog_group_commit_sync_delay=1000;
  10. # 设置事务数
  11. mysql> set global binlog_group_commit_sync_no_delay_count =100;

从库设置

进入 mysql

  1. mysql> show variables like '%slave_parallel%';
  2. +------------------------+----------+
  3. | Variable_name | Value |
  4. +------------------------+----------+
  5. | slave_parallel_type | DATABASE |
  6. | slave_parallel_workers | 0 |
  7. +------------------------+----------+
  8. mysql> set global slave_parallel_type='LOGICAL_CLOCK';
  9. ERROR 3017 (HY000): This operation cannot be performed with a running slave sql thread; run STOP SLAVE SQL_THREAD first
  10. mysql> stop slave;
  11. mysql> set global slave_parallel_type='LOGICAL_CLOCK';
  12. #设置sql线程数建议4~8
  13. mysql> set global slave_parallel_workers =8;

修改 relay log 信息

  1. mysql> show variables like '%relay_log%';
  2. +---------------------------+--------------------------------------+
  3. | Variable_name | Value |
  4. +---------------------------+--------------------------------------+
  5. | max_relay_log_size | 0 |
  6. | relay_log | mysql-relay-bin |
  7. | relay_log_basename | /var/lib/mysql/mysql-relay-bin |
  8. | relay_log_index | /var/lib/mysql/mysql-relay-bin.index |
  9. | relay_log_info_file | relay-log.info |
  10. | relay_log_info_repository | FILE |
  11. | relay_log_purge | ON |
  12. | relay_log_recovery | OFF |
  13. | relay_log_space_limit | 0 |
  14. | sync_relay_log | 10000 |
  15. | sync_relay_log_info | 10000 |
  16. +---------------------------+--------------------------------------+
  17. 11 rows in set (0.00 sec)
  18. mysql> set global relay_log_info_repository='TABLE';
  19. Query OK, 0 rows affected (0.00 sec)
  20. mysql> set global relay_log_recovery =1;
  21. ERROR 1238 (HY000): Variable 'relay_log_recovery' is a read only variable
  22. mysql>

进入 /etc/my.cnf 下设置

  1. vim /etc/my.cnf
  2. #添加
  3. #同步复制
  4. relay_log_recovery =1
  5. #重启mysql
  6. systemctl restart mysqld

再次进入 mysql 命令

  1. mysql> show variables like '%relay_log%';
  2. +---------------------------+--------------------------------------+
  3. | Variable_name | Value |
  4. +---------------------------+--------------------------------------+
  5. | max_relay_log_size | 0 |
  6. | relay_log | mysql-relay-bin |
  7. | relay_log_basename | /var/lib/mysql/mysql-relay-bin |
  8. | relay_log_index | /var/lib/mysql/mysql-relay-bin.index |
  9. | relay_log_info_file | relay-log.info |
  10. | relay_log_info_repository | FILE |
  11. | relay_log_purge | ON |
  12. | relay_log_recovery | ON |
  13. | relay_log_space_limit | 0 |
  14. | sync_relay_log | 10000 |
  15. | sync_relay_log_info | 10000 |
  16. +---------------------------+--------------------------------------+
  17. mysql> show variables like '%slave_parallel%';
  18. +------------------------+----------+
  19. | Variable_name | Value |
  20. +------------------------+----------+
  21. | slave_parallel_type | DATABASE |
  22. | slave_parallel_workers | 0 |
  23. +------------------------+----------+

发现修改的内容又恢复开始的样子

这样就需要进入 /etc/my.cnf 下设置才保证不会重启修改消失

  1. vim /etc/my.cnf
  2. #添加
  3. #同步复制
  4. slave_parallel_type =LOGICAL_CLOCK
  5. slave_parallel_workers =8
  6. master_info_repository =TABLE
  7. relay_log_info_repository=TABLE
  8. relay_log_recovery =1

再次重启 mysql

  1. systemctl restart mysqld
  2. mysql -uroot -proot

进入 mysql 查看信息,更改已经生效了

  1. mysql> show variables like '%slave_parallel%';
  2. +------------------------+---------------+
  3. | Variable_name | Value |
  4. +------------------------+---------------+
  5. | slave_parallel_type | LOGICAL_CLOCK |
  6. | slave_parallel_workers | 8 |
  7. +------------------------+---------------+
  8. mysql> show variables like '%relay_log%';
  9. +---------------------------+--------------------------------------+
  10. | Variable_name | Value |
  11. +---------------------------+--------------------------------------+
  12. | max_relay_log_size | 0 |
  13. | relay_log | mysql-relay-bin |
  14. | relay_log_basename | /var/lib/mysql/mysql-relay-bin |
  15. | relay_log_index | /var/lib/mysql/mysql-relay-bin.index |
  16. | relay_log_info_file | relay-log.info |
  17. | relay_log_info_repository | TABLE |
  18. | relay_log_purge | ON |
  19. | relay_log_recovery | ON |
  20. | relay_log_space_limit | 0 |
  21. | sync_relay_log | 10000 |
  22. | sync_relay_log_info | 10000 |
  23. +---------------------------+--------------------------------------+

测试同步复制

数据层面查看

  1. #主添加信息
  2. insert into dept values(3,'ui');
  3. #从库查看信息
  4. select * from dept;
  5. +----+------+
  6. | id | name |
  7. +----+------+
  8. | 1 | java |
  9. | 2 | h5 |
  10. | 3 | ui |
  11. +----+------+

查看线程信息

  1. mysql> use performance_schema;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> select * from replication_applier_status_by_worker;
  6. +--------------+-----------+-----------+---------------+-----------------------+-------------------+--------------------+----------------------+
  7. | CHANNEL_NAME | WORKER_ID | THREAD_ID | SERVICE_STATE | LAST_SEEN_TRANSACTION | LAST_ERROR_NUMBER | LAST_ERROR_MESSAGE | LAST_ERROR_TIMESTAMP |
  8. +--------------+-----------+-----------+---------------+-----------------------+-------------------+--------------------+----------------------+
  9. | | 1 | 27 | ON | ANONYMOUS | 0 | | 0000-00-00 00:00:00 |
  10. | | 2 | 28 | ON | | 0 | | 0000-00-00 00:00:00 |
  11. | | 3 | 29 | ON | | 0 | | 0000-00-00 00:00:00 |
  12. | | 4 | 30 | ON | | 0 | | 0000-00-00 00:00:00 |
  13. | | 5 | 31 | ON | | 0 | | 0000-00-00 00:00:00 |
  14. | | 6 | 32 | ON | | 0 | | 0000-00-00 00:00:00 |
  15. | | 7 | 34 | ON | | 0 | | 0000-00-00 00:00:00 |
  16. | | 8 | 36 | ON | | 0 | | 0000-00-00 00:00:00 |
  17. +--------------+-----------+-----------+---------------+-----------------------+-------------------+--------------------+----------------------+
  18. 8 rows in set (0.00 sec)