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 ~]#
- 主上操作完成,接下来从上操作
- 微信企业号二次开发--自定义菜单接口开发--应用中心
- 微信最新自定义菜单事件
- 微信JSSDK分享到朋友圈和朋友自定义内容功能实现
- 无线安全渗透测试套件WiFi-Pumpkin新版本发布
- Java使用QQ邮箱给其他邮箱发邮件
- 从wireshark抓包开始学习https
- 用于时间序列预测的Python环境
- Pandoc安装实现Markdown转PDF (CentOS6)
- String类replaceAll方法正则替换深入分析
- 微信硬件平台对接--蓝牙
- 初试git+github(linux环境)
- Raspberry PI Nginx 安装
- 微信支付-Java-SpringMVC基础一些代码支持
- 使用Kafka的High Level Consumer
- 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 数组属性和方法
- 【Linux系列-1】top命令详解
- 【Linux系列-2】iostat命令详解
- 【Mybatis-1】MyBatis注解版详解
- 【MyBatis-2】MyBatis之xml 配置版
- 【MyBatis-3】MyBatis xml映射文件详解
- 【剑指Offer】II-数组中数字出现的次数 II
- 【MyBatis-4】MyBatis之动态SQL
- 【MyBatis-4】MyBatis批量insert、update、delete数据
- 【redis6.0.6】深入源码,一探究竟 -- redis服务器开机自启动
- 不是你记忆中的单例模式,但适用的程度,更胜一筹
- 安卓模拟器修改hosts文件
- 为容器化的 Postgres 数据库启用 ssl 连接
- 【SpringBoot web-1】web项目数据校验
- 【SpringBoot web-2】web项目参数传递
- 筛法求素数