当前位置:   article > 正文

centos7 安装MySQL5.7,主从库搭建步骤_主从数据库配置:已经操作系统为centos 7,安装的数据库为mysql5.7,master服务器的

主从数据库配置:已经操作系统为centos 7,安装的数据库为mysql5.7,master服务器的i

1、服务环境

master:192.168.2.150,centos7.6,mysql5.7

slave:192.168.2.151,centos7.6,mysql5.7

注意:两个库的msyql版本要保持一致

2、mysql安装步骤(192.168.2.150和192.168.2.151服务器可按照相同步骤安装

a.下载MySQL 安装包

[root@localhost local]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

b.安装mysql 安装源

[root@localhost local]#  yum -y localinstall mysql57-community-release-el7-11.noarch.rpm 

c.在线安装MySQL,该步骤时间比较长

[root@localhost local]# yum -y install mysql-community-server

d.以上可能会报错,如果报以下错:

  1. ===========================================================================================================================================================================================
  2. Package 架构 版本 源 大小
  3. ===========================================================================================================================================================================================
  4. 正在安装:
  5. mysql-community-libs x86_64 5.7.37-1.el7 mysql57-community 2.4 M
  6. 替换 mariadb-libs.x86_64 1:5.5.68-1.el7
  7. mysql-community-libs-compat x86_64 5.7.37-1.el7 mysql57-community 1.2 M
  8. 替换 mariadb-libs.x86_64 1:5.5.68-1.el7
  9. mysql-community-server x86_64 5.7.37-1.el7 mysql57-community 174 M
  10. 为依赖而安装:
  11. mysql-community-client x86_64 5.7.37-1.el7 mysql57-community 25 M
  12. mysql-community-common x86_64 5.7.37-1.el7 mysql57-community 311 k
  13. 事务概要
  14. ===========================================================================================================================================================================================
  15. 安装 3 软件包 (+2 依赖软件包)
  16. 总计:203 M
  17. Downloading packages:
  18. 警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
  19. file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
  20. "MySQL 5.7 Community Server" 的 GPG 密钥已安装,但是不适用于此软件包。请检查源的公钥 URL 是否配置正确。
  21. 失败的软件包是:mysql-community-libs-compat-5.7.37-1.el7.x86_64
  22. GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

编辑yum源文件 

[root@localhost local]# vim /etc/yum.repos.d/mysql-community.repo

 设置gpgcheck=0,跳过秘钥检查,然后重复执行步骤c

 e.启动mysql,并设置开机启动

  1. [root@localhost local]# systemctl start mysqld
  2. [root@localhost local]# systemctl enable mysqld
  3. [root@localhost local]# systemctl daemon-reload

 f.修改root登录密码

mysql安装完成后,生成的root的临时初始密码放在/var/log/mysqld.log文件中,拷贝出临时密码

[root@localhost local]# vim /var/log/mysqld.log

 g.修改root的临时密码,并设置远程访问权限

[root@localhost local]# mysql -u root -p
  1. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql!@#7896jmg';
  2. Query OK, 0 rows affected (0.00 sec)
  3. # 设置远程登录
  4. mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mysql!@#7896jmg' WITH GRANT OPTION;
  5. Query OK, 0 rows affected, 1 warning (0.00 sec)
  6. mysql> exit

h.开放防火墙3306端口,或者关闭防火墙,具体操作省略。

以上步骤就完成的了centos环境下的mysql安装。

3、主从搭建

a.工作原理

Master 数据库发生变化时,会立马记录到Binary log 日志文件中
Slave数据库启动一个I/O 进程,连接Master数据库,并请求Master变化的二进制日志
Slave I/O获取到二进制日志,并保存到slave的Relay log日志文件中
Slave的SQL进程定时检查Realy log是否变化,发生变化时就更新数据

b.修改mysql的配置文件

[root@localhost local]# vim /etc/my.cnf
master配置文件添加以下设置(192.168.2.150)
  1. # 节点ID,确保唯一
  2. server-id = 1
  3. #开启mysql的binlog日志功能
  4. log-bin = mysql-bin
  5. #控制数据库的binlog刷到磁盘上去 , 0 不控制,性能最好,1每次事物提交都会刷到日志文件中,性能最差,最安全
  6. sync_binlog = 1
  7. #binlog日志格式,mysql默认采用statement,建议使用mixed
  8. binlog_format = mixed
  9. #binlog过期清理时间
  10. expire_logs_days = 7
  11. #binlog每个日志文件大小
  12. max_binlog_size = 100m
  13. #binlog缓存大小
  14. binlog_cache_size = 4m
  15. #最大binlog缓存大
  16. max_binlog_cache_size= 512m
  17. #不生成日志文件的数据库,多个忽略数据库可以用逗号拼接,或者 复制这句话,写多行
  18. binlog-ignore-db=mysql
  19. # 自增值的偏移量
  20. auto-increment-offset = 1
  21. # 自增值的自增量
  22. auto-increment-increment = 1
  23. #跳过从库错误
  24. slave-skip-errors = all

slave配置文件添加以下设置(192.168.2.151)

  1. server-id = 2
  2. log-bin=mysql-bin
  3. relay-log = mysql-relay-bin
  4. #忽略规则
  5. replicate-wild-ignore-table=mysql.%
  6. replicate-wild-ignore-table=information_schema.%

 重启两个mysql,配置生效

c.master数据库,创建复制用户并授权(测试环境可以也直接使用root账号)

  1. CREATE USER master_user IDENTIFIED BY '123456';
  2. grant replication slave on *.* to 'master_user'@'192.168.2.150' identified by '123456';
  3. FLUSH PRIVILEGES;

d.查看master的状态

  1. show master status;
  2. mysql> show master status;
  3. +------------------+----------+--------------+------------------+-------------------+
  4. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  5. +------------------+----------+--------------+------------------+-------------------+
  6. | mysql-bin.000005 154| | mysql | |
  7. +------------------+----------+--------------+------------------+-------------------+
  8. 1 row in set (0.00 sec)

c.配置从库

  1. mysql> CHANGE MASTER TO
  2. MASTER_HOST = '192.168.2.150',
  3. MASTER_USER = 'master_user',
  4. MASTER_PASSWORD = '123456',
  5. MASTER_PORT = 3306,
  6. MASTER_LOG_FILE='mysql-bin.000005',
  7. MASTER_LOG_POS=154,
  8. MASTER_RETRY_COUNT = 60,
  9. MASTER_HEARTBEAT_PERIOD = 10000;
  10. # MASTER_LOG_FILE='mysql-bin.000005',#与主库File 保持一致
  11. # MASTER_LOG_POS=120 , #与主库Position 保持一致

d.启动从库slave进程(巨坑:有些文档使用slave start,在mysql>5.7时使用start slave)

  1. mysql> start slave;
  2. Query OK, 0 rows affected (0.04 sec)

e.测试主重复制,在slave(192.168.2.151)中,登录数据库,然后执行以下脚本

  1. mysql> SLAVE START; #开启复制
  2. mysql> SHOW SLAVE STATUS\G #查看主从复制是否配置成功
  3. *************************** 1. row ***************************
  4. Slave_IO_State: Waiting for master to send event
  5. Master_Host: 192.168.2.150
  6. Master_User: root
  7. Master_Port: 3306
  8. Connect_Retry: 60
  9. Master_Log_File: mysql-bin.000006
  10. Read_Master_Log_Pos: 828
  11. Relay_Log_File: mysql-relay-bin.000088
  12. Relay_Log_Pos: 320
  13. Relay_Master_Log_File: mysql-bin.000006
  14. Slave_IO_Running: Yes
  15. Slave_SQL_Running: Yes
  16. Replicate_Do_DB:
  17. Replicate_Ignore_DB:
  18. Replicate_Do_Table:
  19. Replicate_Ignore_Table:
  20. Replicate_Wild_Do_Table:
  21. Replicate_Wild_Ignore_Table: mysql.%,test.%,information_schema.%
  22. Last_Errno: 0
  23. Last_Error:
  24. Skip_Counter: 0
  25. Exec_Master_Log_Pos: 828
  26. Relay_Log_Space: 693
  27. Until_Condition: None
  28. Until_Log_File:
  29. Until_Log_Pos: 0
  30. Master_SSL_Allowed: No
  31. Master_SSL_CA_File:
  32. Master_SSL_CA_Path:
  33. Master_SSL_Cert:
  34. Master_SSL_Cipher:
  35. Master_SSL_Key:
  36. Seconds_Behind_Master: 0
  37. Master_SSL_Verify_Server_Cert: No
  38. Last_IO_Errno: 0
  39. Last_IO_Error:
  40. Last_SQL_Errno: 0
  41. Last_SQL_Error:
  42. Replicate_Ignore_Server_Ids:
  43. Master_Server_Id: 1
  44. Master_UUID: b6483844-9e89-11ec-8659-000c29e4cc0f
  45. Master_Info_File: /var/lib/mysql/master.info
  46. SQL_Delay: 0
  47. SQL_Remaining_Delay: NULL
  48. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  49. Master_Retry_Count: 60
  50. Master_Bind:
  51. Last_IO_Error_Timestamp:
  52. Last_SQL_Error_Timestamp:
  53. Master_SSL_Crl:
  54. Master_SSL_Crlpath:
  55. Retrieved_Gtid_Set:
  56. Executed_Gtid_Set:
  57. Auto_Position: 0
  58. Replicate_Rewrite_DB:
  59. Channel_Name:
  60. Master_TLS_Version:
  61. 1 row in set (0.00 sec)

查看:Slave_IO_Running: YES、Slave_SQL_Running: YES,说明配置成功。

f.测试
在master(192.168.2.150)中创建数据库test,然后在slave(192.168.2.151)中查看test数据库是否同步过来。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/885186
推荐阅读
相关标签
  

闽ICP备14008679号