Linux 如何测试MySQL运行在哪个端口以及是否可以连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5864242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test which port MySQL is running on and whether it can be connected to?
提问by GeekedOut
I have installed MySQL and even logged in there as a user.
我已经安装了 MySQL,甚至以用户身份登录。
But when I try to connect like this:
但是当我尝试像这样连接时:
http://localhost:3306
mysql://localhost:3306
Neither works. Not sure if both are supposed to work, but at least one of them should :)
两者都不起作用。不确定两者是否都应该工作,但至少其中一个应该:)
How can I make sure that the port is indeed 3306? Is there a linux command to see it somehow? Also, is there a more correct way to try it via a url?
如何确保端口确实是 3306?是否有一个 linux 命令可以以某种方式查看它?另外,有没有更正确的方法来通过 url 尝试它?
采纳答案by Keith
To find a listener on a port, do this:
要在端口上查找侦听器,请执行以下操作:
netstat -tln
You should see a line that looks like this if mysql is indeed listening on that port.
如果 mysql 确实在该端口上侦听,您应该看到如下所示的一行。
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
Port 3306 is MySql's default port.
端口 3306 是 MySql 的默认端口。
To connect, you just have to use whatever client you require, such as the basic mysql client.
要连接,您只需要使用您需要的任何客户端,例如基本的 mysql 客户端。
mysql -h localhost -u user database
mysql -h localhost -u 用户数据库
Or a url that is interpreted by your library code.
或由您的库代码解释的 url。
回答by duffymo
Both URLs are incorrect - should be
两个 URL 都不正确 - 应该是
jdbc:mysql://host:port/database
I thought it went without saying, but connecting to a database with Java requires a JDBC driver. You'll need the MySQL JDBC driver.
我认为这是不言而喻的,但是使用 Java 连接到数据库需要一个 JDBC 驱动程序。您将需要MySQL JDBC 驱动程序。
Maybe you can connect using a socket over TCP/IP. Check out the MySQL docs.
也许您可以通过 TCP/IP 使用套接字进行连接。查看MySQL 文档。
See http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
见http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
UPDATE:
更新:
I tried to telnet into MySQL (telnet ip 3306
), but it doesn't work:
我试图 telnet 到 MySQL ( telnet ip 3306
),但它不起作用:
http://lists.mysql.com/win32/253
http://lists.mysql.com/win32/253
I think this is what you had in mind.
我想这就是你的想法。
回答by bortunac
grep port /etc/mysql/my.cnf
( at least in debian/ubuntu works )
grep port /etc/mysql/my.cnf
(至少在 debian/ubuntu 作品中)
or
或者
netstat -tlpn | grep mysql
verify
核实
bind-address 127.0.0.1
绑定地址 127.0.0.1
in /etc/mysql/my.cnf to see possible restrictions
在 /etc/mysql/my.cnf 中查看可能的限制
回答by mPrinC
netstat -tlpn
It will show the list something like below:
它将显示如下列表:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1393/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1859/master
tcp 0 0 123.189.192.64:7654 0.0.0.0:* LISTEN 2463/monit
tcp 0 0 127.0.0.1:24135 0.0.0.0:* LISTEN 21450/memcached
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 16781/mysqld
Use as root for all details. The -t
option limits the output to TCP connections, -l
for listening ports, -p
lists the program name and -n
shows the numeric version of the port instead of a named version.
以 root 身份使用所有详细信息。该-t
选项将输出限制为 TCP 连接,-l
对于侦听端口,-p
列出程序名称并-n
显示端口的数字版本而不是命名版本。
In this way you can see the process name and the port.
这样就可以看到进程名和端口了。
回答by Monarch Wadia
A simpler approach for some : If you just want to check if MySQL is on a certain port, you can use the following command in terminal. Tested on mac. 3306 is the default port.
一些更简单的方法:如果您只想检查 MySQL 是否在某个端口上,您可以在终端中使用以下命令。在mac上测试。3306 是默认端口。
mysql --host=127.0.0.1 --port=3306
mysql --host=127.0.0.1 --port=3306
If you successfully log in to the MySQL shell terminal, you're good! This is the output that I get on a successful login.
如果您成功登录到 MySQL shell 终端,那您就很好了!这是我成功登录时获得的输出。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9559
Server version: 5.6.21 Homebrew
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
回答by enagra
Using Mysql client:
使用Mysql客户端:
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
回答by mmmdearte
I agree with @bortunac's solution. my.conf is mysql specific while netstat will provide you with all the listening ports.
我同意@bortunac 的解决方案。my.conf 是特定于 mysql 的,而 netstat 将为您提供所有侦听端口。
Perhaps use both, one to confirm which is port set for mysql and the other to check that the system is listening through that port.
也许同时使用两者,一个是确认为 mysql 设置的端口,另一个是检查系统是否正在通过该端口进行侦听。
My client uses CentOS 6.6 and I have found the my.conf file under /etc/, so I used:
我的客户端使用 CentOS 6.6,我在 /etc/ 下找到了 my.conf 文件,所以我使用了:
grep port /etc/my.conf
(CentOS 6.6)
grep port /etc/my.conf
(CentOS 6.6)
回答by zygimantus
3306 is default port for mysql. Check it with:
3306 是 mysql 的默认端口。检查它:
netstat -nl|grep 3306
it should give this result:
它应该给出这个结果:
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:3306 0.0.0.0:* 听
回答by don625
you can use
您可以使用
ps -ef | grep mysql
回答by joseluisq
Try only using -e
(--execute
) option:
仅尝试使用-e
( --execute
) 选项:
$ mysql -u root -proot -e "SHOW GLOBAL VARIABLES LIKE 'PORT';" (8s 26ms)
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
Replace root
by your "username" and "password"
替换root
为您的“用户名”和“密码”