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.
Related
I've installed MySQL on a Google Cloud Virtual Machine (debian buster). What I want to do is to make this accessible publicly (using username / password obviously).
As far as I can tell the server is visible from the outside world, as I can ping the IP and I get results, and I think I've set up a user correctly and given the appropriate permissions so I can log in.
For info, my firewall settings on GCP look like this (this is just the egress, there is one exactly the same for ingress):
Which I'm assuming is correct and leaves the correct port open?
The issue I have when I use MySQL Workbench is that when I try to create a new connection, it gives me the following error:
Your connection attempt failed for user 'username' to the MySQL server at [my ip address]:3306:
Unable to connect to localhost
Please:
1 Check that MySQL is running on address [my ip address]
2 Check that MySQL is reachable on port 3306 (note: 3306 is the default, but this can be changed)
3 Check the user username has rights to connect to [my ip address] from your address (MySQL rights define what clients can connect to the server and from which machines)
4 Make sure you are both providing a password if needed and using the correct password for [my ip address] connecting from the host address you're connecting from**
Any pointers would be gratefully received.
Update: What is really confusing me is the 'Unable to connect to localhost' error. I'm not trying to connect to localhost...?
Update 2: As per comments, results of the following commands:
Note I am trying to connect using the matprichardson username. The svc2toria user is pointing to my own IP address.
Mat, If you want to use your Google Cloud Instance Database using your MySQL workbench. I suggest you connect to it through an SSH tunnel. So, this problem won't happen. I also ran into this problem several times. Connecting through SSH made the job done.
But if your need is something else, this would not help you at all. If your only purpose is managing your database from your local machine using the MySQL workbench. This will work nicely. Create a USER in your Debian VM. and open port 22 to the public. Also, make sure to have strong credentials or a better key file when connecting through SSH. This method is working for every cloud VM database. I'm using this method for G-Cloud, Azure, and AWS. After all of your work is done. Close port 22 (SSH).
My best guess will be because of number 2. "Check that MySQL is reachable on port 3306 (note: 3306 is the default, but this can be changed)".
Your Virtual Machine will have network security controls / firewall which will be blocking port 3306 by default.
I don't use Google cloud but I believe you are looking for "network details" -> "Firewall rules".
did you change your mysqld.cnf already?
bind-address = 0.0.0.0
As none of the suggestions posted worked I went for the rather more nuclear option of deleting and rebuilding my VM and setting MySQL up again from scratch. I must have done something wrong in my initial setup, as things worked without any issues at all once I’d done this.
The location of the MySQL configuration file differs depending on the distribution.
In Ubuntu and Debian the file is located at /etc/mysql/mysql.conf.d/mysqld.cnf
while in Red Hat based distributions such as CentOS, the file is located at /etc/my.cnf
Open the file with your text editor :
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Search for a line that begins with bind-address and set its value to the IP address on which a MySQL server should listen.
By default, the value is set to 127.0.0.1 (listens only in localhost).
In this example, we’ll set the MySQL server to listen on all IPv4 interfaces by changing the value to 0.0.0.0
bind-address = 0.0.0.0
# skip-networking
If there is a line containing skip-networking, delete it or comment it out by adding # at the beginning of the line.
In MySQL 8.0 and higher, the bind-address directive may not be present. In this case, add it under the [mysqld] section.
Once done, restart the MySQL service for changes to take effect. Only root or users with sudo privileges can restart services.
To restart the MySQL service on Debian or Ubuntu, type:
sudo systemctl restart mysql
On RedHat based distributions like CentOS to restart the service run:
sudo systemctl restart mysqld
For more Detail Read Here
OS: Ubuntu 14.04 LTS
php version control : phpbrew
php version : 5.5.10
I pinged localhost which resolved to 127.0.0.1.
This indicates that my host (/etc/hosts) file is correct.
127.0.0.1 localhost
Whenever I try connecting to MySQL using a php script like the one below it doesn't work and gives me the error: no such directory.
//connect to the database
mysql_connect("localhost","root","password") or die(mysql_error());
However, when I connect via 127.0.0.1 it will work
//connect to the database
mysql_connect("127.0.0.1","root","password") or die(mysql_error());
Additionally, my phpmyadmin does not work when logging in using "localhost" i had to change the file to add 127.0.0.1 option during log on.
How can I use localhost to connect to the MySQL database?
PHP is still trying to use the default socket location. This problem can appear if you have moved the MariaDB/MySQL folder from /var/lib/mysql to another location. In order to solve the problem you have to define the new socket's location in the /etc/php.ini file.
mysqli.default_socket =/newDBLocation/mysql/mysql.sock
Watch out, depending on which driver you use, you may have to specify the pdo_mysql.default_socket=!
In order to check your current directory run the following command in mysql:
select ##datadir;
In many cases localhost is a hint to use the UNIX socket to connect to the MySQL server process, where 127.0.0.1 forces a TCP connection. Sometimes the UNIX socket is in an unexpected place, or simply isn't accessible, which results in the "file not found" error.
The socket is typically something like /tmp/mysql.sock or could be in some other place depending on your distribution and how much you've customized it.
Keep in mind that access via UNIX socket, which is local to the server by definition, and TCP are controlled by two different rules. localhost in a GRANT refers to UNIX socket. % or some specific host name refers to remote via TCP.
The answer to this problem was that in the my.cnf located within
/etc/mysql/my.cnf
the line was either
bind-address = 127.0.0.1
or
bind-address = localhost
it should have been
bind-address = 0.0.0.0
which will allow all connections
I have a Mac running Mavericks with MySQL installed from homebrew. When I try nmap localhost and I see:
3306/tcp open mysql
When I try this same command with my computer name:
nmap my_comp_name
I do not get 3306 but I get:
PORT STATE SERVICE
80/tcp open http
3128/tcp open squid-http
8080/tcp open http-proxy
It also says my computer name resolves to some ip address. When I try nmap with that ip address, I get the same response as above. I want to be able to connect to this database with Visual Studio running on my Parallels VM.
This happened some time yesterday and I finally figured out that Norton anti virus was installed blocking that port. So I uninstalled Norton with one of their scripts and restarted. Everything was working fine at work, and then I came home and again, the port is closed.
I do not have a firewall on for either my Mac or Windows (Parallels VM) machine. I cannot figure out why this port gets closed. I do not have a firewall set up on my home network either. This port has been opened in the past on my home network as well. I am at a loss of figuring out what is causing this port to just suddenly close without even rebooting my computer. I basically come home, and port 3306 is now closed. Any thoughts?
Edit: I have also tried adding port = 3306 in the my.cnf file, restarted apache, and that also does not solve my problem.
When you run nmap against localhost it uses IP address 127.0.0.1. When you run it against your hostname, it uses the 'real' IP address of your host.
Clearly you have MySQL configured to bind only on IP address 127.0.0.1, and need to change the config to bind to all addresses if you want to access MySQL from a different host (even if the host is running in a VM inside your machine).
Find the file /etc/my.cnf and see if it contains a line like
bind-address=127.0.0.1
If it does, remove it or change it to
bind-address=0.0.0.0
If my.cnf does not contain a line binding to 127.0.0.1, then you may have to find the script that starts MySQL and examine it to see how it sets the bind address.
You don't need to create or edit my.cnf
Open mysql launch file
vi ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
And change this line
<string>--bind-address=127.0.0.1</string>
to
<string>--bind-address=0.0.0.0</string>
And finally reload MySQL
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
This seems like a binding problem. It seems like it is binding to your private ip instead of all or 127.0.0.1/localhost. But that is just from the top of my head.
From the mysql documentation:
The MySQL server listens on a single network socket for TCP/IP
connections. This socket is bound to a single address, but it is
possible for an address to map onto multiple network interfaces. The
default address is 0.0.0.0. To specify an address explicitly, use the
--bind-address=addr option at server startup, where addr is an IPv4 address or a host name. If addr is a host name, the server resolves
the name to an IPv4 address and binds to that address. The server
treats different types of addresses as follows:
If the address is 0.0.0.0, the server accepts TCP/IP connections on
all server host IPv4 interfaces. If the address is a "regular" IPv4
address (such as 127.0.0.1), the server accepts TCP/IP connections
only for that particular IPv4 address.
Configuration
You can set bind-address directive in my.cnf. Edit /etc/my.cnf or /usr/local/etc/my.cnf, run:
$ nano /etc/my.cnf
Set the address to 0.0.0.0:
bind-address = 0.0.0.0
Make sure you delete the following line or comment out the following line:
#skip-networking
Save and close the file.
Finally restart the mysql service.
source.
I'd like to connect to MySQL (deamon is running on my VPS) via HeidiSQL. I've created new user, commented bind-adress option and when I try to connect with it via HeidiSQL, I've got an error 2003: Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10061).
What should I do?
It could be a number of factors.
See if a firewall is blocking your traffic to the other host
Can you simply ping the host from the client machine?
Can you also open a simple telnet session to the host on port 3306 ?
(If the telnet is accepted, you will probably see some characters appear and you will remain in the telnet session for a few seconds before the connection is closed. If not accepted, you will see the message Connection refused.)
There's a simple checklist for this:
Is your MySQL server bound to "localhost" only? It might be listening for connections only on 127.0.0.1 or ::1 instead of any which is usually 0.0.0.0. Try connecting on your server to your server with mysql --host=host_ip where host_ip is your network IP address. I think the default is localhost-only.
Is port 3306 firewalled? Many distributions allow only SSH by default, so you may need to open this up to your client machine. Try not to open this up to everyone on the internet as having an open MySQL port is asking for trouble. It's always best to limit access to a set of specific IPs if possible.
Can you connect via an SSH tunnel instead? This is far more secure as it means your 3306 port is properly firewalled. There are many tools for creating SSH tunnels, but the basic principle is to forward a local port of your choosing to the remote server's port 3306.
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