17.4 配置从
时间:2022-04-27
本文章向大家介绍17.4 配置从,主要内容包括主从配置 - 从上操作、主从配置 - 从上操作、查看主从同步是否正常、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
主从配置 - 从上操作
- 安装mysql
- 查看my.cnf,配置server-id=132,要求和主不一样
- 修改完配置文件后,启动或者重启mysqld服务
- 把主上aming库同步到从上
- 可以先创建aming库,然后把主上的/tmp/mysql.sql拷贝到从上,然后导入aming库
- mysql -uroot
- stop slave;
- change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx,
- start slave;
- 还要到主上执行 unlock tables
主从配置 - 从上操作
- 在从上机器配置
- 首先在从上安装并启动mysql,然后查看my.cnf,配置server-id=131,要求和主不一样,在配置文件的 log_bin参数 就不需要配置的,因为只有 主上 才需要二进制日志文件
[root@hf-01 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=131
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
# log-error=/var/log/mariadb/mariadb.log
# pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
# !includedir /etc/my.cnf.d
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
保存退出
- 重启mysql服务
[root@hf-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL... SUCCESS!
[root@hf-01 ~]#
- 在增加 server-id 后,对于mysql 是没有任何变化的
[root@hf-01 ~]# ls /data/mysql
aria_log.00000001 hf-01.err ibdata1 localhost.localdomain.err performance_schema
aria_log_control hf-01.pid ib_logfile0 multi-master.info test
auto.cnf ib_buffer_pool ib_logfile1 mysql
- 把主机器上备份的 .sql 数据,拷贝到从机器上,然后做一个数据恢复
- scp 192.168.202.130:/tmp/*.sql /tmp/
- 拷贝数据失败
- 原因:
- 在拷贝的数据的时候,一直拷贝数据失败,因为在主上的机器里有开机启动脚本,所以导致拷贝数据失败
- 原因:
[root@hf-01 ~]# scp 192.168.202.130:/tmp/*.sql /tmp/
root@192.168.74.129's password:
_oo0oo_
[root@hf-01 ~]#
- 解决方法:
- 将主机器的里的开机启动脚本关闭vi .bashrc配置文件文件中注释掉脚本,再来拷贝数据到从机器上,会发现成功
[root@hf-01 ~]# scp 192.168.202.130:/tmp/*.sql /tmp/
The authenticity of host '192.168.202.130 (192.168.202.130)' can't be established.
ECDSA key fingerprint is 25:ce:a9:96:36:88:84:ab:4f:7e:80:a3:e7:36:2c:9f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.202.130' (ECDSA) to the list of known hosts.
root@192.168.202.130's password:
test.sql 100% 1258 1.2KB/s 00:00
zrlog.sql 100% 10KB 9.8KB/s 00:00
[root@hf-01 ~]#
- 进入到从上数据库中——>因为从上没设置密码,所以可以直接进去
[root@hf-01 ~]# mysql -uroot
-bash: mysql: 未找到命令
[root@hf-01 ~]#
- mysql -uroot命令不存在,是因为没有加入到PATH里面
- 这里做个alias别名设置,用单引号' '
[root@hf-01 ~]# alias 'mysql=/usr/local/mysql/bin/mysql'
[root@hf-01 ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'
- 这时候就可以进入到mysql里面去了
[root@hf-01 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
- 创建库
- create database zrlog;
- create database test1;
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> quit
Bye
退出数据库
- 然后将数据库做一个恢复
- mysql -uroot test < /tmp/test.sql
[root@hf-01 ~]# mysql -uroot test < /tmp/test.sql
[root@hf-01 ~]# mysql -uroot zrlog < /tmp/zrlog.sql
[root@hf-01 ~]#
- 保证两边数据一致
- 然后查看/data/mysql/目录下的数据是否和主机器上的/data/mysql/目录是否一致
- 开始实现主从
- 在从机器登录到mysql
[root@hf-01 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
- 然后在数据库里面执行命令,停止slave
- stop slave;
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
- 配置主机器相关配置
- change master to master_host='192.168.202.130', master_user='repl', master_password='hanfeng', master_log_file='hf123.000001', master_log_pos=10549;
- master_host='192.168.202.130',指定主机器host
- master_user='repl',指定主机器用户
- master_password='hanfeng',指定主机器密码
- master_log_file='hanfeng1.000001',指定binlog文件名
- master_log_pos=10549,指定binlog文件大小
- 也可以指定主机器的port,因为在生产环境中,也会有人更改mysql的默认端口 master_port=3306
mysql> change master to master_host='192.168.202.130', master_user='repl', master_password='hanfeng', master_log_file='hf123.000001', master_log_pos=10549;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
- 开始slave
- start slave;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql>
- 这时候通过 show slave statusG 判断主从是否配置成功
查看slave配置
- show slave statusG
- 在G后面不需要加分号,G本身就是一种结束符
- 看 Slave_IO_Running: Yes 是否为yes
- 看 Slave_SQL_Running: Yes 是否为yes
mysql> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.202.130
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: hf123.000001
Read_Master_Log_Pos: 10549
Relay_Log_File: hf-01-relay-bin.000002
Relay_Log_Pos: 279
Relay_Master_Log_File: hf123.000001
Slave_IO_Running: Yes //检查是否为Yes
Slave_SQL_Running: Yes //检查是否为Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 10549
Relay_Log_Space: 452
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: 130
Master_UUID: fd146e3b-efca-11e7-b3d6-000c29ff458f
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)
mysql>
- 解锁“主”上的表(在主上机器操作)
- unlock tables;
[root@hf-01 mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 15
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> unlock table;
Query OK, 0 rows affected (0.00 sec)
- 到这里主从搭建就算完成了
查看主从同步是否正常
- 从上执行mysql -uroot
- show slave stautsG
- 看是否有
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- 还需关注
- Seconds_Behind_Master: 0 //为主从延迟的时间
- Last_IO_Errno: 0
- Last_IO_Error:
- Last_SQL_Errno: 0
- Last_SQL_Error:
- WCF系列教程之消息交换模式之请求与答复模式(Request/Reply)
- Koa-router源码解读
- WCF系列教程之初识WCF
- IntelliJ IDEA 缓存和索引的介绍及清理方法
- Node.js原理
- WCF系列教程之WCF消息交换模式之单项模式
- React Native调用Android相机图库
- IntelliJ IDEA 之 HelloWorld 项目创建及相关配置文件介绍
- 设置 IntelliJ IDEA 主题和字体的方法
- 修改 IntelliJ IDEA 模板注释中的 user 内容
- Android仿京东、天猫商品详情页
- C# checked和unchecked运算符
- 迭代子模式
- WCF系列教程之WCF中的会话
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- MySQL 8.0新特性 — 函数索引
- Docker快速上手指北(一)【技术创作101训练营】
- leetcode树之二叉搜索树的最近公共祖先
- 【技术创作101训练营】技术角 | 在CentOS 8上使用Nginx 1.18: 基本配置
- Java诊断应用之Arthas实战(技术创作101训练营)
- 突击并发编程JUC系列-数组类型AtomicLongArray
- 个人量化投资体系搭建(一)
- 服务端的 WebAssembly 与 Rust 入门篇
- pImpl
- Flask+requests发起页面请求示例
- 【技术创作101训练营】Git 如何成功配置SSH key连接多个代码平台?
- 深入浅出iOS内存管理-技术创作101训练营
- 聊聊原型 Prototype | 技术创作101训练营
- Excelize 2.3.1 发布,Go 语言 Excel 文档基础库,支持加密表格文档
- PUMA560机器人工具箱运动控制A:路径规划-运动学