Friday 16 November 2018

How to test which port MySQL is running on and whether it can be connected to?

I have installed MySQL and even logged in there as a user.
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?

 Answers


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.
tcp        0      0 127.0.0.1:3306              0.0.0.0:*                   LISTEN      
Port 3306 is MySql's default port.
To connect, you just have to use whatever client you require, such as the basic mysql client.
mysql -h localhost -u user database
Or a url that is interpreted by your library code.



grep port /etc/mysql/my.cnf ( at least in debian/ubuntu works )
or
netstat -tlpn | grep mysql
verify
bind-address 127:0.0.1
in /etc/mysql/my.cnf to see possible restrictions



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 --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.
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>



you can use
ps -ef | grep mysql



I agree with @bortunac's solution. my.conf is mysql specific while netstat will provide you 
with all the listening ports.
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.
My client uses CentOS 6.6 and I have found the my.conf file under /etc/, so I used:
grep port /etc/my.conf (CentOS 6.6)



For me, @joseluisq's answer yielded:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
But it worked this way:
$ mysql -u root@localhost  -e "SHOW GLOBAL VARIABLES LIKE 'PORT';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+

0 comments:

Post a Comment