(HY000/2003): Can't connect to MySQL server on DOMAIN (111) [duplicate] - php

For some reason, I've been unable to connect remotely to my MySQL server. I've tried everything and I'm still getting errors.
root#server1:/home/administrator# mysql -u monty -p -h www.ganganadores.cl
Enter password:
ERROR 1045 (28000): Access denied for user 'monty'#'server1.ganganadores.cl' (using password: YES)
Now, I've tried running
GRANT ALL ON *.* to monty#localhost IDENTIFIED BY 'XXXXX';
GRANT ALL ON *.* to monty#'%' IDENTIFIED BY 'XXXXXX';`
and still nothing!
What I'm doing wrong?
EDIT: my.cnf has commented out the bind ip .

To expose MySQL to anything other than localhost you will have to have the following line
For mysql version 5.6 and below
uncommented in /etc/mysql/my.cnf and assigned to your computers IP address and not loopback
For mysql version 5.7 and above
uncommented in /etc/mysql/mysql.conf.d/mysqld.cnf and assigned to your computers IP address and not loopback
#Replace xxx with your IP Address
bind-address = xxx.xxx.xxx.xxx
Or add a
bind-address = 0.0.0.0 if you don't want to specify the IP
Then stop and restart MySQL with the new my.cnf entry. Once running go to the terminal and enter the following command.
lsof -i -P | grep :3306
That should come back something like this with your actual IP in the xxx's
mysqld 1046 mysql 10u IPv4 5203 0t0 TCP xxx.xxx.xxx.xxx:3306 (LISTEN)
If the above statement returns correctly you will then be able to accept remote users. However for a remote user to connect with the correct priveleges you need to have that user created in both the localhost and '%' as in.
CREATE USER 'myuser'#'localhost' IDENTIFIED BY 'mypass';
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypass';
then,
GRANT ALL ON *.* TO 'myuser'#'localhost';
GRANT ALL ON *.* TO 'myuser'#'%';
and finally,
FLUSH PRIVILEGES;
EXIT;
If you don't have the same user created as above, when you logon locally you may inherit base localhost privileges and have access issues. If you want to restrict the access myuser has then you would need to read up on the GRANT statement syntax HERE If you get through all this and still have issues post some additional error output and the my.cnf appropriate lines.
NOTE: If lsof does not return or is not found you can install it HERE based on your Linux distribution. You do not need lsof to make things work, but it is extremely handy when things are not working as expected.
UPDATE: If even after adding/changing the bind-address in my.cnf did not work, then go and change it in the place it was originally declared:
/etc/mysql/mariadb.conf.d/50-server.cnf

Add few points on top of apesa's excellent post:
1) You can use command below to check the ip address mysql server is listening
netstat -nlt | grep 3306
sample result:
tcp 0 0 xxx.xxx.xxx.xxx:3306 0.0.0.0:* LISTEN
2) Use FLUSH PRIVILEGES to force grant tables to be loaded if for some reason the changes not take effective immediately
GRANT ALL ON *.* TO 'user'#'localhost' IDENTIFIED BY 'passwd' WITH GRANT OPTION;
GRANT ALL ON *.* TO 'user'#'%' IDENTIFIED BY 'passwd' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
user == the user u use to connect to mysql ex.root
passwd == the password u use to connect to mysql with
3) If netfilter firewall is enabled (sudo ufw enable) on mysql server machine, do the following to open port 3306 for remote access:
sudo ufw allow 3306
check status using
sudo ufw status
4) Once a remote connection is established, it can be verified in either client or server machine using commands
netstat -an | grep 3306
netstat -an | grep -i established

MySQL only listens to localhost, if we want to enable the remote access to it, then we need to made some changes in my.cnf file:
sudo nano /etc/mysql/my.cnf
We need to comment out the bind-address and skip-external-locking lines:
#bind-address = 127.0.0.1
# skip-external-locking
After making these changes, we need to restart the mysql service:
sudo service mysql restart

You are using ubuntu 12 (quite old one)
First, Open the /etc/mysql/mysql.conf.d/mysqld.cnf file (/etc/mysql/my.cnf in Ubuntu 14.04 and earlier versions
Under the [mysqld] Locate the Line,
bind-address = 127.0.0.1
And change it to,
bind-address = 0.0.0.0
or comment it
Then, Restart the Ubuntu MysQL Server
systemctl restart mysql.service
Now Ubuntu Server will allow remote access to the MySQL Server, But still you need to configure MySQL users to allow access from any host.
User must be 'username'#'%' with all the required grants
To make sure that, MySQL server listens on all interfaces, run the netstat command as follows.
netstat -tulnp | grep mysql
Hope this works !

If testing on Windows, don't forget to open port 3306.

In my case I was using MySql Server version: 8.0.22
I had to add
bind-address = 0.0.0.0
and change this line to be
mysqlx-bind-address = 0.0.0.0
in file at
/etc/mysql/mysql.conf.d
then restart MySQL by running
sudo service mysql restart

I was facing the same problem when I was trying to connect Mysql to a Remote Server. I had found out that I had to change the bind-address to the current private IP address of the DB server.
But when I was trying to add the bind-address =0.0.0.0 line in my.cnf file, it was not understanding the line when I tried to create a DB.
Upon searching, I found out the original place where bind-address was declared.
The actual declaration is in : /etc/mysql/mariadb.conf.d/50-server.cnf
Therefore I changed the bind-address directly there and then all seems working.

Related

Can't connect to my online database with PDO and PHP [duplicate]

I am getting the following error when I try to connect to mysql:
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Is there a solution for this error? What might be the reason behind it?
Are you connecting to "localhost" or "127.0.0.1" ? I noticed that when you connect to "localhost" the socket connector is used, but when you connect to "127.0.0.1" the TCP/IP connector is used. You could try using "127.0.0.1" if the socket connector is not enabled/working.
Ensure that your mysql service is running
service mysqld start
Then, try the one of the following following:
(if you have not set password for mysql)
mysql -u root
if you have set password already
mysql -u root -p
If your file my.cnf (usually in the etc folder) is correctly configured with
socket=/var/lib/mysql/mysql.sock
you can check if mysql is running with the following command:
mysqladmin -u root -p status
try changing your permission to mysql folder. If you are working locally, you can try:
sudo chmod -R 777 /var/lib/mysql/
that solved it for me
The MySQL server is not running, or that is not the location of its socket file (check my.cnf).
Most likely mysql.sock does not exist in /var/lib/mysql/.
If you find the same file in another location then symlink it:
For ex: I have it in /data/mysql_datadir/mysql.sock
Switch user to mysql and execute as mentioned below:
su mysql
ln -s /data/mysql_datadir/mysql.sock /var/lib/mysql/mysql.sock
That solved my problem
If you are on a recent RHEL, you may need to start mariadb (an open source mysql db) instead of the mysql db:
yum remove mysql
yum -y install mariadb-server mariadb
service mariadb start
You should then be able to access mysql in the usual fashion:
mysql -u root -p
Just edit /etc/my.cnf
Add following lines to my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
Restart mysql and connect again
mysql -u user -p password database -h host;
In my case I have moved socket file to another location inside /etc/my.cnf
from /var/lib/mysql/mysql.sock to /tmp/mysql.sock
Even after restarting the mysqld service, I still see the error message when I try to connect.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
The problem is with the way that the client is configured. Running diagnostics will actually show the correct socket path. eg ps aux | grep mysqld
Works:
mysql -uroot -p -h127.0.0.1
mysql -uroot -p --socket=/tmp/mysql.sock
Does not Work:
mysql -uroot -p
mysql -uroot -p -hlocalhost
You can fix this problem by adding the same socket line under [client] section inside mysql config.
Check if your mysqld service is running or not, if not run, start the service.
If your problem isn't solved, look for /etc/my.cnf and modify as following, where you see a line starting with socket. Take a backup of that file before doing this update.
socket=/var/lib/mysql/mysql.sock
Change to
socket=/opt/lampp/var/mysql/mysql.sock -u root
MariaDB, a community developed fork of MySQL, has become the default implementation of MySQL in many distributions.
So first you should start,
$ sudo systemctl start mariadb
If this fails rather try,
$ sudo systemctl start mysqld
Then to start mysql,
$ mysql -u root -p
As of today, in Fedora the package is named mariadb
And in Ubuntu it is called mariadb-server.
So you may have to install it if its not already installed in your system.
Make sure you have enough space left in /var. If Mysql demon is not able to write additional info to the drive the mysql server won't start and it leads to the error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Consider using
expire_logs_days = 10
max_binlog_size = 100M
This will help you keep disk usage down.
Please check whether another mysql service is running.
Make sure you started the server:
mysql.server start
Then connect with root user:
mysql -uroot
Here's what worked for me:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
service mysqld restart
One way to reproduce this error: If you meant to connect to a foreign server but instead connect to the non existent local one:
eric#dev ~ $ mysql -u dev -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (2)
eric#dev ~ $
So you have to specify the host like this:
eric#dev ~ $ mysql --host=yourdb.yourserver.com -u dev -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 235
Server version: 5.6.19 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+-------------------------+
| Database |
+-------------------------+
| information_schema |
| mysql |
| performance_schema |
+-------------------------+
3 rows in set (0.00 sec)
mysql> exit
Bye
eric#dev ~ $
If your mysql was previously working and has stopped suddenly just "reboot" the server.
Was facing this issue on my CentOS VPS.->
Was constantly getting
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'(2)
Tried all techniques, finally restarting the server fixed the issues ->
shutdown -r now
Hope this helps !!
try
echo 0 > /selinux/enforce
if you change files in /var/lib/mysql [ like copy or replace that ], you must set owner of files to mysql this is so important if mariadb.service restart has been faild
chown -R mysql:mysql /var/lib/mysql/*
chmod -R 700 /var/lib/mysql/*
First enter "service mysqld start" and login
It worked for me with the following changes
Whatever path for socket is mentioned in [mysqld] and same in [client] in my.cnf and restart mysql
[mysqld]
socket=/var/lib/mysql/mysql.sock
[client]
socket=/var/lib/mysql/mysql.sock
Please ensure you have installed MySQL server correctly, I met this error many times and I think it's complicated to debug from the socket, I mean it might be easier to reinstall it.
If you are using CentOS 7, here is the correct way to install it:
First of all, add the mysql community source
yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
Then you can install it by yum install mysql-community-server
Start it with systemctl: systemctl start mysqld
My problem was that I installed mysql successfully and it worked fine.
But one day, the same error occurred.
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
And no mysql.sock file existed.
This sollution solved my problem and mysql was up and running again:
Log in as root:
sudo su -
Run:
systemctl stop mysqld.service
systemctl start mysqld.service
systemctl enable mysqld.service
Test as root:
mysql -u root -p
mysql should now be up and running.
I hope this can help someone else as well.
Note that while mysql reads the info of the location of the socketfile from the my.cnf file, the mysql_secure_installation program seems to not do that correctly at times.
So if you are like me and shuffle things around at installationtime you might get into the situation where you can connect to the database with mysql just fine, but the thing can not be secured (not using that script anyway).
To fix this the suggestion from sreddy works well: make a softlink from where the script would expect the socket to where it actually is. Example:
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
(I use /tmp/ as a default location for sockets)
This might be a stupid suggestion but make 100% sure your DB is still hosted at localhost. For example, if a Network Admin chose (or changed to) Amazon DB hosting, you will need that hostname instead!
In my case, I was importing a new database, and I wasnt able to connect again after that. Finally I realized that was a space problem.
So you can delete the last database and expand you hard drive or what I did, restored a snapshot of my virtual machine.
Just in case someone thinks that is useful
I came to this issue when i reinstall mariadb with yum, which rename my /etc/my.cnf.d/client.cnf to /etc/my.cnf.d/client.cnf.rpmsave but leave /etc/my.cnf unchanged.
For I has configed mysqld's socket in /etc/my.cnf, and mysql's socket in /etc/my.cnf.d/client.cnf with customized path.
So after the installation, mysql client cannot find the mysql's socket conf, so it try to use the default socket path to connect the msyqld, which will cause this issue.
Here are some steps to locate this isue.
check if mysqld is running with ps -aef | grep mysqld
$ps -aef | grep mysqld | grep -v grep
mysql 19946 1 0 09:54 ? 00:00:03 /usr/sbin/mysqld
if mysqld is running, show what socket it use with netstat -ln | grep mysql
$netstat -ln | grep mysql
unix 2 [ ACC ] STREAM LISTENING 560340807 /data/mysql/mysql.sock
check if the socket is mysql client trying to connect.
if not, edit /etc/my.conf.d/client.cnf or my.conf to make the socket same with it in mysqld
[client]
socket=/data/mysql/mysql.sock
You also can edit the mysqld's socket, but you need to restart or reload mysqld.
Just rain into the same problem -- and here's how I addressed it.
Assuming mysqld is running, then the problem might just be the mysql client not knowing where to look for the socket file.
The most straightforward way to address this consists in adding the following line to your user's profile .my.cnf file (on linux that's usually under /home/myusername):
socket=<path to the mysql socket file>
If you don't have a .my.cnf file there, then create one containing the following:
[mysql]
socket=<path to the mysql socket file>
In my case, since I moved the mysql default data folder (/var/lib/mysql) in a different location (/data/mysql), I added to .my.cnf the following:
[mysql]
socket=/data/mysql/mysql.sock
Hope this helps.
ran into this issue while trying to connect mysql in SSH client, found adding the socket path to the command helpful when switching between sockets is necessary.
> mysql -u user -p --socket=/path/to/mysql5143.sock
This is a problem if you are running out of disk space.
Solution is to free some space from the HDD.
Please read more to have the explanation :
If you are running MySQL at LINUX check the free space of HDD with the command disk free :
df
if you are getting something like that :
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 5162828 4902260 0 100% /
udev 156676 84 156592 1% /dev
/dev/sda3 3107124 70844 2878444 3% /home
Then this is the problem and now you have the solution!
Since mysql.sock wants to be created at the mysql folder which is almost always under the root folder could not achieve it because lack of space.
If you are periodicaly give the ls command under the mysql directory (at openSUSE 11.1 is at /var/lib/mysql) you will get something like :
hostname:/var/lib/mysql #
.protected IT files ibdata1 mysqld.log systemtemp
.tmp NEWS greekDB mysql mysqld.pid test
ARXEIO TEMP1 ib_logfile0 mysql.sock polis
DATING deisi ib_logfile1 mysql_upgrade_info restore
The mysql.sock file appearing and disappearing often (you must to try allot with the ls to hit a instance with the mysql.sock file on folder).
This caused by not enough disk space.
I hope that i will help some people!!!!
Thanks!
I had to disable explicit_defaults_for_timestamp from my.cnf.

Can't access database - access denied [duplicate]

Following this thread. I've successfully edited my my.cnf file to comment out the #bind-address value and also tried to specify 0.0.0.0 and the specific IP address I want to allow connections.
I do notice that while doing this my.cnf is linked like:
lrwxrwxrwx 1 root root 24 Sep 9 06:21 my.cnf -> /etc/alternatives/my.cnf
and then this my.cnf is also linked:
lrwxrwxrwx 1 root root 20 Sep 9 06:23 my.cnf -> /etc/mysql/mysql.cnf
So in reality, I'm editing the mysql.cnf file.
I stop/start the MYSQL server.
I then run the netstat -nat | grep :3306 and it gives me a:
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
I then go over to the client and get the Can't connect to MySQL server on 'server_name'
What am I doing wrong?
the bind address to 0.0.0.0 is just part of the steps for allowing it to accept remote connections. Those steps include the rem'ing out explicitly with # skip-networking
[mysqld]
bind-address = 0.0.0.0
# skip-networking
And a server restart.
You then need a user,host combo for login and ideally a GRANT to a db to use with adequate (not excessive) rights.
You can see your current users with select user,host from mysql.user
Please see the MySQL Manual page on GRANT Syntax.
I wrote a little answer Here about the wildcard % host and other minor details.
An illustration for a test is below:
create schema testDB007;
use testDB007;
create table t1
( id int not null
);
CREATE USER 'jeffrey123z'#'%' IDENTIFIED BY 'mypass123^';
-- note password is mypass123^
GRANT ALL ON testDB007.* TO 'jeffrey123z'#'%';
SHOW GRANTS FOR 'jeffrey123z'#'%';
Now, the blue row above (USAGE) means almost nothing other than the user can login and that is it. The 2nd row shows the PRIVILEGES for the db from the GRANT cmd.
View user in mysql.user:
Concerning the above picture,
select user,host,password from mysql.user where user='jeffrey123z';
select user,host,authentication_string from mysql.user where user='jeffrey123z';
The first query above is for prior to MySQL 5.7. The second query is for 5.7 and after. The password is hashed. The host is the wildcard % meaning login from any host.
I had the same issue: mysql configured correctly, sudo systemctl restart mysql seemed to work fine, but sudo lsof -i -P -n | grep 3306 still gave me TCP 127.0.0.1:3306 (LISTEN) instead of the expected TCP *:3306 (LISTEN), and I noticed from ps aux | grep mysql that /usr/bin/mysqld hadn't actually been restarted.
A full reboot was necessary for my new settings to take effect.

Unable to connect to Raspberry Pi 3 MySQL via weaved (remote access) or from Pi"s IP

I am kind of new in the world of Raspberry Pi and Linux in general and I've run into a problem.
I installed weaved to have remote access to my raspberry Pi on my other computers. I have access to the web pages ive put in the Pi's www folder , phpmyadmin and page with php script (without SQL). However, as soon as page need a SQL connection, no information is displayed at all and I don't receive an error either. The same problem happen if I type the IP address of the Pi in the URL. Those pages works on localhost thought. The problem seems very similar to this post How do I open up my MySQL on my Raspberry Pi for Outside / Remote Connections? but after trying the first solution and second solution, it doesn't work at all.
Any idea of what would be the issue?
If your issue is not able to remotely connect with MySQL on Raspberry Pi, then try below steps. I had the same issue and got it resolved by performing below commands.
1) sudo nano /etc/mysql/my.cnf
2) # bind-address = 127.0.0.1 // comment this line out
bind-address = 0.0.0.0 //add this line just below above line
3) sudo /etc/init.d/mysql restart //restart mysql
4) sudo mysql -u root -p //login to mysql cli as user 'root'
5) GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'beta' WITH GRANT OPTION;
Here 'root' is the mysql user and 'beta' is the password. Change it accordingly and execute above query in mysql cli.

Can't access MySQL remotely on my work development machine

Today I've been trying to access MySQL running on my remote work machine (Ubuntu), from my home machine (OSX), and I've had no success.
On my work machine, I've checked the following:
sudo netstat -ntlup | grep mysql
gives
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 23692/mysqld
and nmap tells me
PORT STATE SERVICE
3306/tcp open mysql
and in /etc/mysql/my.cnf I've set bind-address = 0.0.0.0
I've also run the following iptables rules:
iptables -A INPUT -i lo -p tcp --dport 3306 -j ACCEPT
iptables -A OUTPUT -o lo -p tcp --sport 3306 -j ACCEPT
What else can I check?
If there is some corporate network rule blocking my MySQL access, what could I check to find this?
The only way I can make this work is ssh tunnelling:
ssh -L 8080:localhost:3306 my_user_name#my_dev_machine_IP
and then if I do
mysql -h 127.0.0.1 --port=8080 -u root -p
I can get in. If I close the ssh tunnel, then I can't log in via mysql, nor even via telnet remote_IP 3306.
But this is no good for what I want, because I need the PHP code running on my local machine to be able to access the remote database.
I've also (hopefully temporarily) opened up the access to the MySQL databases to all hosts/IP:
GRANT ALL PRIVILEGES ON *.* TO 'application_username'#'%'
FLUSH PRIVILEGES
Any advice gratefully received!
Ubuntu has uwf - in some vps installations it is enabled by default. So, if it is, do sudo ufw allow 3306. I am writing this jsut in case you run nmap from your work machine. If nmap shows that result run from your mac, then this does not apply.

Connecting to Remote Server MySQL Issue

I'm attempting to connect to a remote server, which I'll refer to as machine A. I've created a user following the instructions here
CREATE USER 'monty'#'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
WITH GRANT OPTION;
CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
WITH GRANT OPTION;
On machine A I can run the command
mysql -u monty -h website.com -p
This connects to sql with no problem. However, when attempting to do this from some machine B I receive the error:
ERROR 2003 (HY000): Can't connect to MySQL server on 'website.com' (113)
I've also commented out the following line:
# bind-address = 127.0.0.1
in the /etc/mysql/my.cnf file. Still no luck connecting from a remote connection. Any obvious things that I might be missing? Any feedback as always is very much appreciated.
I think it's your GRANT that needs fixing.
GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
Might need to be
GRANT ALL PRIVILEGES ON *.* TO 'monty'#'website.com'
You're going to want to make sure things are secure though.
It's usually best practice to try not to allow outside mysql connects that aren't from localhost.
It looks like the 'website.com' address cannot be resolved from the machine B. Please try to connect the MySQL server using the IP address of machine A, i.e.:
mysql -u monty -h x.x.x.x -p
If it will work, please make sure you mapped the IP address of the machine A to the name 'website.com' correctly.
My problem was that the firewall was blocking the connection.
I was using CentOS 7 and was getting this error:
mysql -usomeuser -h192.168.194.4 -p somedb
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.194.4' (113)
So, I installed telnet to try and got this:
[root#vm3 config]# telnet 192.168.194.4 3306
Trying 192.168.194.4...
telnet: connect to address 192.168.194.4: No route to host
and as others noted, the error 113 is "No route to host" which is not a MySQL config issue.
I could have just opened 3306 to the world or just the one IP I was connecting from, but instead, I decided to create a new zone since it was for my ESX host's internal "hostonly" network.
On the host running MySQL (MariaDB), I ran these firewall commands:
firewall-cmd --new-zone=esxlocalhost --permanent
firewall-cmd --reload
firewall-cmd --zone=esxlocalhost --permanent --add-source=192.168.194.0/24
firewall-cmd --zone=esxlocalhost --permanent --add-port=3306/tcp
firewall-cmd --reload
Once that was done, I could connect on the client:
mysql -usomeuser -h192.168.194.4 -p somedb
Enter password:
And life was good

Categories