龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > MySQL 技术 >

手把手教你实现MySQL双机数据同步(1)(2)

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
三.补充资料 关于如何连接到远程 MySQL 问题,可以采取下面的步骤: 首先先登录到远程机器: jian.ma@camlit~:sshroot@192.168.56.103 password :xxx root@test2~: 编辑配置

三.补充资料

关于如何连接到远程 MySQL 问题,可以采取下面的步骤:

首先先登录到远程机器:

  1. jian.ma@camlit ~: ssh root@192.168.56.103   
  2. password: xxx  
  3. root@test2 ~:  

编辑配置文件:

  1. root@test2 ~: vi /etc/my.cnf  

增加下面一行内容:

  1. [mysqld]   
  2. datadir=/var/lib/mysql   
  3. socket=/var/lib/mysql/mysql.sock   
  4. user=mysql   
  5. old_passwords=1   
  6. bind-address=192.168.56.103###此 IP 地址为 MySQL 本机的 IP 地址  
  7. [mysqld_safe]   
  8. log-error=/var/log/mysqld.log   
  9. pid-file=/var/run/mysqld/mysqld.pi 

重启服务:

  1. root@test2 ~: /etc/init.d/mysqld restart  

创建测试数据库:

  1. root@test2 ~: mysql -u root -p  
  2. Enter password:   
  3. Welcome to the MySQL monitor.  Commands end with ; or g.   
  4. Your MySQL connection id is 2   
  5. Server version: 5.0.45 Source distribution   
  6.    
  7. Type 'help;' or 'h' for help. Type 'c' to clear the buffer.   
  8.    
  9. mysql> create database foo ;  
  10. Query OK, 1 row affected (0.00 sec)   
  11. ###增加用户 test123 从任何主机登录到 MySQL  
  12. mysql> grant all privileges on *.* to 'test123'@'%' identified by 'test123' with grant  option;   
  13. Query OK, 0 rows affected (0.00 sec)   
  14. ###增加用户 test1 从 192.168.7.67 主机登录到 MySQL  
  15. mysql> grant all privileges on foo.* to 'test1'@'192.168.7.67' identified by 'test1' with grant   option;   
  16. Query OK, 0 rows affected (0.00 sec)  

如果有防火墙的设置的话,可以如下设置:

  1. root@test2 ~: iptables -A INPUT -i eth0 -s 192.168.7.67 -p tcp --dport 3306 -j ACCEPT   
  2. root@test2~: /etc/init.d/iptales save  

最后在客户端就可以输入下面命令来远程进入 MySQL 数据库:

  1. jian.ma@camlit ~: mysql -u test1 -h 192.168.56.103 -p   
  2. Enter password:   
  3. Welcome to the MySQL monitor.  Commands end with ; or g.   
  4. Your MySQL connection id is 13   
  5. Server version: 5.0.45 Source distribution   
  6. Type 'help;' or 'h' for help. Type 'c' to clear the buffer. 

mysqld.pl内容如下:

  1. #!/usr/bin/perl 
  2.  
  3. #This script is used to check if the mysql replication is ok 
  4.  
  5. use strict; 
  6. use DBI; 
  7. use POSIX "strftime"
  8.  
  9. my $host           = "192.168.56.103"
  10. my $user           = "test1"
  11. my $passwd         = "test1"
  12. my $port           = "3306"
  13. my $max_behind     = "120"
  14. my $check_log      = "./mysql_check.log"
  15.  
  16.  
  17. #Open the log file  
  18. open (FH, ">> $check_log"or die $!; 
  19.  
  20. #Connect the mysql server  
  21. my $dbh = &MysqlConnect ($host, $port, $user, $passwd); 
  22.  
  23.  
  24. #Get slave sql status 
  25. my $slave_status = &MysqlQuery( $dbh, 'show slave status'); 
  26. print FH "Error: SQL Query Error:" . $dbh->errstr; 
  27.  
  28.  
  29. my $slave_IO              = $slave_status->{Slave_IO_Running}; 
  30. my $slave_SQL             = $slave_status->{Slave_SQL_Running}; 
  31. my $seconds_behind_master = $slave_status->{Seconds_Behind_Master}; 
  32. my $now_time              = POSIX::strftime ("[%Y-%m-%d %H:%M:%S]", localtime); 
  33.  
  34.  
  35. print "Check the Slave MySQL stauts....n"
  36. print "_" x 50"n"
  37. print "Time:ttt$now_timen"
  38. print "Slave IO Running:tt$slave_IOn"
  39. print "Slave SQL Running::tt$slave_SQLn"
  40. print "Behind Master Seconds:tt$seconds_behind_mastern"
  41.  
  42.  
  43. if ($seconds_behind_master > $max_behind){ 
  44.     print "Slave SQL Server is far behind master "
  45.  
  46.  
  47. #---Functions----# 
  48. sub MysqlConnect  { 
  49.  
  50.     my ($host, $port, $user, $passwd) = @_; 
  51.     my $dsn  = "DBI:mysql:host=$host;port=$port"
  52.     return DBI->connect($dsn, $user, $passwd, {RaiseError => 1}); 
  53.  
  54.  
  55. sub MysqlQuery { 
  56.  
  57.     my ($dbh , $query) = @_; 
  58.     my $sth = $dbh->prepare($query); 
  59.     my $res = $sth->execute; 
  60.     return undef unless $res; 
  61.     my $row = $sth->fetchrow_hashref; 
  62.     $sth->finish; 
  63.     return $row; 

精彩图集

赞助商链接