Connect to remote mysql via terminal - mysql

For a test I set up user mysql user with % access. When I try to connect from a remote machine I get:
Enter password:
Then immediately get this:
ERROR 2003 (HY000): Can’t connect to MySQL server on ‘xxxxxxx’ (1)
Any ideas

Verify that you can connect to the mysql port with telnet from that remote machine, e.g.
telnet 192.168.1.52 3306
If that fails, there's a firewall somewhere preventing you to connect, or you're running mysql on a different port than the default(3306) or mysql isn't configured to listen on the interface/ip address (maybe it's just configured to listen on connections from localhost, check the bind-address configuration in the my.cnf config file)

Try to comment the bind-address = 127.0.0.1 in your /etc/mysql/my.cnf
# security:
# using "localhost" in connects uses sockets by default
# skip-networking
# bind-address = 127.0.0.1 --- commented
and watch the results. If not work try to use the IP address and if still not work. roll back the original file.

Can you telnet to the MySQL port 3306? If you can't then check to make sure the MySQL server service is listening on that port. Open a command prompt and type the following example.
Example: telnet yourmysqlservername.com 3306

Related

Database Not connectable through Public IP

NOTE. operation system is ubuntu 14.*
I am breaking my head over my connection on my MYSQL server. I try to connect to my MYSQL server internal through my public ip. But i am unable to connect. While i am able to connect when i connect to localhost and when i connect externally.
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
I commented the bind-adress out and this should bind the addres to 0.0.0.0. I also tried to setup the bind-adres to 0.0.0.0. This also doesnt work.
My hosts file is setup up as follows:
127.0.0.1 MY-IP
When i telnet on port 80 to my public ip there is no problem.
telnet MY-IP 80
I get:
Trying MY-IP...
Connected to MY-IP.
Escape character is '^]'.
But when i try this on port 3306 it keeps on:
Trying MY-IP...
I also granted all the right permissions to my MYSQL users. And i am possible to connect externaly and internaly. But it is not possible for me to connect internaly with my public ip.
Also i tried to find a solution with netstat.
netstat -tln
give mes
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
EDIT: Just disabled UFW. It still doesnt work.
ufw status
Status: inactive
EDIT2: Is it possible that mysql is ignoring the /etc/hosts file?
Could somebody help me a bit further with some great ideas where the problem might be.
You should change the bind address to 0.0.0.0 then restart the mysqld. Also make sure there is no firewall enabled:
iptables -n -L
if there are rules, blocking port 3306, delete them, or just disable the firwall with
iptables -F
also use netstat to check if the server is listening on the external ip:
netstat -tupan |grep :3306

Connect to MySQL Database Remote

I have a MySQL database on another computer and I want to be able to connect from other computers (even computers on different networks). I did the following
MySQL
CREATE USER 'username_here'#'%' IDENTIFIED BY 'password_here';
then
GRANT ALL PRIVILEGES ON *.* TO 'username_here'#'%';
FLUSH PRIVILEGES;
I also commented out bind-address in the my.ini file (I have windows)
Then in the Java application (that utilizes the MySQL database) I did
jdbc:mysql://public_ip:3306/database_name
I used my public ip address in the jdbc url.
Finally, I opened port 3306 in firewall. But I am not able to connect to the database remotely. What other things do I need to do to connect?
First of all: Do not public post your real ip address and port and also say that you opened the firewall. Perhaps hackers now find it too easy to hack, but you can not rely on that ;-).
To your problem: Perhaps you configured your mysql to listen at localhost 3306 and not to your public ip address. Please enter netstat -ltn in your shell. If there is a line
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
and none with the public host address then you only have access from inside the network. Then, you have to configure your host adress in your mysql ini file. (just replace 127.0.0.1 or localhost by your ip address.
FYI : It is a very bad idea to have a publicly open port to your database server, specially MySQL default(3306) port. You are just inviting people to try to break into your database.
To access MySQL remotely, follow below steps :
1) Comment out following lines in my.cnf file.
#bind-address = 127.0.0.1
#skip-networking
2) Restart mysql server.
net stop MySQL
net start MySQL
If you got The service name is invalid then Open task manager => Click Service button => search MySql service => Restart
Remotely connect with mysql :
mysql -h HOST -u USERNAME -pPASSWORD
You can also check the open port with nmap command :
nmap -PN Ip_address
Note : Mask your ip address in your question.
MySQL Java Connectivity :
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
Connection connection = DriverManager.getConnection("jdbc:mysql://publicIpAddress/dbName?",
sqluser,password);

Remote Access MySQL connection error

I am trying to remotely connect to a MySQL server. I have followed advice from (1) and setup a user on the ip address I will be remotely accessing from.
user$ mysql -u TestUser -p -h 129.169.66.149
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '129.169.66.149' (60)
I have checked and the port (default, 3306) is correct and the ip address is correct. MySQL is also running.
From a remote computer, I can successfully ping the server
ping 129.169.66.149
64 bytes from 129.169.66.149: icmp_seq=48 ttl=63 time=1.010 ms
But when I use Telnet:
TELNET 129.169.66.149
Trying 129.169.66.149...
telnet: connect to address 129.169.66.149: Operation timed out
telnet: Unable to connect to remote host
Can anyone advise? Is this a firewall issue?
(1) - https://superuser.com/questions/826896/access-wordpress-mysql-database-remotely
First try to check TCP connection issue, using netcat and telnet : on the mysql server, stop mysqld (to release port 3306) and run netcat on listen mode :
nc -l -p 3306
Now, netcat is listening port 3306 (like mysqld does when it's running) and will show what happen on that port (incoming connections, what's in the wire...).
On your remote computer (mysql client), try to connect to the mysql server host :
telnet 129.169.66.149 3306
or :
nc 129.169.66.149 3306
If this is not working, this is not a mysql server configuration issue but a network issue, and you must check your router firewall rules.
Otherwise, your problem comes from mysql server settings. Check your mysql configuration file for bind-address, and remove them to make mysqld accept clients from any IP address.
The 3306 port on the 129.169.66.149 server is closed you'll have to open this port
Use this reference as a guide http://www.cyberciti.biz/faq/linux-unix-open-ports/

Connecting to MySQL from Workbench

MySQL remote access
I have a mysql database, running on Ubuntu Server 12.04 that I need to access remotely. For some reason this is become much more of a chore than I think it should be.
I have been through countless threads trying to resolve this issue with no luck what so ever. I do not have another linux box to test my connection. I am only using the MySQL Workbench from a Window 7 machine.
Here is what I have done so far:
set the iptables to accept
set the my.cnf to have the bind address of the server
created a user for both localhost and %
grant all to those users
restarted mysql
verified the user has all priv
verified mysql is listening on 3306
give the correct setting to Workbench and I get
"Your connection attempt failed for user 'USER' from your host to server at x.x.x.x:3306:
Can't connect to MySQL server on 'x.x.x.x' (10061)"
EDIT: I did notice that it show 'localhost and NOT the ipaddress when I run this cmd, but i'm not sure how to change that, or if it is even the issue. Thoughts?
# lsof -i -P | grep :3306
mysqld 5775 mysql 10u IPv4 154265 0t0 TCP localhost:3306 (LISTEN)
vim /etc/mysql/my.cnf
Change the following line to reflect as below:
(bind-address = 127.0.0.1)
bind-address = 0.0.0.0
Close the file then and restart mysql
To verify that mysql port 3306 is listening on all interfaces:
netstat -lnt | grep 3306
You should see this:
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
Find mysql config file (/etc/mysql/)
comment out the following line by putting a hash character in front of it as shown -> #bind-address = 127.0.0.1
-> Restart the server: sudo service mysql restart

cannot access mysql server under some circumstances

Here's my situation:
I have on my server mysqld running.
telnet localhost 3306 gives access
telnet 127.0.0.1 3306 gives access
telnet my_host_name 3306 doesn't give access
telnet ip_of_my_host_name 3306 doesn't give access
I checked the port: 3306 is open. My Firewall doesn't block this port
In the mysql logfile I have a message concerning the log sequence which is "in the future".
Is there a relationship between the problem and this message ?
My database uses Innodb.
Your problem seems to be something related to the binding address of the mysql daemon.
You can have a look here to get additional information about binding to several addresses (localhost and the public IP address).
Check https://dev.mysql.com/doc/refman/5.0/en/server-options.html#option_mysqld_skip-networking.
Many distributions have this setting as default. In your my.cnf, look for:
skip-networking
and comment that line out.
If you just installed MySQL, remote access is disabled by default. If that's the case, you need to edit the configuration to allow it and restart mysqld. There are several instruction sets on how to do this, such as this one.
Since you can connect to localhost, we know that you have the bind-address setting configured.
Look in your my.cnf (or my.ini) file for the following line:
bind-address = 127.0.0.1
The above line lets you connect ONLY to the local address. MySQL binds itself to the loopback IP, and nothing else.
To be able to connect to all IP addresses on the machine (bind to all addresses), including the local address, change the configuration to the following:
bind-address = 0.0.0.0
Alternatively, since 0.0.0.0 is the default, you can comment it out like so:
#bind-address = 0.0.0.0
Then restart MySQL server.
Your error message is unrelated to this issue.