Master 服务器上建立用于 Slave 服务器复制数据的帐户

[root@master ~]# mysql

mysql> grant replication slave,replication client on *.* to 'allentuns'@'192.168.2.200' identified by '1234@6@'; Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

113

mysql> show grants
for 'allentuns'@'192.168.2.200';  #查看授权用户

+-----------------------------------------------------------------------------------------------------------------------------------

| Grants for allentuns@192.168.2.200|

+-----------------------------------------------------------------------------------------------------------------------------------

| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'allentuns'@'192.168.2.200' IDENTIFIED BY PASSWORD '*

7B8E3D52A612E2CB04E31B43FCDC20A07317E332'
|

+-----------------------------------------------------------------------------------------------------------------------------------

1 row in set (0.00 sec)

 Slave 服务器上使用授权用户连接测试

[root@slave ~]# mysql -uallentuns -p1234@6@ -h 192.168.2.100

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 6

Server version: 5.6.12-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, 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

修改 Master 服务器上的 Mysqld 主配置文件

[root@master ~]# vim /usr/local/mysql/my.cnf
log-bin = master-bin                          #二进制日志文件
binlog_format = mixed  #二进制日志文件索引 server-id = 1     #用于识别的 ID

port = 3306            #Mysql 的默认端口号 basedir = /usr/local/mysql/ #Mysql 源程序目录
datadir = /mydata/data      #数据存储目录路径

socket = /var/lib/mysql/mysql.sock #套接字文件路径

innodb_file_per_table = 1    #每表一个文件

114

[root@master ~]# service mysqld restart

修改 Slave 服务器上的 Mysqld 主配置文件

[root@slave ~]# vim /usr/local/mysql/my.cnf

#log-bin = master-bin  #注释二进制日志文件,如果当其它从服务器的主服务器,否则关闭

#binlog_format = mixed #注释此行

skip_slave_start = 1    #启动服务时不自动启动从服务线程

read_only = 1           #设置 Slave 服务器为只读

server-id = 10 relay_log = relay_log relay_log_index = relay_log.index port = 3306

basedir = /usr/local/mysql/

datadir = /mydata/data

socket
= /var/lib/mysql/mysql.sock innodb_file_per_table = 1

[root@slave ~]# service mysqld restart

 查看 Master 服务器的二进制日志及二进制日志事件位置用于 Slave

服务器复制

mysql> RESET MASTER;   #清空二进制日志
Query OK, 0 rows affected (0.05 sec) mysql> show master status;

+-------------------+----------+--------------+------------------+-------------------+

| File             |
Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+-------------------+----------+--------------+------------------+-------------------+

| master-bin.000003 |     120 |             |                  |                   |

+-------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

注释:File:表示从此日志开始复制    Position:表示从这个事件开始复制 偏移位

115

 Slave 服务器上同步 Master 服务器上面的数据

mysql> CHANGE MASTER TO

->  MASTER_HOST='192.168.2.100',

->  MASTER_USER='allentuns',

->  MASTER_PASSWORD='1234@6@',

->  MASTER_PORT=3306,

->
 MASTER_LOG_FILE='master-bin.000003',

->  MASTER_LOG_POS=120;

Query OK, 0 rows affected, 2 warnings (0.38 sec)
mysql> help change master to #获取帮助信息 Name: 'CHANGE MASTER TO'

Description:
Syntax:

CHANGE MASTER TO option [,
option] ...
CHANGE MASTER TO MASTER_HOST='master2.mycompany.com', MASTER_USER='replication',
MASTER_PASSWORD='bigs3cret', MASTER_PORT=3306, MASTER_LOG_FILE='master2-bin.001', MASTER_LOG_POS=4,
MASTER_CONNECT_RETRY=10;

 启动 Slave 服务器的复制线程并查看状态

mysql> start
slave;    #启动 Slave 服务器线程 Query OK, 0 rows affected (0.02 sec)

mysql> show
slave status\G;

116

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.100                                               #Master 服务器地址
Master_User: allentuns      #连接
Master 服务器用户名 Master_Port: 3306     #Master 服务器的监听端口
Connect_Retry: 60                   #重试时间间隔

Master_Log_File: master-bin.000003    #I/O 线程读取的二进制日志文件 Read_Master_Log_Pos: 120                                                             #I/O 线程读取的二进制日志文件事件位置
Relay_Log_File: relay_log.000002                      #SQL 线程正在读取的中继日志文件
Relay_Log_Pos: 284    #SQL 线程读取和执行的中继日志文件事件位置
Relay_Master_Log_File: master-bin.000003

Slave_IO_Running: Yes                  #Slave 服务器的 IO 线程状态 Slave_SQL_Running: Yes                                                      #Slave 服务器的 SQL 线程状态

Replicate_Do_DB: #下面 Replicate 开头的表示用来指明哪些库或者表在复制时不需要同步 Replicate_Ignore_DB:

Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:

Last_Errno: 0                    #SQL 线程读取日志参数的错误数量
Last_Error:       #SQL 线程读取日志参数的错误消息

Skip_Counter: 0                    #最近被用于 SQL_SLAVE_SKIP_COUNTER 的值 Exec_Master_Log_Pos: 120

Relay_Log_Space: 451

Until_Condition: None                 #所有原有中继日志的总大小 Until_Log_File:

Until_Log_Pos: 0

117

Master_SSL_CA_File:

Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher:
Master_SSL_Key:

Seconds_Behind_Master: 0                   #落后于 Master 服务器的时间
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: 1

Master_UUID: 84774a86-3ee8-11e4-a268-000c29ad35d7

Master_Info_File: /mydata/data/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 u
pdate it

Master_Retry_Count: 86400

Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: Auto_Position: 0

ERROR:

No query specified

Slave 服务器查看启动的线程

[root@slave ~]# mysql -e
"show processlist;"

+----+-------------+-----------+------+---------+------+------------------------------------------------------------------------

| Id | User       | Host      | db
 |
Command |
Time |
State                                                                   | Info            |

+----+-------------+-----------+------+---------+------+------------------------------------------------------------------------

|  3 | system user |         | NULL | Connect |  533 | Waiting for master to send event                                           | NULL

|

|  4 | system user |          | NULL | Connect |
 533
| Slave has read all relay log; waiting for the slave I/O thread to update i t |
NULL                 |

|  5 |
root      |
localhost |
NULL | Query
 |   0 |
init                                                                  | show processlist |

 Master 服务器创建数据库并在 Slave 服务器上验证是否存在

###在 Master 服务器创建数据库并查看

[root@master ~]# mysql -e 'create database mydbtest;' [root@master ~]# mysql -e 'show databases;'

| Database           |

+--------------------+

| information_schema |

| mydbtest           |

| mysql              |

| performance_schema |

| test                |

###在 Slave 服务器查看是否有'mydbtest'数据库

[root@slave ~]# mysql -e 'show databases;'

| Database           |

+--------------------+

| information_schema |

| mydbtest           | #数据库已经成功同步到 slave 服务器

| mysql              |

| performance_schema |

| test                |

 Master Slave 服务器查看二进制日志事件位置已更新

###查看 Master 服务器

[root@master ~]# mysql -e 'show master status;'

+-------------------+----------+--------------+------------------+-------------------+

| File             |
Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

###查看 Slave 服务器

[root@slave ~]# mysql -e
'show slave status\G;' |grep 'Read_Master_Log_Pos'
Read_Master_Log_Pos: 226

5.主从监控

5.1Linux 系统 sendmail 发邮件到 139 外部邮箱

1、安装 sendmail 和 mailx

# yum -y
install sendmail mailx

2、修改配置文件

# cp /etc/mail.rc /etc/mail.rc.bak

# cat >>
/etc/mail.rc << EOF

set from=13260071987@139.comsmtp=smtp.139.com

set smtp-auth-user=13260071987@139.comsmtp-auth-password=yi15093547036 smtp- auth=login

EOF

3、重新启动服务

# service sendmail restart

4、发送测试邮件

echo "I Love You"
|mail -s "邮件主题:MIS you" 13260071987@139.com

5.2 分别在从服务器(Slave)上创建登陆用户,只限定本地运行,保证安全

mysql> grant all privileges on *.* to "zhengyansheng"@"127.0.0.1"
identified by "passwor

Query OK, 0 rows affected (0.04 sec)

mysql> grant all privileges on *.* to "zhengyansheng"@"localhost" identified by "passwor
d123";

Query OK, 0 rows affected (0.00 sec)

5.3Mysql 监控脚本

#!/bin/bash

#check MySQL_Slave Status

#crontab time 00:10

MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:"
"]+ '{print $4}'`
MYSQLIP=`ifconfig eth1|grep "inet addr" | awk -F[:" "]+ '{print $4}'`
STATUS=$(/usr/local/mysql/bin/mysql -u zhengyansheng -ppassword123 -S /tmp/mysql.
sock -e "show slave status\G" | grep -i
"running")

IO_env=`echo $STATUS | grep IO | awk ' {print $2}'`
SQL_env=`echo $STATUS | grep SQL | awk '{print $2}'`
DATA=`date +"%y-%m-%d %H:%M:%S"`

if [ "$MYSQLPORT" == "3306" ]

then

echo "mysql is running"
else

mail
-s "warn!server: $MYSQLIP mysql is down"
13260071987@139.comfi

if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]

then

echo "Slave is running!" else

echo "####### $DATA #########">>     /mydata/check_mysql_log/check_mysql_slave.l

echo "Slave is not running!"
>>    /mydata/check_mysql_log/check_mysql_slave.log

echo "Slave is not running!"
| mail -s "warn! $MYSQLIP MySQL Slave is not running" 1326

0071987@139.comfi

5.4 定时执行监控脚本

[root@slave ~]# crontab -l

*/1 * * * * root /bin/sh /mydata/check_mysql_health.sh

5.5 测试:停止 slave 进程,看是否能收到邮件

mysql> stop slave;

Query OK, 0 rows affected (0.01 sec)

mysql> show
slave status\G

*************************** 1. row *************************** Slave_IO_State:

Master_Host: 192.168.2.100

Master_User: allentuns

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: master-bin.000003

Read_Master_Log_Pos: 226

Relay_Log_File: relay_log.000005

Relay_Log_Pos: 284

Relay_Master_Log_File: master-bin.000003

Slave_IO_Running: No #IO 线程已经停止 Slave_SQL_Running: No #SQL 线程已经停止 Replicate_Do_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: 226

Relay_Log_Space: 615

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: NULL 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: 1

Master_Info_File: /mydata/data/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
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)

主从半同步复制

linux MySql 在 Master 主从复制配置的更多相关文章

  1. mysql 5.5主从复制配置

    首先将主库现有的要实现主从的数据库原原本本地复制到从库上,目的是一开始就让主从同步,让binlog日志从最新的记录开始同步! 备份: 方法1:快捷导出所要的库如(库goods)[注意:该方法仅适合My ...

  2. mysql 8.0 主从复制配置

    背景: 主库: 192.168.211.128 从库: 192.168.211.129 一.关闭防火墙 [root@node01 ~]# systemctl disable firewalld [ro ...

  3. Mysql安装及主从复制配置

    1.下载 mysql数据库 wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.9-linux-glibc2.5-x86_64.ta ...

  4. Windows环境和Linux环境下Redis主从复制配置

    Windows环境下和Linux环境下配置Redis主从复制基本上一样,都是更改配置文件.Windows环境下修改的配置文件是:redis.windows.conf.redis.windows-ser ...

  5. Linux下mysql的主从复制配置

    1.准备两台数据库环境,或者单台多实例环境,能正常启动和登录. 数据库的安装和多实例配置请参考https://www.cnblogs.com/qiuhom-1874/p/9757061.html. 2 ...

  6. Linux 笔记 - 第二十三章 MySQL 主从复制配置

    一.前言 MySQL Replication,也被称为主从复制.AB 复制.简单来说就是 A 和 B 两台服务器做主从后,在 A 服务器上写入数据,B 服务器上也会跟着写入输入,两者之间的数据是实时同 ...

  7. linux mysql主从复制配置

    1.设置主库master的servie-id值并且开启bin-log功能参数vi /etc/my.cnf修改my.cnf的参数:[mysqld]server-id=1 //每一个库的server-id ...

  8. Linux MySQL主从复制(Replication)配置

    MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器(主服务器master)复制数据到另一个服务器(从服务器slave)的一个进程. 配置主服务器(mast ...

  9. linux mysql主从复制

    centos7 安装 mariadb 1 yum 源  -- 配置阿里的 2 rmp 方式 3 源码编译方式  -- 专业DBA 一些知识点: 虚拟环境 不影响 redis/ mariadb/mysq ...

随机推荐

  1. Http请求详解(转)----请求+响应各字段详解

    参考HTTP深入浅出http请求(转)-----http请求的过程和实现机制 1. HTTP请求格式 首先介绍HTTP协议:超文本传输协议(HTTP,HyperText Transfer Protoc ...

  2. 一款新的好用的SSH工具——FinalShell,比XShell更牛逼~

    FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运维工具,充分满足开发,运维需求.特色功能:免费海外服务器远程桌面加速,ssh加速,双边tcp加速,内网穿 ...

  3. C++获取寄存器eip的值

    程序中需要打印当前代码段位置 如下 #include <stdio.h> #include <stdlib.h> #include <math.h> #ifdef ...

  4. 技术文档翻译-------glove readme(1)

    Package Contents To train your own GloVe vectors, first you tools. An example is included in demo.sh ...

  5. ICML 2019 分析

    ICML 2019 分析 Word Embeddings Understanding the Origins of Bias in Word Embeddings Popular word embed ...

  6. 【不错】MySQL 事务隔离级别

    一.事务描述 1.事务的四个特性 ACID 1. A:原子性 = 一个事务或者都成功.或者都失败: 2. C:一致性 = 在整个事务的生命周期里面,查询到的数据是一致的: MVCC多版本并发控制:利用 ...

  7. lsb-realse

    [root@localhost ~]# lsb_release -a -bash: lsb_release: command not found 解决方法:yum install redhat-lsb ...

  8. LeetCode链表简单题

    一.21合并两个有序链表 代码如下: class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNod ...

  9. 20191110 Spring Boot官方文档学习(4.2)

    4.2.外部化配置 Spring Boot使您可以外部化配置,以便可以在不同环境中使用相同的应用程序代码.您可以使用Properties文件,YAML文件,环境变量和命令行参数来外部化配置.属性值可以 ...

  10. 20191127 Spring Boot官方文档学习(5)

    5.Spring Boot Actuator:可投入生产的功能 Spring Boot包含许多其他功能,可帮助您在将应用程序投入生产时监控和管理您的应用程序.您可以选择使用HTTP端点或JMX管理和监 ...