前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >17.4 配置从

17.4 配置从

作者头像
运维小白
发布2018-02-07 10:44:10
6430
发布2018-02-07 10:44:10
举报
文章被收录于专栏:运维小白运维小白运维小白

主从配置 - 从上操作

  • 安装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

主从配置 - 从上操作

  • 在从上机器配置
  1. 首先在从上安装并启动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
保存退出
  1. 重启mysql服务
[root@hf-01 ~]#  /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL... SUCCESS! 
[root@hf-01 ~]# 
  1. 在增加 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
  1. 把主机器上备份的 .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 ~]# 
  1. 进入到从上数据库中——>因为从上没设置密码,所以可以直接进去
[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> 
  1. 创建库
  • 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

退出数据库
  1. 然后将数据库做一个恢复
  • 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 ~]# 
  • 保证两边数据一致
  1. 然后查看/data/mysql/目录下的数据是否和主机器上的/data/mysql/目录是否一致
  2. 开始实现主从
  3. 在从机器登录到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> 
  1. 然后在数据库里面执行命令,停止slave
  • stop slave;
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 
  1. 配置主机器相关配置
  • 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)
  1. 开始slave
  • start slave;
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> 
  1. 这时候通过 show slave status\G 判断主从是否配置成功

查看slave配置

  • show slave status\G
    • 在G后面不需要加分号,\G本身就是一种结束符
    • 看 Slave_IO_Running: Yes 是否为yes
    • 看 Slave_SQL_Running: Yes 是否为yes
mysql> show slave status\G
*************************** 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> 
  1. 解锁“主”上的表(在主上机器操作)
  • 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)
  1. 到这里主从搭建就算完成了

查看主从同步是否正常

  • 从上执行mysql -uroot
  • show slave stauts\G
  • 看是否有
  • 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:
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 主从配置 - 从上操作
  • 主从配置 - 从上操作
  • 查看主从同步是否正常
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档