'localhost' is not allowed to connect to this MySQL server - php

When I tried to connect to my http://localhost/phpmyadmin the following error appeared:
#1130 - Host 'localhost' is not allowed to connect to this MySQL server
I tried to connect to MySQL from command line and the same error appeared as well.
I used root as user, there are many suggestions that tell to do some scripts from MySQL, but I can't log in MySQL even though with root user. I have a root access but I can't log in because of that error, that error appears when I try to log in.

You could start MySQL with the --skip-grant-tables option
service mysqld stop
mysqld_safe --skip-grant-tables &
Then allow the user you're logging in as access from localhost:
grant all on <db>.<table> to '<user>'#'localhost' identified by password('<password>')
Then start MySQL normally:
service mysqld start
Replace all the <word> placeholders above with the actual settings. If you have root access you can just log in with the root user and run the grant line.

Related

ERROR 2003 (HY000) while trying to connect to mysql remotely

I am trying to connect to mysql remotely. Because this is such a common question I read several tutorials/questions but I keep getting this error:
ERROR 2003 (HY000): Can't connect to MySQL server on 'the-ip-address' (60)
I edited the /etc/mysql/my.cnf and commented the line of bind_address like this:
#bind-address = 127.0.0.1
After that I ran:
$ sudo service mysql restart
with the output of:
mysql stop/waiting
mysql start/running, process 9853
mysql is running on default port: 3306
Using the user with which I am trying to connect remotely is working locally on the server (I can connect to the mysql from the server).
In phpmyadmin I configure these users:
user#localhost
user#127.0.0.1
user#87.45.34.23
Then I am trying to connect with the following:
mysql -u user -p -h 87.45.34.23
Maybe I am missing something... Thank you in advanced
UPDATE
As #Geoffrey suggested in the comments the problem was with a firewall. For this reason I will accept his answer although the answer itself is not for that but in the comments he was right.
Connection refused means the MySQL server is not listening or is firewalled.
By commenting out the bind line, MySQL doesn't bind to anything and only allows local socket access, you need to bind it to either the local IP, or all IPs by specifying 0.0.0.0
Also ensure that skip-networking is not set anywhere.

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.

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

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.

What's the cause of this "PHP/MySQL Can't Connect Through Socket" error? [duplicate]

I am having a big problem trying to connect to mysql. When I run:
/usr/local/mysql/bin/mysql start
I have the following error :
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
I do have mysql.sock under the /var/mysql directory.
In /etc/my.cnf I have:
[client]
port=3306
socket=/var/mysql/mysql.sock
[mysqld]
port=3306
socket=/var/mysql/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M
and in /etc/php.ini I have :
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
mysql.default_socket = /var/mysql/mysql.sock
I have restarted apache using sudo /opt/local/apache2/bin/apachectl restart
But I still have the error.
Otherwise, I don't know if that's relevant but when I do mysql_config --sockets I get
--socket [/tmp/mysql.sock]
If your file my.cnf (usually in the /etc/mysql/ 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 755 /var/lib/mysql/
That solved it for me.
Are you sure you installed MySQL as well as MySQL server?
For example to install MySQL server I'll use yum or apt to install both MySQL command line tool and the server:
yum -y install mysql mysql-server (or apt-get install mysql mysql-server)
Enable the MySQL service:
/sbin/chkconfig mysqld on
Start the MySQL server:
/sbin/service mysqld start
Afterwards set the MySQL root password:
mysqladmin -u root password 'new-password' (with the quotes)
A quick workaround that worked for me: try using the local ip address (127.0.0.1) instead of 'localhost' in mysql_connect(). This "forces" php to connect through TCP/IP instead of a unix socket.
I got the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
Tried several ways and finally solved it through the following way:
sudo gksu gedit /etc/mysql/my.cnf
Modified:
#bind-address = 127.0.0.1
to:
bind-address = localhost
and restarted:
sudo /etc/init.d/mysql restart
It worked.
Make sure you are running mysqld : /etc/init.d/mysql start
I got this error when I set cron job for my file. I changed the permissions of file to 777 but it still not worked for me. Finally I got the solution. May be it will be helpful for others.
Try with this command:
mysql -h 127.0.0.1 -P 3306 -u root -p
Remember that -h means host, -P means port and -p means password.
To prevent the problem from occurring, you must perform a graceful shutdown of the server from the command line rather than powering off the server.
shutdown -h now
This will stop the running services before powering down the machine.
Based on Centos, an additional method for getting it back up again when you run into this problem is to move mysql.sock:
mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak
service mysqld start
Restarting the service creates a new entry called mqsql.sock
As can be seen by the many answers here, there are lots of problems that can result in this error message when you start the MySQL service. The thing is, MySQL will generally tell you exactly what's wrong, if you just look in the appropriate log file.
For example, on Ubuntu, you should check /var/log/syslog. Since lots of other things might also be logging to this file, you probably want to use grep to look at mysql messages, and tail to look at only the most recent. All together, that might look like:
grep mysql /var/log/syslog | tail -50
Don't blindly make changes to your configuration because someone else said 'This worked for my system.' Figure out what is actually wrong with your system and you'll get a better result much faster.
Another workaround is to edit /etc/my.cnf and include host in the section [client]
[client]
#password = your_password
host = 127.0.0.1
port = 3306
socket = /var/run/mysql/mysql.sock
And then restarting the mysql service.
This workaround was tested in: Server version: 5.5.25a-log Source distribution
Try with -h (host) and -P(port):
mysql -h 127.0.0.1 -P 3306 -u root -p
I had the same problem and it has been caused by an update of mysql drivers when mysql server was running.
I fixed it just restarting both mysql and apache2:
sudo service mysql stop
sudo service mysql start
sudo service apache2 stop
sudo service apache2 start
sudo service mysql start
This should serve you just fine. There could be a possibility that you changed some commands that affected the mysql configurations.
In my case, I was using Centos 5.5. I found that the problem was because the mysql service was stopped some how.
So I started mysql service with the command:
/etc/init.d/mysqld start
So.. silly mistake.
If everything worked just fine and you just started seeing this error, before you do anything else, make sure you're not out of disk space:
df -h
If the volume where the mysql.sock is being created is at 100% use, MySql won't be able to create it and this will be the cause of this error. All you need to do is delete something that's not needed, like old log files.
There are many solutions to this problem but for my situation, I just needed to correct the DATE on the machine/server (Ubuntu 16.04 Server).
i) Check the date of your server and correct it.
ii) Run sudo /etc/init.d/mysql restart
That should get it started.
I was getting the error because I was running MAMP and my .sock file was in a different location. I just added a symbolic link where the app thought it should be that pointed to where it actually was and it worked like a charm.
If you are using AWS (Amazon Web Services) Micro version, then it is a memory issue. When I ran
mysql
from the terminal it would say
ERROR 2002 (HY000): Can't connect to local MySQL server through socket /var/run/mysqld/mysqld.sock' (111)
So I tried the following and it would just fail.
service mysqld restart
After much searching, I found out that you have to create a swap file for MySQL to have enough memory. Instructions are listed: http://www.prowebdev.us/2012/05/amazon-ec2-linux-micro-swap-space.html.
Then, I was able to restart mysqld.
I also found that this was a permissions problem. I compared the MySQL files to a working install (both on Debian 6 squeeze) and had to make the following ownership changes (where mydatabase is any database(s) you have).
Ownership mysql:mysql:
chown mysql:mysql /var/lib/mysql
chown mysql:mysql /var/lib/mysql/ib*
chown mysql:mysql /var/lib/mysql/mydatabase
chown mysql:mysql /var/lib/mysql/mydatabase/*
chown mysql:mysql /var/lib/mysql/mysql/*
Ownership mysql:root:
chown mysql:root /var/lib/mysql/mysql
chown mysql:root /var/run/mysqld
Ownership mysql:adm:
chown mysql:adm /var/log/mysql
chown mysql:adm /var/log/mysql.err
chown mysql:adm /var/log/mysql.log*
For me - this was simply a case of MySQL taking a long time to load. I have over 100,000 tables in one of my databases and it did eventually start but obviously has to take a long time in this instance.
You might want to chek if the hard disk is full (df on the console), that's what ultimately triggered this error for me.
you can always start mysql server by specifying the location of the mysql.sock file using the --socket option like
mysql --socket=/var/mysql/mysql.sock
This will work even if the location of socket file in specified in a different location in the my.cnf file.
For those whose any solution did not work, try:
cd /etc/mysql
check if my.cnf is present
nano my.cnf
and make sure you have only one bind-address as follows:
bind-address = 127.0.0.1
If not, that might be the problem, just exit nano and save the file.
and service mysql start
note that if you don't have nano (its a text editor) just install it with apt-get install nano and once in just press Ctrl+X to exit, dont forget to say Y to save and use the same file)
sudo service mysqld start
Worked for me, I'm using Centos
I had this problem too when trying to start the server, so many of the answers here that just say to start the server didn't work. The first thing you can do is execute the following to see if there are any config errors:
/usr/sbin/mysqld --verbose --help 1>/dev/null
I did have one error that showed up:
160816 19:24:33 [Note] /usr/sbin/mysqld (mysqld 5.5.50-0ubuntu0.14.04.1-log) starting as process 9461 ...
160816 19:24:33 [Warning] Using unique option prefix myisam-recover instead of myisam-recover-options is deprecated and will be removed in a future release. Please use the full name instead.
160816 19:24:33 [Note] Plugin 'FEDERATED' is disabled.
160816 19:24:33 [ERROR] /usr/sbin/mysqld: unknown variable 'innodb-online-alter-log-max-size=4294967296'
160816 19:24:33 [ERROR] Aborting
A simple grep -HR "innodb-online-alter-log-max-size" /etc/mysql/ showed me exactly what file contained the offending line, so I removed that line from the file.
Then, checking my /var/log/mysql/error.log file I had:
InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
InnoDB: than specified in the .cnf file 0 671088640 bytes!
160816 22:46:46 [ERROR] Plugin 'InnoDB' init function returned error.
160816 22:46:46 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
160816 22:46:46 [ERROR] Unknown/unsupported storage engine: InnoDB
160816 22:46:46 [ERROR] Aborting
Based on this question the accepted solution wouldn't work because I couldn't even get the server started, so I followed what some of the comments said and deleted my /var/lib/mysql/ib_logfile0 and /var/lib/mysql/ib_logfile1 files.
This allowed the server to start and I was able to connect and execute queries, however checking my error log file it was quickly getting filled up with several tens of thousands of lines like this:
160816 22:52:15 InnoDB: Error: page 1415 log sequence number 82039318708
InnoDB: is in the future! Current system log sequence number 81640793100.
InnoDB: Your database may be corrupt or you may have copied the InnoDB
InnoDB: tablespace but not the InnoDB log files. See
InnoDB: http://dev.mysql.com/doc/refman/5.5/en/forcing-innodb-recovery.html
InnoDB: for more information.
Based on a suggestion from here, to fix this I did a mysqldump and restore of all databases (see the link for several other solutions).
$ mysqldump -u root -p --allow-keywords --add-drop-database --comments --hex-blob --opt --quote-names --databases db_1 db_2 db_3 db_etc > backup-all-databases.sql
$ mysql -u root -p < backup-all-databases.sql
Everything appears to be working as expected now.
When you use localhost to connect to MySQL, the operating system uses the socket connector. However, if you use the 127.0.0.1 IP address, the operating system will use the TCP/IP connector. So, a possible solution when you’re having issues with the socket connector is trying to establish the connection using TCP/IP by specifying the 127.0.0.1 IP address instead of localhost.
Adding
--protocol=tcp
to the list of pramaters in your connection worked for me.
This was good enough for me
sudo /etc/init.d/mysql restart
I ran into this issue today. None of these answers provided the fix. I needed to do the following commands (found here https://stackoverflow.com/a/20141146/633107) for my mysql service to start:
sudo /etc/init.d/mysql stop
cd /var/lib/mysql/
ls ib_logfile*
mv ib_logfile0 ib_logfile0.bak
mv ib_logfile1 ib_logfile1.bak
... etc ...
/etc/init.d/mysql restart
This was partly indicated by the following errors in /var/log/mysql/error.log:
140319 11:58:21 InnoDB: Completed initialization of buffer pool
InnoDB: Error: log file ./ib_logfile0 is of different size 0 50331648 bytes
InnoDB: than specified in the .cnf file 0 5242880 bytes!
140319 11:58:21 [ERROR] Plugin 'InnoDB' init function returned error.
140319 11:58:21 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
140319 11:58:21 [ERROR] Unknown/unsupported storage engine: InnoDB
140319 11:58:21 [ERROR] Aborting
I also saw the disk full error, but only when running commands without sudo. If the permissions check fails, it reports disk full (even when your partition is not even close to full).
CentOS 7, 64 bit. Fresh installation.
In my case, the error was because I didn't have the right MySQL server and MySQL client installed.
Using yum, I removed mariadb and mysql-community edition. I downloaded the rpm's for the client and server from the official MySQL website and installed the server and client.
On installing the server, I was shown a message that the password to the root account for MySQL was stored in a file which I could view with sudo cat /root/.mysql_secret.
So after installing the client and server, I checked if MySQL was working (I think I rebooted before doing so) with the command sudo service mysql status and I got the result.
MySQL running (2601) [ OK ]
I logged into MySQL using the password from the .mysql_secret file:
mysql -uroot -pdxM01Xfg3DXEPabpf. Note that dxM01Xfg3DXEPabpf is the password mentioned in the .mysql_secret file.
and then typed entered the following command at the mysql prompt to change the password of root:
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('somePassword');
Everything worked fine from then on.
This doesn't directly answer your question but a subset of it, namely using PythonAnywhere. I kept stumbling upon this question when looking for a fix so I'm adding it here in the hope that it will help others in my situation.
PythonAnywhere decided to change the database connection hostnames in order to improve efficiency and reliability, as detailed here:
The official host name you should use for connecting to your account's
MySQL database instance has changed from mysql.server to
yourusername.mysql.pythonanywhere-services.com. This bypasses a part of our infrastructure that has started showing problems in recent
weeks, and it should be much more efficient and reliable than the old
way.
Hence, you will need to update your hostname to the value highlighted above.

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