系统环境:Centos7

主:192.168.8.162

从:192.168.8.127

前提条件

a.关闭防火墙 

  • systemctl stop firewalld

关闭防火墙开机自启

  • systemctl disable firewalld

b.关闭selinux

  • vi /etc/selinux/config

将SELINUX=enforcing改为SELINUX=disabled

设置后需要重启才能生效

本文参考:CSDN“我的小熊不见了”博主,原文链接:https://blog.csdn.net/qq1311256696/article/details/90747435

首先两台数据库安装mysql8.0

1.查看本机是否安装mariadb

  • rpm -qa|grep mariadb 
  • rpm -e --nodeps 文件名 
2.查看是否已经安装了mysql
  • rpm -qa | grep -i mysql
如果存在则通过 rpm -ev 包名 --nodeps 卸载
再执行 rpm -qa | grep -i mysql 看是否卸载完成
查找mysql文件,使用rm -rf mysql文件路径删除
  • find / -name mysql
 
删除配置文档
  • rm -rf /etc/my.cnf

3.安装mysql8.0的yum源

  • yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

执行 yum search mysql  如果出现 mysql-community-server.x86_64 则安装源成功

4.安装mysql8.0

  • yum -y install mysql-community-server.x86_64

5.启动mysql

  • systemctl start mysqld

开机自启:systemctl enable mysqld

6.查看mysql版本

  • mysql -V

7.设置mysql密码

  • cat /var/log/mysqld.log

!/dChNphO81E  mysql随机生成的密码

修改密码

  • mysqladmin -u root -p password

9.主库配置

创建一个复制用户,授予用户 slave REPLICATION SLAVE权限和REPLICATION CLIENT权限,用于在主从库之间同步数据。

  • mysql -uroot -p
  • USE mysql;
  • CREATE USER slave IDENTIFIED BY 'Slave@123';
  • GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';

  • FLUSH PRIVILEGES;

vi /etc/my.cnf

添加

server-id=162
log-bin=/var/lib/mysql/mysql-bin

重启数据库

  • systemctl restart mysqld

连接数据库

  • mysql -uroot -p

查看日志情况

  • SHOW MASTER STATUS;

记录下File和Position的值

10.从库配置

  • vi /etc/my.cnf

添加 server-id=127

重启数据库

  • systemctl restart mysqld

连接数据库

  • mysql -uroot -p

停止主从

  • stop slave

从数据连接主数据库:

  • change master to master_host='192.168.8.162', master_user='slave', master_password='Slave@123', master_port=3306, master_log_file='mysql-bin.000002', master_log_pos= 1013, master_connect_retry=30;
  • host、user、password、port为主库的ip、用户名和密码、端口
  • master_log_file为从主库哪个日志读数据即上文中的File

  • master_log_pos为从哪里开始读即上文中的Position

  • master_connect_retry为超时重试时间

开始主从

  • start slave

查看同步状态

  • show slave status \G;

SlaveIORunning 和 SlaveSQLRunning 都是Yes说明主从复制已经开启。

若 Slave_IO_Running为Connecting可能是mysql8的密码规则问题

主数据库修改slave的密码规则即可:

  • ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'Slave@123';
  1. mysql> stop slave;
  2. Query OK, rows affected (0.00 sec)
  3.  
  4. mysql> change master to master_host='192.168.8.162', master_user='slave', master_password='Slave@123', master_port=, master_log_file='mysql-bin.000002', master_log_pos= , master_connect_retry=;
  5. Query OK, rows affected, warnings (0.01 sec)
  6.  
  7. mysql> show slave statu \G;
  8. ERROR (): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'statu' at line
  9. ERROR:
  10. No query specified
  11.  
  12. mysql> show slave status \G;
  13. *************************** . row ***************************
  14. Slave_IO_State:
  15. Master_Host: 192.168.8.162
  16. Master_User: slave
  17. Master_Port:
  18. Connect_Retry:
  19. Master_Log_File: mysql-bin.
  20. Read_Master_Log_Pos:
  21. Relay_Log_File: localhost-relay-bin.
  22. Relay_Log_Pos:
  23. Relay_Master_Log_File: mysql-bin.
  24. Slave_IO_Running: No
  25. Slave_SQL_Running: No
  26. Replicate_Do_DB:
  27. Replicate_Ignore_DB:
  28. Replicate_Do_Table:
  29. Replicate_Ignore_Table:
  30. Replicate_Wild_Do_Table:
  31. Replicate_Wild_Ignore_Table:
  32. Last_Errno:
  33. Last_Error:
  34. Skip_Counter:
  35. Exec_Master_Log_Pos:
  36. Relay_Log_Space:
  37. Until_Condition: None
  38. Until_Log_File:
  39. Until_Log_Pos:
  40. Master_SSL_Allowed: No
  41. Master_SSL_CA_File:
  42. Master_SSL_CA_Path:
  43. Master_SSL_Cert:
  44. Master_SSL_Cipher:
  45. Master_SSL_Key:
  46. Seconds_Behind_Master: NULL
  47. Master_SSL_Verify_Server_Cert: No
  48. Last_IO_Errno:
  49. Last_IO_Error: error connecting to master 'slave@192.168.8.162:3306' - retry-time: retries: message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
  50. Last_SQL_Errno:
  51. Last_SQL_Error:
  52. Replicate_Ignore_Server_Ids:
  53. Master_Server_Id:
  54. Master_UUID:
  55. Master_Info_File: mysql.slave_master_info
  56. SQL_Delay:
  57. SQL_Remaining_Delay: NULL
  58. Slave_SQL_Running_State:
  59. Master_Retry_Count:
  60. Master_Bind:
  61. Last_IO_Error_Timestamp: ::
  62. Last_SQL_Error_Timestamp:
  63. Master_SSL_Crl:
  64. Master_SSL_Crlpath:
  65. Retrieved_Gtid_Set:
  66. Executed_Gtid_Set:
  67. Auto_Position:
  68. Replicate_Rewrite_DB:
  69. Channel_Name:
  70. Master_TLS_Version:
  71. Master_public_key_path:
  72. Get_master_public_key:
  73. Network_Namespace:
  74. row in set (0.00 sec)
  75.  
  76. ERROR:
  77. No query specified
  78.  
  79. mysql> start slave;
  80. Query OK, rows affected (0.01 sec)
  81.  
  82. mysql> show slave status \G;
  83. *************************** . row ***************************
  84. Slave_IO_State: Waiting for master to send event
  85. Master_Host: 192.168.8.162
  86. Master_User: slave
  87. Master_Port:
  88. Connect_Retry:
  89. Master_Log_File: mysql-bin.
  90. Read_Master_Log_Pos:
  91. Relay_Log_File: localhost-relay-bin.
  92. Relay_Log_Pos:
  93. Relay_Master_Log_File: mysql-bin.
  94. Slave_IO_Running: Yes
  95. Slave_SQL_Running: Yes
  96. Replicate_Do_DB:
  97. Replicate_Ignore_DB:
  98. Replicate_Do_Table:
  99. Replicate_Ignore_Table:
  100. Replicate_Wild_Do_Table:
  101. Replicate_Wild_Ignore_Table:
  102. Last_Errno:
  103. Last_Error:
  104. Skip_Counter:
  105. Exec_Master_Log_Pos:
  106. Relay_Log_Space:
  107. Until_Condition: None
  108. Until_Log_File:
  109. Until_Log_Pos:
  110. Master_SSL_Allowed: No
  111. Master_SSL_CA_File:
  112. Master_SSL_CA_Path:
  113. Master_SSL_Cert:
  114. Master_SSL_Cipher:
  115. Master_SSL_Key:
  116. Seconds_Behind_Master:
  117. Master_SSL_Verify_Server_Cert: No
  118. Last_IO_Errno:
  119. Last_IO_Error:
  120. Last_SQL_Errno:
  121. Last_SQL_Error:
  122. Replicate_Ignore_Server_Ids:
  123. Master_Server_Id:
  124. Master_UUID: 0d26dbd8-d501-11e9-9f3d-000c29e24dcf
  125. Master_Info_File: mysql.slave_master_info
  126. SQL_Delay:
  127. SQL_Remaining_Delay: NULL
  128. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  129. Master_Retry_Count:
  130. Master_Bind:
  131. Last_IO_Error_Timestamp:
  132. Last_SQL_Error_Timestamp:
  133. Master_SSL_Crl:
  134. Master_SSL_Crlpath:
  135. Retrieved_Gtid_Set:
  136. Executed_Gtid_Set:
  137. Auto_Position:
  138. Replicate_Rewrite_DB:
  139. Channel_Name:
  140. Master_TLS_Version:
  141. Master_public_key_path:
  142. Get_master_public_key:
  143. Network_Namespace:
  144. row in set (0.00 sec)
  145.  
  146. ERROR:
  147. No query specified

11.测试主从数据库

主库创建test数据库

  • create database test;

测试完成!

如果想只同步某一个数据库

可以在主库的my.cnf

添加

binlog-do-db:binlog日志记录哪个db

或者在从库的my.cnf添加

replicate-do-db:需要复制的数据库名

replicate-ignore-db:不需要复制的数据库名

【linux】【mysql】mysql主从数据库的更多相关文章

  1. Mysql的主从数据库没有同步的解决办法

    Mysql的主从数据库没有同步的解决办法 今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist; 查看下进程是否Sleep太多.发现很正常. ...

  2. 解决Mysql的主从数据库没有同步的两种方法

    今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist;   查看下进程是否Sleep太多.发现很正常.show master status; ...

  3. mysql配置主从数据库

    1.目的 1.1 实现数据备份 1.2 项目访问时可以实现读写分离,提高访问和操作数据的速度<读写分离好处> 2.背景 这次的主从配置主要实现主库数据的改变可以实现同步到从库中: 此次试验 ...

  4. wamp mysql 创建主从数据库

    这跟你是不是wmap没有关系的, wamp是一个集成环境,只是一次性帮你创建一个web服务器而已 下面给你些配置 一.登录Master服务器,修改my.ini ,添加如下内容: [wampmysqld ...

  5. mysql服务器主从数据库同步配置(转)

    <a href=""></a> <p>首先需要在同一个局域网内的两台机器(当然也可以用一台机器虚拟两台机器出来),都安装上mysql服务.< ...

  6. MySQL搭建主从数据库 实现读写分离

    首先声明,实际生产中,网站为了提高用户体验,性能等,将数据库实现读写分离是有必要的,我们让主数据库去写入数据,然后当用户查询的时候,然后在从数据库读取数据,故能减轻数据库的压力,实现良好的用户体验! ...

  7. mysql服务器主从数据库同步配置

    首先需要在同一个局域网内的两台机器(当然也可以用一台机器虚拟两台机器出来),都安装上mysql服务. 主机A: 192.168.1.100 从机B: 192.168.1.101 可以有多台从机. 1. ...

  8. mysql主从数据库不同步的2种解决方法(转)

    今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist; 查看下进程是否Sleep太多.发现很正常. show master status; ...

  9. mysql主从数据库不同步的2种解决方法 (转载)

    今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist; 查看下进程是否Sleep太多.发现很正常. show master status; ...

  10. Linux下MySQL数据库主从同步配置

    说明: 操作系统:CentOS 5.x 64位 MySQL数据库版本:mysql-5.5.35 MySQL主服务器:192.168.21.128 MySQL从服务器:192.168.21.129 准备 ...

随机推荐

  1. copy好文“IT34岁危机破解心法”

    在博客园中偶然发现一个好文,收藏并记录以下.在工作中更多的从企业单位用人角度去思考,或许能在职场及职业规划中更加的游刃有余,有的放矢.下面是原文. 本文题目虽是“IT人34岁危机”破解心法,但内容同样 ...

  2. GC回收算法&&GC回收器

    GC回收算法 什么是垃圾? 类比日常生活中,如果一个东西经常没被使用,那么就可以说是垃圾. 同理,如果一个对象不可能再被引用,那么这个对象就是垃圾,应该被回收. 垃圾:不可能再被引用的对象. fina ...

  3. 数据的查找和提取[2]——xpath解析库的使用

    xpath解析库的使用 在上一节,我们介绍了正则表达式的使用,但是当我们提取数据的限制条件增多的时候,正则表达式会变的十分的复杂,出一丁点错就提取不出来东西了.但python已经为我们提供了许多用于解 ...

  4. 答辩过了,但我知道我写的是一坨shi

    如果时间可以重来, 请好好珍惜大学里最重要的一份答卷. 以后的你会发现, 这种完全独立做出科研成果的机会, 可能不会再有. 1.保研了,才发现科研是多么难的一件事. 一直以来,读的喜欢的专业,考试成绩 ...

  5. spring-cloud-kubernetes与k8s的configmap

    本文是<spring-cloud-kubernetes实战系列>的第六篇,主要内容是在kubernetes上部署一个java web应用,该应用使用了spring-cloud-kubern ...

  6. CF 462 C. A Twisty Movement 分段想 线段树 或 dp

    题意 有一个只包含1和2的序列,试翻转一个区间,使得结果中非连续非递减数列最长. 思路 一. 作出1的前缀计数和为cnt1,2的后缀计数和为cnt2, 由于要找出[1,1,1][2,2,2][1,1, ...

  7. CodeForces 939E Maximize

    Maximize 题意:整个程序有2种操作,操作1将一个元素放入集合S中,且保证最新插入的元素不小于上一次的元素, 操作2 找到集合S中的某个子集合, 使得 集合中最大的元素减去平均数的值最大. 题解 ...

  8. POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 27277   Accepted:  ...

  9. 用PhpStrom线上连接修改linux服务器上代码配置

    为了进一步提高自己的技能水平,不久前入手了一台服务器,不贵,一年也就不到两百,因为自己对于linux机器比较生疏,命令用的有点抠脚.老需要查阅处理. 于是我选择用PhpStrom直接连接线上服务器,通 ...

  10. 【Offer】[53-3] 【数组中数值和下标相等的元素】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 假设一个单调递增的数组里的每个元素都是整数并且是唯一的.请编程实现一个函数,找出数组中任意一个数值等于其下标的元素.例如,在数组{-3, ...