I got the problem that my mysql, while locally (Ubuntu 20.04.3 LTS) available, can't be reached via a remote client:
telnet 127.0.0.1 3306
Trying 127.0.0.1...
Connected to 127.0.0.1.
but
telnet xxx.xxx.xxx.xxx 3306
Trying xxx.xxx.xxx.xxx...
telnet: Unable to connect to remote host: Connection refused
I tried everything I found, but to no success. This is what I tried / checked:
/etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = ::
mysqlx-bind-address = ::
I also tried to set those addresses to "0.0.0.0", the result is the same
I also did a restart via systemctl restart mysql and netstat -ntlup shows
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::3306 :::* LISTEN 567829/mysqld
tcp6 0 0 :::33060 :::* LISTEN 567829/mysqld
UFW is inactive
ufw status
Status: inactive
There is a mysql user with remote access
SELECT user, host, account_locked, password_expired FROM user;
+---------+-----------+----------------+------------------+
| user | host | account_locked | password_expired |
+---------+-----------+----------------+------------------+
| my_user | % | N | N |
This user has read permissions to all tables of a database. I executed a FLUSH PRIVILEGES;.
Server accessibility
The server generally is accessible from remote. I can connect via SSH and HTTP/HTTPS. I set the server provider firewall settings (Hetzner Cloud Server) and 3306 aswell as 33060 are allowed incoming ports.
I checked the MySQL server accessibility on the server itself via its remote IP, my local computer and from another external server. All show the above mentioned "Connection refused" via telnet and when trying to connect via the mysql command.
Those were all the tips and configurations I found on existing solutions of this problem. I am stuck for two days now and can't find a way forwards. Does anyone have an idea?
I need external connection to MySQL. Using Plesk/Ubuntu 16/nginx. In firewall policy the port 3306 is allowed from any IP.
Is it somewhere else where it shall be allowed? If so, what file is needed to be edited?
You have to make changes to MySQL configuration, and allow remote access for the user.
1. Open MySQL configuration file. For RHEL-based distributions - /etc/my.cnf, for Debian-based - /etc/mysql/my.cnf.
2. Change bind-address parameter to bind-address = 0.0.0.0 and make sure that skip-networking parameter is not defined.
3. Restart MySQL daemon:
# service mysql restart
Or:
# service mysqld restart
Or:
# service mariadb restart
The service name depends on the installed MySQL version. You can do all three if you are not sure. It will do no harm unless the error was made in my.cnf.
4. Grant the access to remote IP address and login to MySQL. For example, if you want to allow access to database called database for user user with password password and remote IP address 203.0.113.2:
GRANT ALL ON database.* TO user#'203.0.113.2' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
To create a user that has access to all databases, execute the following query:
GRANT ALL PRIVILEGES ON *.* TO 'user'#'203.0.113.2' IDENTIFIED BY 'password' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
FLUSH PRIVILEGES;
5. You can verify that MySQL server is listening on the correct IP address using netstat. Here is the desired output - 0.0.0.0 address:
# netstat -anp | grep :3306
tcp6 0 0 0.0.0.0:3306 :::* LISTEN 2723/mysqld
netstat result:
netstat -anp | grep :3306
tcp6 0 0 127.0.0.1:3306 :::* LISTEN 1003/mysqld
I want to connect to MySQL(it's running on my local machine) using JDBC. I am able to connect to MySQL using Url:
"jdbc:mysql://localhost:3306"
When I tried
"jdbc:mysql://192.xxx.xxx.xxx:3306"
I got errors.
I am working on Ubuntu & my etc/hosts file has this entry:
127.0.0.1 localhost 192.xxx.xxx.xxx
Any pointer on this?
Edit:
my.cnf has this enrty:
bind-address = 127.0.0.1
netstat -na | grep 3306 gave me:
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:3306 127.0.0.1:57243 ESTABLISHED
As mentioned in comments: The BIND-ADDRESS setting in my.cnf forced mysql to listen an 127.0.0.1 only so no network connect from outside is possible.
Remove bind address and you'll be fine.
I think there is no need to bind the address so please remove bind address and get done. Note:- you need to restart mysql server.
I have a virtual machine (CentOS) running on Google's Compute Engine and everything seems pretty straight forward, have read all the documentation I can find, but this one thing is hanging me up. I know outbound SMTP connections are blocked by the GCE, but incoming should be no problem from what I've read. However, I've configured the server correctly (I think) and added the firewall for SMTP through gcutil, but still no dice:
gcutil addfirewall smtp --description="Incoming smtp allowed." --allowed="tcp:smtp"
This adds the rule to allow SMTP connections.
gcutil listfirewalls
+------------------------+---------------------------------------+---------+------------+-------------+-------------+
| name | description | network | source-ips | source-tags | target-tags |
+------------------------+---------------------------------------+---------+------------+-------------+-------------+
| default-allow-internal | Internal traffic from default allowed | default | 10.0.0.0/8 | | |
| default-ssh | SSH allowed from anywhere | default | 0.0.0.0/0 | | |
| http2 | Incoming http allowed. | default | 0.0.0.0/0 | | |
| pop3 | Incoming pop3 allowed. | default | 0.0.0.0/0 | | |
| smtp | Incoming smtp allowed. | default | 0.0.0.0/0 | | |
+------------------------+---------------------------------------+---------+------------+-------------+-------------+
SMTP rule is listed. Other ports work fine, I can get into ports 80, 22, and 110. But when trying to get into port 25:
telnet nextcore.com 25
Trying 173.255.112.1...
It will not connect.
Connecting to localhost port 25 on the virtual machine works just fine:
telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 nextcore.localdomain ESMTP Postfix
What am I missing?
I'm not sure what you have listening on port 25 (e.g. sendmail, postfix, etc). I can confirm that I also can't connect to your VM on port 25, though I can connect on port 80.
You can check which programs are listening on which ports with sudo netstat -lpn -A inet, e.g.
$ sudo netstat -lpn -A inet
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 2691/sshd
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 15845/nc
udp 0 0 0.0.0.0:23153 0.0.0.0:* 2291/dhclient
udp 0 0 0.0.0.0:68 0.0.0.0:* 2291/dhclient
udp 0 0 10.87.233.49:123 0.0.0.0:* 2566/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 2566/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 2566/ntpd
You might discover that your mail program is only listening on localhost (127.0.0.1), or that you are missing an entry in /etc/hosts.allow or have an entry in '/etc/hosts.deny` that blocks requests from outside your local network.
You can take a look at this: https://developers.google.com/compute/docs/networking#mailserver
You will see that Google Compute Engine blocks outbound connections on ports 25, 465, and 587.
The instruction for setting up postfix to work with GCE is available on that page as well.
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?
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.
Using Mysql client:
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
grep port /etc/mysql/my.cnf ( at least in debian/ubuntu works )
or
netstat -tlpn | grep mysql
or
mysql -u user_name -puser_pass -e "SHOW variables LIKE 'port';"
verify
bind-address 127.0.0.1
in /etc/mysql/my.cnf to see possible restrictions
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.
In this way you can see the process name and the port.
Try only using -e (--execute) option:
$ mysql -u root -proot -e "SHOW GLOBAL VARIABLES LIKE 'PORT';" (8s 26ms)
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
Replace root by your "username" and "password"
Both URLs are incorrect - should be
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.
Maybe you can connect using a socket over TCP/IP. Check out the MySQL docs.
See 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:
http://lists.mysql.com/win32/253
I think this is what you had in mind.
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>
3306 is default port for mysql. Check it with:
netstat -nl|grep 3306
it should give this result:
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
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 |
+---------------+-------+
you can use
ps -ef | grep mysql
On a mac os X, there are two options. netstat or lsof
Using netstat will not show the process on Mac OS X. so using netstat you can only search by port.
Using lsof will show the process name.
I did the following as I was encountering port conflicts (docker containers):
netstat -aln | grep 3306
Outputs:
tcp46 0 0 *.3306 *.* LISTEN
sudo lsof -i -P | grep -i "LISTEN" | grep -i 3306
Outputs:
mysqld 60608 _mysql 31u IPv6 0x2ebc4b8d88d9ec6b 0t0 TCP *:3306 (LISTEN)
If you are on a system where netstat is not available (e.g. RHEL 7 and more recent Debian releases) you can use ss, as below:
sudo ss -tlpn | grep mysql
And you'll get something like the following for output:
LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=5307,fd=14))
The fourth column is Local Address:Port. So in this case Mysql is listening on port 3306, the default.
I think the most appropriate way of finding the port associated with mysql server using lsof command line tool.
lsof -i -n -P | grep mysql
mysqld 1556 prince 10u IPv4 0x6ad3fd78a9051969 0t0 TCP 127.0.0.1:3306 (LISTEN)
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)