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;

主从配置 - 主上操作

  1. 在两台机器安装并启动mysql服务后,首先在主上进行操作
  2. 修改/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 

保存退出
  1. 更改完配置文件后,需要重启mysql
  • /etc/init.d/mysqld restart
[root@hanfeng ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@hanfeng ~]# 
  1. 这时候来/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等等(这个文件是实现主从配置的根本,没有这个文件根本没有办法完成主从)
  1. 测试,准备一个数据做演示用的
  2. 首先做一个备份
  • 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 ~]# 
  1. 然后创建一个新的库
  • 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 ~]# 
  1. 创建好库后,还需要把数据恢复一下,那也就是说做的主从,参考的对象就是 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 ~]# 
  1. 再来查看/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 ~]# 
  1. 能看到hf123.000001二进制文件是有增加的,hf123.000001增长的大小是和zrlog这个库的保持一致的,hf123.000001文件里完整的记录了数据库的创建的库,创建的表,以及表里的内容全都有
  2. 下面创建用于主从相互同步数据的用户
  3. 先进入到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> 
  1. 创建用户
  • 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> 
  1. 锁定表,目的是不让表继续写,因为一会需要做 从 机器配置,需要进行一个同步,让两台机器同步,保证两台机器的数据一致,同步才不会出错
  • flush tables with read lock;
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> 
  1. 查看一下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
  1. 然后退出数据库,做一个数据同步
  2. 查看/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 ~]# 
  1. 备份数据库,除了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 ~]# 
  1. 等会把/tmp/目录下 .sql文件都拷贝到 从上 去
[root@hanfeng ~]# ls /tmp/*sql
/tmp/test.sql  /tmp/zrlog.sql
[root@hanfeng ~]# 
  1. 主上操作完成,接下来从上操作