MySQL5.7主从复制配置

Wesley13
• 阅读 547

1 my.cnf文件 配置

  binlog_format = ROW
  log_bin_trust_function_creators=1
  log-error = /usr/local/mysql/data/hostname.err
  log-bin = /usr/local/mysql/arch/mysql-bin
  expire_logs_days = 7

#server-id需要与master不一致

  server-id = 1739

2 在master主机上创建同步用户

grant replication slave on *.* to sync@'%' identified by 'Zj123456!';
flush privileges;

3 查看主机上master status

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 989 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

4  从库上执行master

mysql>

change master to

master_host='192.168.56.100',

master_port=3306,

master_user='sync',

master_password='Zj123456!',

master_log_file='mysql-bin.000005',

master_log_pos=3820;

mysql> start slave;

mysql>show slave status\G;

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.100
Master_User: sync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 3820
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: omsprd.%,wmsb01.%,wmsb02.%,wmsb03.%,wmsb04.%,wmsb05.%,wmsb06.%,wmsb07.%,wmsb08.%,wmsb08.%,wmsb09.%,wmsb10.%,wmsb11.%,wmsb27.%,wmsb31.%,wmsb32.%,wmsb33.%,wmsb34.%,wmsb35.%
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3820
Relay_Log_Space: 521
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1739
Master_UUID: 71f0e5b7-16f2-11e9-949d-0800271f440a
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

看到这两行,表示成功

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

5 验证同步是否成功

查看my.cnf 设置同步的数据库与表

#need to sync tables
replicate-wild-do-table=omsprd.%
replicate_wild_do_table=wmsb01.%
replicate_wild_do_table=wmsb02.%
replicate_wild_do_table=wmsb03.%
replicate_wild_do_table=wmsb04.%
replicate_wild_do_table=wmsb05.%
replicate_wild_do_table=wmsb06.%
replicate_wild_do_table=wmsb07.%
replicate_wild_do_table=wmsb08.%
replicate_wild_do_table=wmsb08.%
replicate_wild_do_table=wmsb09.%
replicate_wild_do_table=wmsb10.%
replicate_wild_do_table=wmsb11.%
replicate_wild_do_table=wmsb27.%
replicate_wild_do_table=wmsb31.%
replicate_wild_do_table=wmsb32.%
replicate_wild_do_table=wmsb33.%
replicate_wild_do_table=wmsb34.%
replicate_wild_do_table=wmsb35.%

验证

主库上操作
mysql> create database omsprd;
Query OK, 1 row affected (0.00 sec)

mysql> use omsprd;
Database changed

mysql> CREATE TABLE `t_banner` (
-> `id` int(11) NOT NULL AUTO_INCREMENT,
-> `img` varchar(128) NOT NULL,
-> `url` varchar(128) NOT NULL,
-> `creater` varchar(10) NOT NULL,
-> `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
-> `name` varchar(64) NOT NULL,
-> `is_delete` int(11) NOT NULL DEFAULT '0',
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='广告';
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t_banner(id,img,url,creater,name) values(1,'zhangsan', 'php001','zhangjin','test');
Query OK, 1 row affected (0.00 sec)

mysql>

从库查询

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| omsprd |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> use omsprd;
Database changed
mysql> show tables;
+------------------+
| Tables_in_omsprd |
+------------------+
| t_banner |
+------------------+
1 row in set (0.00 sec)

mysql> select * from t_banner;
+----+----------+--------+----------+---------------------+------+-----------+
| id | img | url | creater | create_time | name | is_delete |
+----+----------+--------+----------+---------------------+------+-----------+
| 1 | zhangsan | php001 | zhangjin | 2019-01-13 06:58:58 | test | 0 |
+----+----------+--------+----------+---------------------+------+-----------+
1 row in set (0.00 sec)

mysql>

期间遇到的问题

1 mysql从库启动不了,与主库的uuid一致,删除data文件,重新初始化搞定

2 Slave_IO_Running: NO 

  第一次 密码不对,修改后还是不可以

  第二次 重新刷新用户名赋予权限,远程登录可以,重新change to master后可以搞定。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这