三.补充资料
关于如何连接到远程 MySQL 问题,可以采取下面的步骤:
首先先登录到远程机器:
- jian.ma@camlit ~: ssh root@192.168.56.103
- password: xxx
- root@test2 ~:
编辑配置文件:
- root@test2 ~: vi /etc/my.cnf
增加下面一行内容:
- [mysqld]
- datadir=/var/lib/mysql
- socket=/var/lib/mysql/mysql.sock
- user=mysql
- old_passwords=1
- bind-address=192.168.56.103###此 IP 地址为 MySQL 本机的 IP 地址
- [mysqld_safe]
- log-error=/var/log/mysqld.log
- pid-file=/var/run/mysqld/mysqld.pi
重启服务:
- root@test2 ~: /etc/init.d/mysqld restart
创建测试数据库:
- root@test2 ~: mysql -u root -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.0.45 Source distribution
- Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
- mysql> create database foo ;
- Query OK, 1 row affected (0.00 sec)
- ###增加用户 test123 从任何主机登录到 MySQL
- mysql> grant all privileges on *.* to 'test123'@'%' identified by 'test123' with grant option;
- Query OK, 0 rows affected (0.00 sec)
- ###增加用户 test1 从 192.168.7.67 主机登录到 MySQL
- mysql> grant all privileges on foo.* to 'test1'@'192.168.7.67' identified by 'test1' with grant option;
- Query OK, 0 rows affected (0.00 sec)
如果有防火墙的设置的话,可以如下设置:
- root@test2 ~: iptables -A INPUT -i eth0 -s 192.168.7.67 -p tcp --dport 3306 -j ACCEPT
- root@test2~: /etc/init.d/iptales save
最后在客户端就可以输入下面命令来远程进入 MySQL 数据库:
- jian.ma@camlit ~: mysql -u test1 -h 192.168.56.103 -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 13
- Server version: 5.0.45 Source distribution
- Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysqld.pl内容如下:
- #!/usr/bin/perl
- #This script is used to check if the mysql replication is ok
- use strict;
- use DBI;
- use POSIX "strftime";
- my $host = "192.168.56.103";
- my $user = "test1";
- my $passwd = "test1";
- my $port = "3306";
- my $max_behind = "120";
- my $check_log = "./mysql_check.log";
- #Open the log file
- open (FH, ">> $check_log") or die $!;
- #Connect the mysql server
- my $dbh = &MysqlConnect ($host, $port, $user, $passwd);
- #Get slave sql status
- my $slave_status = &MysqlQuery( $dbh, 'show slave status');
- print FH "Error: SQL Query Error:" . $dbh->errstr;
- my $slave_IO = $slave_status->{Slave_IO_Running};
- my $slave_SQL = $slave_status->{Slave_SQL_Running};
- my $seconds_behind_master = $slave_status->{Seconds_Behind_Master};
- my $now_time = POSIX::strftime ("[%Y-%m-%d %H:%M:%S]", localtime);
- print "Check the Slave MySQL stauts....\n";
- print "_" x 50, "\n";
- print "Time:\t\t\t$now_time\n";
- print "Slave IO Running:\t\t$slave_IO\n";
- print "Slave SQL Running::\t\t$slave_SQL\n";
- print "Behind Master Seconds:\t\t$seconds_behind_master\n";
- if ($seconds_behind_master > $max_behind){
- print "Slave SQL Server is far behind master ";
- }
- #---Functions----#
- sub MysqlConnect {
- my ($host, $port, $user, $passwd) = @_;
- my $dsn = "DBI:mysql:host=$host;port=$port";
- return DBI->connect($dsn, $user, $passwd, {RaiseError => 1});
- }
- sub MysqlQuery {
- my ($dbh , $query) = @_;
- my $sth = $dbh->prepare($query);
- my $res = $sth->execute;
- return undef unless $res;
- my $row = $sth->fetchrow_hashref;
- $sth->finish;
- return $row;
- }
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。