Centos7下mysql5.7.22主从配置
一:服务器配置
1.1修改root密码:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxx';
1.2添加远程登陆用户:
GRANT ALL PRIVILEGES ON *.* TO 'abc'@'%' IDENTIFIED BY 'xxxx' WITH GRANT OPTION;
1.3统一默认编码:
vim /etc/my.cnf 在 [mysqld]下添加编码配置:
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
二:主从配置(主):
2.1vim /etc/my.cnf 在 [mysqld]下添加:
#要给从机同步的库
binlog-do-db=test
#不给从机同步的库(多个写多行)
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#自动清理 7 天前的log文件,可根据需要修改
expire_logs_days=7
# 启用二进制日志
log-bin=mysql-bin
# 服务器唯一ID,一般取IP最后一段
server-id=101
2.2 开启log_bin 在 [mysqld]下添加:
log-bin = mysql-bin
server-id = 072
重启服务:
systemctl restart mysqld
查看是log_bin否启动:
mysql -u root -p
mysql> show variables like '%log_bin%';
+---------------------------------+---------------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------------+
| log_bin | ON |
| log_bin_basename | /usr/local/mysql/data/mysql-bin |
| log_bin_index | /usr/local/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+---------------------------------------+
6 rows in set (0.01 sec)
2.3重启服务器
reboot
2.4建立备份账号并授权
mysql>GRANT FILE ON *.* TO 'backup'@'%' IDENTIFIED BY 'xxxx';
mysql>GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* to 'backup'@'%' identified by 'xxxx';
刷新权限列表:
flush privileges;
取消用户授权:
revoke all on *.* from backup;
2.5查询主服务器的状态:
show master status;
mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000002 | 735 | test01 | mysql,information_schema,performance_schema,sys | |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)
三:主从配置(从):
3.1拷贝主mysql的my.cnf文件到从服务器,替换从my.cnf,并修改如下配置:
scp /etc/my.cnf root@192.168.x.x:/etc/my.cnf
server-id = 2
加上以下参数可以避免更新不及时,SLAVE 重启后导致的主从复制出错。
read_only = 1
master_info_repository=TABLE
relay_log_info_repository=TABLE
3.2配置从mysql:
change master to master_host='x.x.x.x',master_port=3306,master_user='backup',master_password='xxx',master_log_file='mysql-bin.000002',master_log_pos=735;
注!!!语句中间不要断开,master_port为mysql服务器端口号(无引号),master_user为执行同步操作的数据库账户,“120”无单引号(此处的735就是show master status 中看到的position的值,这里的mysql-bin.000002就是file对应的值)。
如报错:
ERROR 29 (HY000): File '/var/log/mysql-relay-index.index' not found (Errcode: 13 - Permission denied)
编辑my.cnf文件,把log文件集中到/var/log/mysql目录:
[mysqld]
relay_log = /var/log/mysql/mysql-relay-bin
relay_log_index = /var/log/mysql/mysql-relay-index
log_error = /var/log/mysql/mysql-error.err
修改/var/log/mysql目录权限:
chown -R mysql:mysql /var/log/mysql/
重启mysql
systemctl restart mysqld
进入mysql,再次运行change配置
3.3正确执行后启动Slave同步进程
mysql> start slave;
查看从服务器状态
show slave status \G
\G后不加;号
输出:
Slave_IO_Running: No
Slave_SQL_Running: Yes
两个必须为Yes才是正确启动!!!
查看log文件:
vim /var/log/mysql/mysql-error.err
2019-01-16T02:17:02.480802Z 2 [ERROR] Slave I/O for channel '': Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work. Error_code: 1593
因从虚拟机为主安装mysql后拷贝生成,造成主从mysql UUID值相同,需要修改为不同值:
找到data文件夹下的auto.cnf文件,修改里面的uuid值,保证各个db的uuid不一样,重启db即可
重启mysql
systemctl restart mysqld
再次查看从服务器状态:
mysql> show slave status \G
mymsql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting to reconnect after a failed registration on master
Master_Host: 192.168.10.11
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 735
Relay_Log_File: mysql-relay-bin.000003
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
还是报错:
Slave_IO_Running: Connecting
查看log文件:
2019-01-16T02:53:53.094695Z 7 [ERROR] Slave I/O for channel '': Master command COM_REGISTER_SLAVE failed: Access denied for user 'backup'@'%' (using password: YES) (Errno: 1045), Error_code: 1597
重新赋权:
mysql>GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* to 'backup'@'%' identified by 'xxxx';
切记要刷新:
flush privileges;
mysql> stop slave;
mysql> start slave;
mysql> show slave status \G
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.72
Master_User: wxjcbackup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 2708
Relay_Log_File: mysql-relay-bin.000006
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果show slave status \G
报错:
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
Slave_IO_Running: No
Slave_SQL_Running: Yes
....
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
在主mysql下运行:
mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000004 | 154 | test01 | mysql,information_schema,performance_schema,sys | |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)
在从mysql下运行:
mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000006 | 154 | test01 | mysql,information_schema,performance_schema,sys | |
+------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)
两个file编号不一致,以主为准,重新定位:
stop slave;
change master to master_host='x.x.x.x',master_port=3306,master_user='backup',master_password='xxx',master_log_file='mysql-bin.000004',master_log_pos=154;
start slave;
show slave status;
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.10.11
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
四:测试同步:
查看主服务器数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.01 sec)
主库新建数据库:
mysql> create database testdb02;
Query OK, 1 row affected (0.00 sec)
从库查看数据库同步情况:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
| testdb01 |
| testdb02 |
+--------------------+
7 rows in set (0.00 sec)
关闭掉主数据库的读取锁定
unlock tables;
show databases;
use test;
show tables;
感谢:
https://blog.csdn.net/happy_pinocchio/article/details/79592422
五:重新做主从,完全同步 (适用于主库从库的数据相差较大;要求数据完全统一的情况 )
5.1 先进入主库,进行锁表,此处锁定为只读状态,防止数据写入
(可选,因如有数据库备份,可直接利用备份)
flush tables with read lock;
5.2 进行数据备份,把数据备份为.sql的文件
(可选,因如有数据库备份,可直接利用备份)
切换到mysql用户
su mysql
mysqldump -uroot -p‘密码’ --all-databases > /data/mysql/backup/mysql.back.sql
如报错:
先查看vim /etc/profile是否添加mysql环境变量,如没有则添加
export PATH=$PATH:/usr/local/mysql/bin
export PATH
5.3 进入主库,进行解锁
(可选,因如有数据库备份,可直接利用备份)
unlock tables;
5.4 把mysql的备份文件传输到从库服务器上
(位置任意,但要能找到)
scp -r /data/mysql/backup/mysql.bask.sql root@mysql02:/data/mysql/backup/
5.5 进入从库,停止从库的状态
stop slave;
清除slave上的同步位置,删除所有旧的同步日志,使用新的日志重新开始.(使用前先停止slave服务)
reset slave;(可选)
5.6 在从库中导入数据备份
source /data/mysql/backup/mysql.back.sql ;
或
mysql -uroot -p‘....’ database -f < /data/mysql/backup/mysql.bask.sql
(-f 为跳过错误的Sql,继续往下执行,可不加)
5.7 重新设置从库同步
change master to master_host = '主库的IP’, master_user = '设置主从时设定的主库的用户', master_port=主库的端口, master_password='主库设定的密码', master_log_file = 'mysqld-bin.xxxxxx', master_log_pos=xxxx;
注意: master_log_file与master_log_pos 是主库show master status信息里的| File与Position
5.8 重新开启从库同步
start slave;
5.9 查看同步状态
mysql> show slave status\G
查看:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
感谢:
https://blog.csdn.net/GX_1_11_real/article/details/80658527
Centos7下mysql5.7.22主从配置的更多相关文章
- Centos7下MySql5.7安装及配置
安装MySql 软件包: mysql-community-libs-5.7.22-1.el7.x86_64.rpm mysql-community-common-5.7.22-1.el7.x86_64 ...
- centos7下mysql5.7的安装与配置
centos7下MySQL5.7的安装与配置 下载 下载地址 根据系统和版本选择红框中的四个RPM包下载即可,然后放到centos7系统中的/opt目录下,等待稍后安装. 安装前的准备 1. 检查系统 ...
- CentOS7下MySQL5.7的安装-RPM方式
Installing MySQL on Linux Using RPM Packages 下载安装包 mysql下载地址:https://dev.mysql.com/downloads/mysql/ ...
- Centos7 下的SVN安装与配置
Centos7 下的SVN安装与配置 1.关闭防火墙 临时关闭防火墙 systemctl stop firewalld 永久防火墙开机自关闭 systemctl disable firewalld 临 ...
- centos7安装mysql5.7.19及配置远程连接
centos7安装mysql5.7.19及配置远程连接------https://blog.csdn.net/Lh19931122/article/details/77996213
- CentOS7下NFS服务安装及配置固定端口
CentOS7下NFS服务安装及配置 系统环境:CentOS Linux release 7.4.1708 (Core) 3.10.0-693.el7.x86_64 软件版本:nfs-utils-1. ...
- docker创建mysql5.7.22并配置主从
debian系统 安装docker (参考网址:https://cloud.tencent.com/developer/article/1360720) 1.更新现有的包列表 sudo apt upd ...
- 超详细的CentOS7 64位下MySQL5.7安装与配置(YUM)【转发+新创】
安装环境:CentOS7 64位 MINI版,安装MySQL5.7 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...
- CentOS7下MySQL5.7安装配置方法图文教程(YUM)
安装环境:CentOS7 64位,MySQL5.7 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/ # ...
随机推荐
- centos7: php7.2.9安装配置
下载php http://cn2.php.net/distributions/php-7.2.9.tar.gz 解压到: /usr/local/src/ 接下来进行参数配置,配置前如果没有libxml ...
- Django用户认证组件
用户认证 主要分两部分: 1.auth模块 from django.contrib import auth 2.User对象 from django.contrib.auth.models imp ...
- 牛客练习赛41 B-666RPG
题目链接:https://ac.nowcoder.com/acm/contest/373/B 题意:有n个回合,每个回合给1个数,每个回合你有两种选择 1.加上第i个数 2.将当前数乘-1 想知道有多 ...
- 『TensorFlow』第七弹_保存&载入会话_霸王回马
首更: 由于TensorFlow的奇怪形式,所以载入保存的是sess,把会话中当前激活的变量保存下来,所以必须保证(其他网络也要求这个)保存网络和载入网络的结构一致,且变量名称必须一致,这是caffe ...
- 51nod1647 小Z的trie
题意:给你n个字符串,m次查询,每次问你第p个字符串的s到t的字符串在n个字符串建成的字典树上出现了多少次 题解:先建出字典树,在字典树上拓展sam,记录每个子串的出现次数.查询时只需找出在字典树上的 ...
- bzoj4516: [Sdoi2016]生成魔咒 sam
题意:每次插入一个数字,查询本质不同的子串有多少个 题解:sam,数字很大,ch数组用map来存,每次ins之后查询一下新建点表示多少个本质不同的子串(l[np]-l[fa[np]]) /****** ...
- Leetcode 128 *
class Solution { public: int longestConsecutive(vector<int>& nums) { ; unordered_map<in ...
- C语言实现哈夫曼编码(最小堆,二叉树)
// 文件中有通过QT实现的界面#include <stdio.h> #include <stdlib.h> #include <string.h> typedef ...
- 22. Generate Parentheses C++回溯法
把左右括号剩余的次数记录下来,传入回溯函数. 判断是否得到结果的条件就是剩余括号数是否都为零. 注意判断左括号是否剩余时,加上left>0的判断条件!否则会memory limited erro ...
- spring security antMatchers相关内容
一.antMatcher与antMatchers的区别以及使用场景 来源:https://stackoverflow.com/questions/35890540/when-to-use-spring ...