怎么通过SSH连接Mysql数据库

怎么通过SSH连接Mysql数据库,第1张

1.打开UE,点击文件——Ftp/Telent——SSH/Telent账号管理器

在没有数据库账号时,先添加一个,如果有,可以跳过此步

服务器输入地址(链接IP地址),协议选择SSH,用户名输入数据库的用户名,密码是数据库链接密码(就是建立一个直接到数据库用户的链接)

2.点击文件——Ftp/Telent——SSH/Telent控制台,选择要链接的账号,点击链接

3.链接后,输入以下命令:db2

connect

to

uibs

,此处uibs为数据库名

4.要执行sql命令,输入:db2

"你要执行的sql语句",如:

db2

"select

count(*)

from

banknotice"

注意,在这里执行的sql命令,最好是查询条数的,如果是查询记录,还会返回的内容的乱码,可能与编码有关

相关的linux命令:

ls

显示当前目录下的所有文件夹

Cd

文件夹

进入到某个文件夹

Cd

..

返回上级目录,注意字母和..之间有空格

许多时候当要使用Mysql时,会遇到如下情况:

1. 信息比较重要,希望通信被加密。

2. 一些端口,比如3306端口,被路由器禁用。

对第一个问题的一个比较直接的解决办法就是更改mysql的代码,或者是使用一些证书,不过这种办法显然不是很简单。

这里要介绍另外一种方法,就是利用SSH通道来连接远程的Mysql,方法相当简单。

一 建立SSH通道

只需要在本地键入如下命令:

ssh -fNg -L 3307:127.0.0.1:3306 myuser@remotehost.com

The command tells ssh to log in to remotehost.com as myuser, go into the background (-f) and not execute any remote command (-N), and set up port-forwarding (-L localport:localhost:remoteport ). In this case, we forward port 3307 on localhost to port 3306 on remotehost.com.

二 连接Mysql

现在,你就可以通过本地连接远程的数据库了,就像访问本地的数据库一样。

mysql -h 127.0.0.1 -P 3307 -u dbuser -p db

The command tells the local MySQL client to connect to localhost port 3307 (which is forwarded via ssh to remotehost.com:3306). The exchange of data between client and server is now sent over the encrypted ssh connection.

或者用Mysql Query Brower来访问Client的3307端口。

类似的,用PHP访问:

<?php

$smysql = mysql_connect( "127.0.0.1:3307", "dbuser", "PASS" )

mysql_select_db( "db", $smysql )

?>

Making It A Daemon

A quick and dirty way to make sure the connection runs on startup and respawns on failure is to add it to /etc/inittab and have the init process (the, uh, kernel) keep it going.

Add the following to /etc/inittab on each client:

sm:345:respawn:/usr/bin/ssh -Ng -L 3307:127.0.0.1:3306 myuser@remotehost.com

And that should be all you need to do. Send init the HUP signal ( kill -HUP 1 ) to make it reload the configuration. To turn it off, comment out the line and HUP init again.

Navicat

Premium---能 *** 作多种数据库的图形化界面工具

Navicat

for

Mysql

--- *** 作mysql的图形化界面工具

背景:

我们经常去 *** 作生产环境服务器,用命令 *** 作比较不方便,所以我们想和平时开发的时候一样,用客户端 *** 作数据库,下面就介绍平时用的最多的两种数据库客户端工具SQLyog和Navicat。

这两种工具链接的问题描述:

前提是用命令启动停止MySQL数据都正常的情况下。

若用平常的方式直接输入ip,用户名,密码,testConnection的时候会报错error2003,无法连接localhost。原因不是数据库没开权限,是没有使用ssh链接。

1、SQLyog客户端的使用

mysql

Host

Address(一般为localhost),端口(一般都为3306),用户名(一般为root),密码,

直接点Connect或Test

Connection都连接不成功,需要选择ssh进行配置,见下图:

需要对Use

SSH

Tunneling进行打对勾,然后,输入必填项

SSH

Host

Address——>远程服务器的ip地址或域名

Username——>远程服务器的登录用户名

SSH

Port——>默认是22,此处固定不变

Password——>远程服务器的登录密码

Mysql和ssh中的必填项都输入完毕后,在mysql界面,点击Test

Connection,测试通过。然后点击Connect,进行数据库的链接

2、navicat客户端的使用

输入目标数据库的域名/IP

(一般为localhost),端口(一般都为3306),用户名(一般为root),密码,如下图:

然后不要点OK按钮,将标签切换到SSH这个tab,如下图:

与SQLyog相同,选中Use

SSH

Tunnel,输入远程服务器的ip,远程服务器的登录用户名和密码,端口22为默认的不需要修改,

然后点击ok按钮,保存链接即可


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8461010.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-16
下一篇 2023-04-16

发表评论

登录后才能评论

评论列表(0条)

保存