普通用户Mysql 5.6.13 主从,主主及nagios的mysql slave监控

时间:2022-05-31 06:40:24

Master:192.168.209.19

Slave:192.168.209.20

mysql版本:mysql5.6.13

 

1. 以root身份创建普通用户,如mysql,并创建mysql安装目录:

   # useradd mysql

   # passwd mysql

   # mkdir /mysql

   # chown mysql:mysql /mysql

安装依赖包:

yum -y install  gcc gcc-c++ autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make cmake

2. 下载MySQL5.6.13完整开发版, 并将其拷贝到/mysql目录下.

   文件: mysql-5.6.13.tar.gz

 

3. 以mysql用户解包MySQL,并将MySQL解包后的目录更改为/mysql/:

   # su - mysql

   $ cd

   $ mv mysql-5.6.13.tar.gz /mysql

   $ tar xvzf mysql-5.6.13.tar.gz

4. 配置MySQL:

   $ cd /mysql/5.6.13

$cmake -DCMAKE_INSTALL_PREFIX=/mysql/mysql-5.6

$make

$make install

$cd /mysql/mysql-5.6/scripts

$ ./mysql_install_db --user=mysql --basedir=/mysql/mysql-5.6 --datadir=/mysql/mysql-5.6/data

#cp /mysql/mysql-5.6/support-files/mysql.server /etc/init.d/mysql

#cp /mysql/mysql-5.6/support-files/my-default.cnf /etc/my.cnf

启动mysql

$/mysql/mysql-5.6/support-files/mysql.server start

error解决:

[ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist

初始化数据库就ok,即:./mysql_install_db --user=mysql --basedir=/mysql/mysql-5.6 --datadir=/mysql/mysql-5.6/data

5.配置主从:

5.1、主从服务器分别操作:

  5.1.1、mysql版本一致

  5.1.2、初始化表,启动mysql

  5.1.3、设置mysql root密码, /mysql/mysql-5.6/bin/mysqladmin -u root password ‘123456’

5.2、修改master:

   #vi /etc/my.cnf

       [mysqld]

       log-bin=mysql-bin   #启用二进制日志

       server-id=20       #服务器ID标示,主从不一致

 

5.3、修改slave:

   #vi /etc/my.cnf

       [mysqld]

       log-bin=mysql-bin   #启用二进制日志

       server-id=21      #服务器ID标示,主从不一致

5.2 登录mysql master配置

#/mysql/mysql-5.6/bin/mysql -uroot -p123456 

   mysql>GRANT REPLICATION SLAVE ON *.* to 'test'@'%' identified by '123456'; 

#一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.209.20,加强安全。

 

6、登录主服务器的mysql,查询master的状态

   mysql>show master status;

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

   | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

   | mysql-bin.000007 |      320 |              |                  |

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

   1 row in set (0.00 sec)

   注:获取file和position。

 

7、配置从服务器mysql Slave:

   mysql>change master to master_host='192.168.209.19',master_user='test',master_password='123456',master_log_file='mysql-bin.000007',master_log_pos=320;   #320为主服务器的position,无单引号

   Mysql>start slave;    //启动从服务器复制功能

8、检查slave状态:

mysql> show slave status\G

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.209.19

                  Master_User: mysync

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000007

          Read_Master_Log_Pos: 320

               Relay_Log_File: cdn20-relay-bin.000002

                Relay_Log_Pos: 283

        Relay_Master_Log_File: mysql-bin.000007

             Slave_IO_Running: Yes

            Slave_SQL_Running: 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: 320

              Relay_Log_Space: 456

              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: 20

                  Master_UUID: 6a36aa69-1c81-11e4-8213-b8ca3af2484f

             Master_Info_File: /mysql/mysql-5.6/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 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)

 

Slave_IO及Slave_SQL进程YES状态,否则都是错误的状态(如:其中一个NO均属错误)。

至此,主从服务器配置完成。

 

9、主从服务器测试:

主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:

mysql> create database test1;

Query OK, 1 row affected (0.00 sec)

 

mysql> use test1

Database changed

mysql> create table test1(id int(4),name char(8));

Query OK, 0 rows affected (0.01 sec)

 

mysql> insert into test values(001,'test1');

ERROR 1146 (42S02): Table 'test1.test' doesn't exist

mysql> insert into test1 values(001,'test1');

Query OK, 1 row affected (0.00 sec)

 

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

| test1              |

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

5 rows in set (0.00 sec)

 

从服务器Mysql查询:

 

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

| test1              |

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

5 rows in set (0.01 sec)

至此,mysql主从配置完成,可以正常使用;后续会加入集群模式。

 

10、nagios mysql slave 监控

因为网上没完整的mysql集群监控插件,写个简单的脚本监控mysql主从,只需要传入几个简单参数即可实现,脚本如下:

[root@host1 objects]# cat /usr/local/nagios/libexec/check_mysql_slave.sh

#!/bin/bash

 

host=$1

mysqluser=$2

mysqlpasswd=$3

mysqlport=$4

Slave_Yes=`mysql -h $host -u$mysqluser -p$mysqlpasswd -P $4 -e 'show slave status\G' | grep -c Yes`

        if [ $Slave_Yes -eq 2 ];then

                echo "Mysql slave is OK!"

        exit 0;

        else

        echo "Mysql slave is Error!";

        exit 2;

        fi

 

定义command.cfg

#check_mysql_slave

define command{

        command_name check_mysql_slave

        command_line $USER1$/check_mysql_slave.sh $HOSTADDRESS$ $USER7$ $USER8$ $USER9$

}

 

定义主机配置

define service{

        use                             local-service         ; Name of service template to use

        host_name                       hostname

        service_description             MySQL_AB

        check_command                   check_mysql_slave

        notifications_enabled           0

        }

 

 

 

同时,需要将mysqlusermysqlpasswdmysqlport都写入到resource.cfg文件中

/usr/local/nagios/etc/resource.cfg

$USER7$=root

$USER8$=passwd

$USER9$=3306

 

配置完成之后,reload一下nagios配置即可。

具体监控如下:

 普通用户Mysql 5.6.13 主从,主主及nagios的mysql slave监控

 

=================MySQL 主主=================

MySQL主主复制,两台服务器的任何一台上面的数据库存发生了改变都会同步到另一台服务器上,两台服务器互为主从,并且都能向外提供服务。

详情如下:

server1:192.168.203.10

server2:192.168.203.20

OS:CentOS release 6.3 (Final) 64bit

mysql版本:mysql-5.6.13

一、创建并授权mysql用户

这一步在每一台(主)服务器上创建一个用户,并为之授权,使它们可以互相访问彼此的数据库

server1

创建允许server2来访问的用户test2,密码为:123456

mysql> GRANT REPLICATION SLAVE ON *.* TO 'test2'@'192.168.203.20'  IDENTIFIED BY '123456';

server2

创建一个允许server1来访问的用户test1,密码为:123456

mysql> GRANT REPLICATION SLAVE ON *.*  TO 'test1'@'192.168.203.10'  IDENTIFIED BY '123456';

二、修改MySQL配置文件

MySQL的配置文件中修改/etc/my.cnf

server1如下:

 [mysqld]

 server-id = 10

 log-bin = mysql-bin

 replicate-do-db = mysqldb

 auto-increment-increment = 2

 auto-increment-offset = 1

# service mysqld restart

server2如下

 [mysqld]

 server-id = 20

 log-bin = mysql-bin

 replicate-do-db = mysqldb

 auto-increment-increment = 2

 auto-increment-offset = 2 

# service mysqld restart

参数解释:

  两个配置文件只有server-id不同和auto-increment-offset不同

  auto-increment-offset是用来设定数据库中自动增长的起点的,因为服务器都设定了一次自动增长值2,所以它们的起点必须得不同,这样才能避免两台服务器数据同步时出现主键冲突

  replicate-do-db 指定同步的数据库,mysqldb数据库

  auto-increment-increment的值应设为整个结构中服务器的总数,此实验为两台服务器,所以值设为2

三、查询数据库的二进制日志和pos

  主主模式,测试的话,可以先做一个数据库,然后,让第二个数据库来将数据自动同步,故不需要备份数据或者是导出导入数据。 

server1数据库

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000006 |    11230 |              |                  |                   |

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

1 row in set (0.00 sec)

 

server2数据库

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000007 |    10426 |              |                  |                   |

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

1 row in set (0.00 sec)

 

五、说明二进制日志路径

server1:

# mysql> change master to master_host='192.168.203.20',master_user='test2',master_password='123456',master_log_file='mysql-bin.000007',master_log_pos=10426;   

server2:

# mysql>change master to master_host='192.168.203.10',master_user='test2',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=11230;

 

六、启动复制(Replication)功能

在两台数据库上分别执行

# mysql> START SLAVE;

至此配置完成!

 

七、测试:

在任意一台服务器上创建一个数据库或者修改数据,对应的在另一台服务器上查看数据库数据。