部署mysql集群基础环境

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了部署mysql集群基础环境相关的知识,希望对你有一定的参考价值。

设置slave2为master1从:部署集群基础环境

1.1 问题

本案例要求为mysql集群准备基础环境,完成以下任务操作:

  • 数据库授权

  • 部署MySQL双主多从结构

  • 配置本机hosts解析记录

1.2 方案

使用4台RHEL 6虚拟机,如图-1所示。其中192.168.4.10、192.168.4.11作为MySQL双主服务器,192.168.4.12、192.168.4.13作为主服务器的从服务器。

技术分享

图-1

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:准备环境

  1. [[email protected] ~]# cat /etc/hosts

  2. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

  3. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

  4. 192.168.4.10     master1         master1.tarena.com

  5. 192.168.4.11     master2         master2.tarena.com

  6. 192.168.4.12     slave1            slave1.tarena.com

  7. 192.168.4.13     slave2            slave2.tarena.com

  8. 192.168.4.100   master1         master1.tarena.com

  1. [[email protected] ~]# ping -c 2 master1

  2. PING master1 (192.168.4.10) 56(84) bytes of data.

  3. 64 bytes from master1 (192.168.4.10): icmp_seq=1 ttl=64 time=0.378 ms

步骤二:部署数据库主机

1)安装启动数据库(4台数据库主机master1,master2,slave1,slave2执行以下操作)

  1. [[email protected] ~]# tar xvf MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar    //解压软件包

  2. .. ..

  3. [[email protected] ~]# rpm -Uvh MySQL-*.rpm                                //安装MySQL

  4. .. ..

  5. [[email protected] ~]# service mysql start

2)初始化配置数据库(4台数据库主机master1,master2,slave1,slave2执行以下操作)

  1. [[email protected] ~]# cat /root/.mysql_secret         //查看随机生成密码

  2. # The random password set for the root user at Thu May  7 22:15:47 2015 (local time): wW1BNAjD


  3. [[email protected] ~]# mysql -uroot -pwW1BNAjD        //使用随机生成密码登陆

  1. mysql> set password=password("pwd123");          //修改数据库root密码

步骤三:部署双主多从结构

1)数据库授权(4台数据库主机master1,master2,slave1,slave2执行以下操作)

部署主从同步只需要授权一个主从同步用户即可,但是我们要部署MySQL-MMM架构,所以在这里我们将MySQL-MMM所需用户一并进行授权设置。再授权一个测试用户,在架构搭建完成时测试使用。

  1. [[email protected] ~]# mysql -uroot -ppwd123

数据库授权部分为了方便试验我们直接允许所有地址访问了,真实环境需谨慎

  1. mysql> grant   replication  slave  on  *.*  to  [email protected]"%" identified by  "pwd123";                                //主从同步授权

  1. mysql> grant  replication  client  on *.*  to  [email protected]"%" identified by "monitor";                                         //MMM所需架构用户授权

  1. mysql> grant  replication client,process,super   on *.*  to  [email protected]"%" identified by "agent";                                 //MMM所需架构用户授权

  1. mysql> grant  all  on *.*  to  [email protected]"%" identified by "pwd123";  //测试用户授权

2)开启主数据库binlog日志、设置server_id(master1,master2)

master1设置:

  1. [[email protected] ~]# cat /etc/my.cnf

  2. [mysqld]

  3. datadir=/var/lib/mysql

  4. socket=/var/lib/mysql/mysql.sock

  5. user=mysql

  6. server_id=10                        //设置server_id,该值集群中不可以重复

  7. log-bin                            //开启bin-log日志

  8. # Disabling symbolic-links is recommended to prevent assorted security risks

  9. symbolic-links=0

  10. [mysqld_safe]

  11. log-error=/var/log/mysqld.log

  12. pid-file=/var/run/mysqld/mysqld.pid

  13. [[email protected] ~]# service mysql restart                //重启MySQL服务

  14. [[email protected] ~]# ls /var/lib/mysql/master1-bin*        //查看binlog日志是否生成

    master2设置:

  15. [[email protected] ~]# cat /etc/my.cnf

  16. [mysqld]

  17. datadir=/var/lib/mysql

  18. socket=/var/lib/mysql/mysql.sock

  19. user=mysql

  20. server_id=11

  21. log-bin

  22. # Disabling symbolic-links is recommended to prevent assorted security risks

  23. symbolic-links=0


  24. [mysqld_safe]

  25. log-error=/var/log/mysqld.log

  26. pid-file=/var/run/mysqld/mysqld.pid

  27. [[email protected] ~]# service mysql restart

  28. [[email protected] ~]# ls /var/lib/mysql/master2-bin.*

    3)从库设置server_id

    slave1设置:

    1. [[email protected] ~]# cat /etc/my.cnf

    2. [mysqld]

    3. datadir=/var/lib/mysql

    4. socket=/var/lib/mysql/mysql.sock

    5. user=mysql

    6. server_id=12

    7. # Disabling symbolic-links is recommended to prevent assorted security risks

    8. symbolic-links=0


    9. [mysqld_safe]

    10. log-error=/var/log/mysqld.log

    11. pid-file=/var/run/mysqld/mysqld.pid

    12. [[email protected] ~]# service mysql restart

slave2设置:
  1. [[email protected] ~]# cat /etc/my.cnf

  2. [mysqld]

  3. datadir=/var/lib/mysql

  4. socket=/var/lib/mysql/mysql.sock

  5. user=mysql

  6. server_id=13

  7. # Disabling symbolic-links is recommended to prevent assorted security risks

  8. symbolic-links=0


  9. [mysqld_safe]

  10. log-error=/var/log/mysqld.log

  11. pid-file=/var/run/mysqld/mysqld.pid

  12. [[email protected] ~]# service mysql restart

4)配置主从从从关系

配置master2、slave1、slave2成为master1的从服务器

查看master1服务器binlong日志使用节点信息:

  1. [[email protected] ~]# mysql -uroot -ppwd123

  2. .. ..

  3. mysql> show master status\G

  4. File: master1-bin.000001

  5. Position: 120

  6. 设置master2为master1从:

  7. [[email protected] ~]# mysql -uroot -ppwd123

  8. .. ..

  9. mysql> change  master  to                         //设置主服务器信息

  10. -> master_host="192.168.4.10",                //设置主服务器IP地址

  11. -> master_user="slaveuser",                //设置主从同步用户

  12. -> master_password="pwd123",                //设置主从同步密码

  13. -> master_log_file="master1-bin.000001",    //设置主库binlog日志名称

  14. -> master_log_pos=120;                        //设置主从binlog日志使用节点

    1. mysql> start slave;                            //启动同步进程

    2. mysql> show slave status\G                        //查看主从是否成功

启动同步进程后查看IO节点和SQL节点是否为Yes如果均为Yes表示主从正常。Slave_IO_Running: Yes                //IO节点正常Slave_SQL_Running: Yes                //SQL节点正常.. .设置slave1为master1从:[[email protected] ~]# mysql -uroot -ppwd123mysql> change  master  to-> master_host="192.168.4.10",-> master_user="slaveuser",-> master_password="pwd123",-> master_log_file="master1-bin.000001",-> master_log_pos=120;
  1. mysql> start slave;

  2. mysql> show slave status\G

  3. Slave_IO_Running: Yes                //IO节点正常

  4. Slave_SQL_Running: Yes                //SQL节点正常\

    设置slave2为master1从:

[[email protected] ~]# mysql -uroot -ppwd123mysql> change  master  to-> master_host="192.168.4.10",-> master_user="slaveuser",-> master_password="pwd123",-> master_log_file="master1-bin.000001",-> master_log_pos=120;mysql> start slave;mysql> show slave status\GSlave_IO_Running: Yes                //IO节点正常Slave_SQL_Running: Yes                //SQL节点正常5)配置主主从从关系,将master1配置为master2的从

查看master2的binlog使用信息:

  1. [[email protected] ~]# mysql -uroot -ppwd123

  2. mysql> show master status\G

  3. File: master2-bin.000001

  4. Position: 120

设置master1成为master2的从:
  1. [[email protected] ~]# mysql -uroot -ppwd123

  2. mysql> change  master  to

  3. -> master_host="192.168.4.11",

  4. -> master_user="slaveuser",

  5. -> master_password="pwd123",

  6. -> master_log_file="master2-bin.000001",

  7. -> master_log_pos=120;

  8. mysql> start slave;

  9. mysql> show slave status\G

  10. .. ..

  11. Slave_IO_Running: Yes                //IO节点正常

  12. Slave_SQL_Running: Yes                //SQL节点正常

  13. 6)测试主从架构是否成功

  14. master1更新数据,查看其它主机是否同步:

以上是关于部署mysql集群基础环境的主要内容,如果未能解决你的问题,请参考以下文章

Database基础:部署集群基础环境MySQL-MMM架构部署MySQL-MMM架构使用

部署集群基础环境,MySQL-MMM架构部署,MySQL-MMM架构使用

实现MySQL读写分离 部署集群基础环境(有图)

07: MMM集群概述 部署集群基础环境 MMM集群部署 总结和答疑

基于Docker搭建大数据集群基础组件配置

集群环境分析及部署(基础)