从零开始配置MySQL MMM

来源:互联网 发布:人工智能吧 编辑:程序博客网 时间:2024/05/21 14:01

 下面要配置的MySQL Cluster环境包含四台数据库服务器和一台管理服务器,如下:

function
IPServer Nameserver idmonitor192.168.84.174--master192.168.85.167db11master192.168.85.169db22slave192.168.85.171db33slave192.168.85.168db44

            配置完成后,使用下面的虚拟IP访问MySQL Cluster

IProle192.168.85.200writer192.168.85.201reader192.168.85.202reader192.168.85.203reader

        一、配置MySQL Relication

        1. 安装MySQL

        通过yum命令直接安装了mysql5.1.52。

        2. 修改配置文件/etc/my.cnf

        要将添加的内容放在配置文件的[mysqld]部分,如下:        

[plain] view plaincopyprint?
  1. [mysqld]  
  2. datadir=/var/lib/mysql  
  3. socket=/var/lib/mysql/mysql.sock  
  4. user=mysql  
  5.   
  6. #下面为新添加的内容  
  7. default-storage-engine = innodb  
  8.   
  9. replicate-ignore-db = mysql  
  10. binlog-ignore-db    = mysql  
  11.   
  12. server-id           = 1  
  13. log-bin             = /var/log/mysql/mysql-bin.log  
  14. log_bin_index       = /var/log/mysql/mysql-bin.log.index  
  15. relay_log           = /var/log/mysql/mysql-bin.relay  
  16. relay_log_index     = /var/log/mysql/mysql-bin.relay.index  
  17. expire_logs_days    = 10  
  18. max_binlog_size     = 100M  
  19. log_slave_updates   = 1  

       注意:

       1)server-id在每台服务器上的值都是不一样,在这里依次为1、2、3、4。

       2)因为在这里把log文件配置到了/var/log/mysql下,而mysql默认的目录是在/var/lib/mysql,所以首先要新建mysql文件夹,然后用chown -R mysql.mysql mysql命令将mysql的所有者修改为用户mysql。其次要保证,mysql文件夹的权限755(即-rwxr-xr-x)。

       如果没有修改权限和所有者,重启服务时就会在错误日志中出现找不到mysql-bin.log或者mysql-bin.log.index的错误(/usr/libexec/mysqld: File '/var/log/mysql/mysql-bin.log.index' not found (Errcode: 13))。

       3. 重新启动mysql服务

       在完成了对my.cnf的修改后,通过service mysqld restart重新启动mysql服务。在正确启动后,可以通过如下方式检查配置是否正确:

       1)登录mysql,执行show master status,看是否有如下输出:

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      106 |              | mysql            |
+------------------+----------+--------------+------------------+

        2)到/var/log/mysql目录下,看是否产生了类似mysql-bin.000001和mysql-bin.log.index的文件。


       二、新建同步数据库需要的用户

       使用mysql-mmm时一共需要三个用户: replication、mmm_agent和mmm_monitor(管理服务器上用来监控cluster状态的用户,所以可以限定只能从管理服务器登录)。使用下面三条命令新建这三个用户并分配相应的权限:

[sql] view plaincopyprint?
  1. GRANT REPLICATION CLIENT                 ON *.* TO 'mmm_monitor'@'192.168.84.%' IDENTIFIED BY 'monitor';  
  2. GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.85.%'   IDENTIFIED BY 'agent';  
  3. GRANT REPLICATION SLAVE                  ON *.* TO 'replication'@'192.168.85.%' IDENTIFIED BY 'replication';  


      三、同步主从数据库

      1. 从主数据库服务器导出当前数据库内容      

[sql] view plaincopyprint?
  1. mysql> FLUSH TABLES WITH READ LOCK;  
  2. mysql> SHOW MASTER STATUS;  
  3. +------------------+----------+--------------+------------------+  
  4. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  5. +------------------+----------+--------------+------------------+  
  6. | mysql-bin.000001 |      106 |              | mysql            |  
  7. +------------------+----------+--------------+------------------+  

        注意保留上述信息,后面还会用到。另外,不要结束当前mysql控制台,重新打开一个窗口,导出数据库。

        # mysqldump -uroot -proot --all-databases > db01_20111005.sql

        释放锁

[html] view plaincopyprint?
  1. mysql> UNLOCK TABLES;  
       2. 将导出的sql文件导入到其他几台数据库服务器上。首先通过scp复制过去:

[html] view plaincopyprint?
  1. # scp db01_20111005.sql root@192.168.85.167:/root/  

       在其他几台服务其上导入改SQL文件:

[html] view plaincopyprint?
  1. # mysql -uroot -proot < db01_20111005.sql  

      3. 启动从数据库SLAVE进程。

[sql] view plaincopyprint?
  1. mysql> flush privileges;  
  2. Query OK, 0 rows affected (0.00 sec)  
  3.   
  4. mysql> CHANGE MASTER TO master_host='192.168.85.167', master_port=3306, master_user='replication',master_password='replication', master_log_file='mysql-bin.000001', master_log_pos=106;  
  5. Query OK, 0 rows affected (0.07 sec)  
  6.   
  7. mysql> start slave;  
  8. Query OK, 0 rows affected (0.00 sec)  
  9.   
  10. mysql> show slave status\G  
  11. *************************** 1. row ***************************  
  12.                Slave_IO_State: Waiting for master to send event  
  13.                   Master_Host: 192.168.85.180  
  14.                   Master_User: replication  
  15.                   Master_Port: 3306  
  16.                 Connect_Retry: 60  
  17.               Master_Log_File: mysql-bin.000001  
  18.           Read_Master_Log_Pos: 106  
  19.                Relay_Log_File: mysql-bin.000003  
  20.                 Relay_Log_Pos: 251  
  21.         Relay_Master_Log_File: mysql-bin.000001  
  22.              Slave_IO_Running: Yes  
  23.             Slave_SQL_Running: Yes  
  24.               Replicate_Do_DB:  
  25.           Replicate_Ignore_DB: mysql  
  26.            Replicate_Do_Table:  
  27.        Replicate_Ignore_Table:  
  28.       Replicate_Wild_Do_Table:  
  29.   Replicate_Wild_Ignore_Table:  
  30.                    Last_Errno: 0  
  31.                    Last_Error:  
  32.                  Skip_Counter: 0  
  33.           Exec_Master_Log_Pos: 106  
  34.               Relay_Log_Space: 400  
  35.               Until_Condition: None  
  36.                Until_Log_File:  
  37.                 Until_Log_Pos: 0  
  38.            Master_SSL_Allowed: No  
  39.            Master_SSL_CA_File:  
  40.            Master_SSL_CA_Path:  
  41.               Master_SSL_Cert:  
  42.             Master_SSL_Cipher:  
  43.                Master_SSL_Key:  
  44.         Seconds_Behind_Master: 0  
  45. Master_SSL_Verify_Server_Cert: No  
  46.                 Last_IO_Errno: 0  
  47.                 Last_IO_Error:  
  48.                Last_SQL_Errno: 0  
  49.                Last_SQL_Error:  
  50. 1 row in set (0.00 sec)  

        4. 将db02作为master,db01作为slave,重复1-3。


        四、安装MMM

        在管理服务器和数据库服务器上分别要运行mysql-mmm monitor和agent程序。下面分别安装:

        1. 安装监控程序

         在管理服务器(192.168.84.174)上,执行下面命令:

[plain] view plaincopyprint?
  1. # yum -y install mysql-mmm-monitor*  

         与monitor依赖的所有文件也会随之安装,但是有一个例外perl-Time-HiRes,所以还需要执行下面的命令:

[plain] view plaincopyprint?
  1. # yum -y install perl-Time-HiRes*  

         2. 安装代理程序

         在数据库服务器上执行下面的命令:

[plain] view plaincopyprint?
  1. # yum -y install mysql-mmm-agent*  

         五、配置MMM
        1. 编辑mmm_common.conf

         完成安装后,所有的配置文件都放到了/etc/mysql-mmm/下面。管理服务器和数据库服务器上都要包含一个共同的文件mmm_common.conf,内容如下:

[plain] view plaincopyprint?
  1. active_master_role      writer  
  2.   
  3. <host default>  
  4.     cluster_interface       eth0  
  5.   
  6.     pid_path                /var/run/mysql-mmm/mmm_agentd.pid  
  7.     bin_path                /usr/libexec/mysql-mmm/  
  8.   
  9.     replication_user        replication  
  10.     replication_password    replication  
  11.   
  12.     agent_user              mmm_agent  
  13.     agent_password          agent  
  14. </host>  
  15.   
  16. <host db1>  
  17.     ip      192.168.85.167  
  18.     mode    master  
  19.     peer    db2  
  20. </host>  
  21.   
  22. <host db2>  
  23.     ip      192.168.85.169  
  24.     mode    master  
  25.     peer    db1  
  26. </host>  
  27.   
  28. <host db3>  
  29.     ip      192.168.85.171  
  30.     mode    slave  
  31. </host>  
  32.   
  33. <host db4>  
  34.     ip      192.168.85.168  
  35.     mode    slave  
  36. </host>  
  37.   
  38. <role writer>  
  39.     hosts   db1, db2  
  40.     ips     192.168.85.200  
  41.     mode    exclusive  
  42. </role>  
  43.   
  44. <role reader>  
  45.     hosts   db2, db3, db4  
  46.     ips     192.168.85.201, 192.168.85.202, 192.168.85.203  
  47.     mode    balanced  
  48. </role>  

        可以在db1上编辑该文件后,通过scp命令分别复制到monitor、db2、db3和db4上。


        2. 编辑mmm_agent.conf。在数据库服务器上,还有一个mmm_agent.conf需要修改,其内容是:

[plain] view plaincopyprint?
  1. include mmm_common.conf  
  2.   
  3. # The 'this' variable refers to this server.  Proper operation requires  
  4. # that 'this' server (db1 by default), as well as all other servers, have the  
  5. # proper IP addresses set in mmm_common.conf.  
  6. this db1  
最后一行的db1,在不同的数据库服务器上要分别改为db2、db3和db4,否则代理就会无法启动。

        3. 编辑mmm_mon.confg。在管理服务器上,修改mmm_mon.conf文件,修改后内容为:

[plain] view plaincopyprint?
  1. include mmm_common.conf  
  2.   
  3. <monitor>  
  4.     ip                  192.168.84.174  
  5.     pid_path            /var/run/mysql-mmm/mmm_mond.pid  
  6.     bin_path            /usr/libexec/mysql-mmm  
  7.     status_path         /var/lib/mysql-mmm/mmm_mond.status  
  8.     ping_ips            192.168.85.167, 192.168.85.169, 192.168.85.171, 192.168.85.168  
  9.     auto_set_online     60  
  10.   
  11.     # The kill_host_bin does not exist by default, though the monitor will  
  12.     # throw a warning about it missing.  See the section 5.10 "Kill Host  
  13.     # Functionality" in the PDF documentation.  
  14.     #  
  15.     # kill_host_bin     /usr/libexec/mysql-mmm/monitor/kill_host  
  16.     #  
  17. </monitor>  
  18.   
  19. <host default>  
  20.     monitor_user        mmm_monitor  
  21.     monitor_password    monitor  
  22. </host>  
  23.   
  24. debug 0  

         六、启动MMM

        1. 在数据库服务器上启动代理程序

[plain] view plaincopyprint?
  1. # cd /etc/init.d/  
  2. # chkconfig mysql-mmm-agent on  
  3. # service mysql-mmm-agent start  

        2. 在管理服务器上启动监控程序

[plain] view plaincopyprint?
  1. # cd /etc/init.d/  
  2. # chkconfig mysql-mmm-monitor on  
  3. # service mysql-mmm-monitor start  
      启动后,稍等几秒,可以通过mmm_control程序查看状态:

[plain] view plaincopyprint?
  1. # mmm_control show  
  2.   db1(192.168.85.167) master/ONLINE. Roles: writer(192.168.85.200)  
  3.   db2(192.168.85.169) master/ONLINE. Roles: reader(192.168.85.202)  
  4.   db3(192.168.85.171) slave/ONLINE. Roles: reader(192.168.85.203)  
  5.   db4(192.168.85.168) slave/ONLINE. Roles: reader(192.168.85.201)  

       七、遇到两个问题
        1. 监控程序服务器无法启动

         在管理服务器上,一切都完成后,通过mmm_control查看状态,得到下面的错误信息:ERROR: Can't connect to monitor daemon! 通过编辑/etc/mysql-mmm/mmm_mon.conf文件将debug 0改为debug 1,打开监控程序的debug状态。重新启动监控程序(service mysql-mmm-monitor restart),就会看到详细的错误信息,找不到Perl Time HiRes库。执行yum -y install perl-Time-HiRes*就可以解决。

       2. 防火墙问题导致Warning: agent on host db1 is not reachable.

       控制台程序正确启动后,再次执行mmm_control show,却看到下面的输出:

[plain] view plaincopyprint?
  1. # Warning: agent on host db1 is not reachable  
  2. # Warning: agent on host db2 is not reachable  
  3. # Warning: agent on host db3 is not reachable  
  4. # Warning: agent on host db4 is not reachable  
  5.   db1(192.168.85.167) master/ONLINE. Roles:  
  6.   db2(192.168.85.169) master/ONLINE. Roles:  
  7.   db3(192.168.85.171) slave/ONLINE. Roles:  
  8.   db4(192.168.85.168) slave/ONLINE. Roles:  

       再次打开debug,发现了下面的错误信息:

2011/10/07 13:38:45 DEBUG Sending command 'GET_AGENT_STATUS()' to db4 (192.168.85.167:9989)
2011/10/07 13:38:45 ERROR The status of the agent on host 'db4' could not be determined (answer was: 0).

       通过telnet 192.168.85.167 9989下面检查网络连接,得到了No route to host的错误信息。登录db1,通过setup程序里的Firewall configuration关闭Firewall(这不是一个好主意)。同样,关闭db2、db3和db4上的防火墙,再次重启监控程序,一切回到正常状态!


    参考文章:

    MySQL MMM 官方安装文档 http://mysql-mmm.org/mmm2:guide 

    MMM Manual http://mysql-mmm.org/mysql-mmm.html

0 0