17.3 配置主
时间:2022-04-27
本文章向大家介绍17.3 配置主,主要内容包括主从配置 - 主上操作、主从配置 - 主上操作、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
主从配置 - 主上操作
- 安装mysql
- 修改my.cnf,增加server-id=130和log_bin=aminglinux1
- 修改完配置文件后,启动或者重启mysqld服务
- 把mysql库备份并恢复成aming库,作为测试数据
- mysqldump -uroot mysql > /tmp/mysql.sql
- mysql -uroot -e “create database aming”
- mysql -uroot aming < /tmp/mysql.sql
- 创建用作同步数据的用户
- grant replication slave on . to 'repl'@slave_ip identified by 'password';
- flush tables with read lock;
- show master status;
主从配置 - 主上操作
- 在两台机器安装并启动mysql服务后,首先在主上进行操作
- 修改/etc/mys.cnf配置文件
- 在配置文件下[mysqld]下添加
- server-id=130 这个id可以自定义,这里根据ip来定义
- log_bin=hf123 打开binlog,名字自定义为log_bin=hf123 最终如下
[root@hanfeng ~]# vim /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
server_id = 130
log_bin=hf123
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
保存退出
- 更改完配置文件后,需要重启mysql
- /etc/init.d/mysqld restart
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL.. SUCCESS!
[root@hanfeng ~]#
- 这时候来/data/mysql/目录下,会生成一些文件
- ls -lt /data/mysql/
[root@hanfeng ~]# ls -lt /data/mysql/
总用量 110728
-rw-rw----. 1 mysql mysql 50331648 1月 19 23:35 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 1月 19 23:35 ibdata1
-rw-rw----. 1 mysql mysql 104317 1月 19 23:35 hanfeng.err
-rw-rw----. 1 mysql mysql 5 1月 19 23:35 hanfeng.pid
-rw-rw----. 1 mysql mysql 15 1月 19 23:35 hf123.index
-rw-rw----. 1 mysql mysql 120 1月 19 23:35 hf123.000001
drwx------. 2 mysql mysql 4096 1月 19 20:58 zrlog
-rw-rw----. 1 mysql mysql 56 1月 2 22:41 auto.cnf
drwx------. 2 mysql mysql 4096 1月 2 22:28 mysql
drwx------. 2 mysql mysql 4096 1月 2 22:28 performance_schema
-rw-rw----. 1 mysql mysql 50331648 1月 2 22:28 ib_logfile1
drwx------. 2 mysql mysql 6 1月 2 22:26 test
[root@hanfeng ~]#
- 其中 .index 索引页,这个文件是必须要有的
- 其中 .000001 这个是二进制日志文件,会持续生成2、3、4等等(这个文件是实现主从配置的根本,没有这个文件根本没有办法完成主从)
- 测试,准备一个数据做演示用的
- 首先做一个备份
- mysqldump -uroot -phanfeng zrlog > /tmp/zrlog.sql
[root@hanfeng ~]# mysqldump -uroot -phanfeng zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
- 然后创建一个新的库
- mysql -uroot -phanfeng -e "create database han"
[root@hanfeng ~]# mysql -uroot -phanfeng -e "create database han"
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
- 创建好库后,还需要把数据恢复一下,那也就是说做的主从,参考的对象就是 han 这个库
- mysql -uroot -phanfeng han < /tmp/zrlog.sql
[root@hanfeng ~]# mysql -uroot -phanfeng han < /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
- 再来查看/data/mysql/目录下的文件
- ls -lt /data/mysql/
[root@hanfeng ~]# ls -lt /data/mysql/
总用量 110740
-rw-rw----. 1 mysql mysql 50331648 1月 19 23:40 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 1月 19 23:40 ibdata1
-rw-rw----. 1 mysql mysql 10337 1月 19 23:40 hf123.000001
drwx------. 2 mysql mysql 4096 1月 19 23:40 han
-rw-rw----. 1 mysql mysql 104317 1月 19 23:35 hanfeng.err
-rw-rw----. 1 mysql mysql 5 1月 19 23:35 hanfeng.pid
-rw-rw----. 1 mysql mysql 15 1月 19 23:35 hf123.index
drwx------. 2 mysql mysql 4096 1月 19 20:58 zrlog
-rw-rw----. 1 mysql mysql 56 1月 2 22:41 auto.cnf
drwx------. 2 mysql mysql 4096 1月 2 22:28 mysql
drwx------. 2 mysql mysql 4096 1月 2 22:28 performance_schema
-rw-rw----. 1 mysql mysql 50331648 1月 2 22:28 ib_logfile1
drwx------. 2 mysql mysql 6 1月 2 22:26 test
[root@hanfeng ~]#
- 能看到hf123.000001二进制文件是有增加的,hf123.000001增长的大小是和zrlog这个库的保持一致的,hf123.000001文件里完整的记录了数据库的创建的库,创建的表,以及表里的内容全都有
- 下面创建用于主从相互同步数据的用户
- 先进入到mysql里面去
- mysql -uroot -phanfeng
[root@hanfeng ~]# mysql -uroot -phanfeng
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 194
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>
- 创建用户
- grant replication slave on . to 'repl'@'192.168.202.131' identified by 'hanfeng';
- 指定权限,replication slave权限
- 针对repl这个用户
- 针对 从 的那个IP,指定来源(若是写所有的IP会很危险)
mysql> grant replication slave on *.* to 'repl'@'192.168.202.131' identified by 'hanfeng';
Query OK, 0 rows affected (0.00 sec)
mysql>
- 锁定表,目的是不让表继续写,因为一会需要做 从 机器配置,需要进行一个同步,让两台机器同步,保证两台机器的数据一致,同步才不会出错
- flush tables with read lock;
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql>
- 查看一下binlog的文件和大小,并记住binlog的filename
- show master status;
mysql> show master status;
+--------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------+----------+--------------+------------------+-------------------+
| hf123.000001 | 10549 | | | |
+--------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> quit
Bye
- 然后退出数据库,做一个数据同步
- 查看/data/mysql/下有哪些库,主上有哪些库,一会从上也得有哪些库,同步这些库,就意味着这些数据都得备份过去
[root@hanfeng ~]# ls /data/mysql
auto.cnf hanfeng.err hf123.000001 ibdata1 ib_logfile1 performance_schema zrlog
han hanfeng.pid hf123.index ib_logfile0 mysql test
[root@hanfeng ~]#
- 备份数据库,除了mysql库,因为mysql库里面有账号密码,从上的时候不可能把所有权限复制过去,所以mysql不需要备份
- 备份其他的库
[root@hanfeng ~]# mysqldump -uroot -phanfeng test > /tmp/test.sql
Warning: Using a password on the command line interface can be insecure.
[root@hanfeng ~]#
- 等会把/tmp/目录下 .sql文件都拷贝到 从上 去
[root@hanfeng ~]# ls /tmp/*sql
/tmp/test.sql /tmp/zrlog.sql
[root@hanfeng ~]#
- 主上操作完成,接下来从上操作
- Python机器学习库:Scikit-Learn简介
- 很简单的企业管理器---我写程序的方式,几个自定义控件。
- 使命必达: 深入剖析WCF的可靠会话[原理揭秘篇](上)
- 其实添加数据也可以这样简单——表单的第三步抽象(针对UI及后置代码)
- 为Symfony2和Redis正名,基于PHP的10亿请求/周网站打造
- 如何使用Python基线预测进行时间序列预测
- 如何使用统计显着性检验来解释机器学习结果
- 其实添加数据也可以这样简单——表单的第一步抽象(针对数据访问层)《怪怪设计论: 抽象无处不在 》有感
- WCF服务端运行时架构体系详解[上篇]
- 使命必达: 深入剖析WCF的可靠会话[编程篇](下)
- 在网页里让文本框只能输入数字的一种方法。外加回车换Tab
- 如何用Python从零开始实现简单的线性回归
- 使命必达: 深入剖析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 数组属性和方法
- Redis排行榜的设计与实现
- 血的教训 | 一次订单号重复的事故差点被开除
- 60分钟看懂HMM的基本原理
- R语言确实会蛮耗费磁盘空间哦
- 很多时候你就是不知道如何提问
- seurat标准流程实例之2个10x样本的项目(GSE135927数据集)
- 使用 Jenkins 和 Ansible 实现 CI/CD
- 太刺激了,面试官让我手写跳表,而我用两种实现方式吊打了TA!
- 被 Google 选择的下一代数据面 Cilium 是什么 - 上手实践
- K8S 生态周报| 是时候从 k8s v1.16 升级了
- ESP8266(一)| 基于OneNet的温度采集显示系统
- 隐秘的 MySQL 类型转换
- 对Jenkinsfile语法说不,开源项目Jenkins Json Build挺你
- Volatile概述
- SparkStreaming和Kafka基于Direct Approach如何管理offset