当前位置:Gxlcms > 数据库问题 > mysql连接方式二

mysql连接方式二

时间:2021-07-01 10:21:17 帮助过:38人阅读

参数解释

-C    使用压缩功能,是可选的,加快速度。 
-P    用一个非特权端口进行出去的连接。 
-f    一旦SSH完成认证并建立port forwarding,则转入后台运行。 
-N    不执行远程命令。该参数在只打开转发端口时很有用(V2版本SSH支持)

1)如果是windows 下用navicat for mysql 之类的数据库管理软件,比较简单,
在"常规"选项卡填写主机名为localhost或127.0.0.1,远程数据库的账号和密码
"SSH"选项卡填写远程服务器的主机名和ssh端口号,及登录的用户和密码,测试连接是否成功
2)windows 暂时没找出类似

ssh -NCPf root@localaddress -L 3388:mysqlserver:3306

命令安静模式下建立ssh tunnel。 我用了putty试了下。连接可以成功。呆后续研究

3.mysql 主从备份使用ssh tunnel,下面摘自别人文章,待验证

1). 备份初始数据。
关闭所有相关的数据更新操作程序,或者对要备份的数据库加锁,避免更新,然后用 tar 备份到从服务器上。或者可以用mysqlhotcopy。
2). 需要在主服务器上,配置日志功能
这需要重启服务,使其启用日志功能。配置文件 my.cnf 可以放在 mysql 的安装目录中。
[mysqld] 
server-id=1
log-bin=mydb-bin
binlog-do-db=mydb
重启服务后,可以在数据目录下,看见该数据库的日志文件,并且可以通过 "show master status",查看到主服务器的状态。
3). 设置备用用户
mysql>GRANT REPLICATION SLAVE ON *.* TO backup@‘localhost‘ IDENTIFIED BY ‘backup‘;
这里之所以用本地连接,是因为后面采用 ssh 隧道连接 mysql
4). 设置从服务器
[mysqld]
server-id=2
master-host=127.0.0.1
master-port=3307
master-user=backup
master-password=backup
master-connect-retry=10
report-host=127.0.0.1
5). 远程连接隧道
ssh -f -N -4 -L 3307:127.0.0.1:3306 mysqlm@remotemysql  
6). 启动从服务器
可以通过 "show processlist",查看备份情况。并且可以查看mysql的错误日志,了解是否连接正常,同步备份功能是否工作正常。

4.因为使用yii框架在写一个项目,所以远程mysql禁止直接登录后,yii的配置文件也要相应修改,linux下比较简单,可参看以下

http://www.yiiframework.com/forum/index.php/topic/30678-mysql-through-ssh-tunnel/
windows待续......               敲命令验证: $ mysql -uroot -h127.0.0.1 -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.47-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MySQL [(none)]> quit
Bye

24975@DESKTOP-I4OIMJC ~
mysql -uroot -hlocalhost -p
Enter password:
ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/run/mysql.sock‘ (2)

24975@DESKTOP-I4OIMJC ~
mysql --protocol=tcp -uroot -hlocalhost -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.5.47-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MySQL [(none)]> quit
Bye

24975@DESKTOP-I4OIMJC ~
mysql --protocol=tcp -uroot -hlocalhost -p‘******‘
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.47-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MySQL [(none)]>

mysql连接方式二

标签:命令   work   压缩   测试   oracle   安全设置   框架   ip地址   master   

人气教程排行