Connecting to Remote Server MySQL Issue - php

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

Related

Ubuntu 20.4 Mysql access from remote IP with PHP issue

I want to access to MySQL db on an Ubuntu Server from another Web server.
I get my web server outgoing IP with this PHP script running on the Web server:
$realIP = file_get_contents("http://ipecho.net/plain");
echo "Real IP: $realIP<br>";
I opened the ufw port on the Mysql Server:
sudo ufw allow from realIP_result to any port 3306
I change the Mysql configuration file to accept remote connections:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
and then edit updated the bind-address to 0.0.0.0:
bind-address = 0.0.0.0
I create the user on MySQL Server:
CREATE USER 'sammy'#'remote_server_ip' IDENTIFIED WITH mysql_native_password BY 'password';
I created in this way, but I also tried without the mysql_native_password.
I grant all to that user:
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'#'remote_server_ip' WITH GRANT OPTION;
I flush privileges:
FLUSH PRIVILEGES;
And when I try to connect from the web server with this PHP script:
$conn2=mysqli_connect("MYSQLIP","sammy","password","mydbname");
// Check connection
if ($error = mysqli_connect_error($conn2)) {
echo "Failed to connect to MySQL: " . $error;
exit();
}
I get this error:
Warning: mysqli_connect(): (HY000/2002): Connection timed out in /customers/e/9/2/mysite/myscript.php on line XX Failed to connect to MySQL: Connection timed out
How can I resolve, or debug further?

SQLSTATE[HY000] [1698]

How can I overcome this error ?
Error: SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost' (SQL: select count(*) as aggregate from users
where email = email#gmail.com
I'm using Ubuntu
Please help
Login as root first:
$ sudo mysql -u root
Then CREATE or ALTER a non-root user (use '127.0.0.1' instead of 'localhost' if needed):
CREATE USER 'admin'#'localhost' IDENTIFIED BY 'adminspassword';
GRANT ALL PRIVILEGES ON *.* TO 'admin'#'localhost';
FLUSH PRIVILEGES;
Exit and restart:
exit
$sudo service mysql restart
$sudo service apache2 restart
And edit the .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3630
DB_DATABASE=yourdbname
DB_USERNAME=admin
DB_PASSWORD=adminspassword
MySQL will make a difference between "localhost" and "127.0.0.1".
It might be possible that 'root'#'localhost' is not allowed because there is an entry in the user table that will only allow root login from 127.0.0.1.
This could also explain why some application on your server can connect to the database and some not because there are different ways of connecting to the database. And you currently do not allow it through "localhost".
I know its late however looking for answers, I couldn't find anything and at last I got this answer.
$sudo mysql -u root
[mysql] use mysql;
[mysql] update user set plugin='' where User='root';
[mysql] flush privileges;
[mysql] \q
Now you should be able to log in as root.
Thanks #Matematikisto in this thread
I encountered this problem in MySQL 8, Ubuntu 20. By default, the policy does not grant "GRANT" rights, including for root, but even after manipulations to obtain them, the application was able to gain access only after granting the mysql_native_password rights to the account. Maybe it will help someone:
UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('password'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
the full sequence of steps to change the password and its format is here:
MySQL: How to reset or change the MySQL root password?

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.

(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.

Not able to connect to mysql via php script , but can login to phpmyadmin in LAMP UBUNTU

I am trying to connect to mysql using PHP in ubuntu LAMP. I can login to phpmyadmin , but not able to connect using php code. The code was working well in XAMPP .
I am using the following code
$con=mysql_connect("127.0.0.1","myusername","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
But it shows the following error
Could not connect: Access denied for user 'root'#'localhost' (using password: YES)
There is 3 (that i know of) posible solutions to this depending on the problem.
There is a firewall in ubuntu that you need to open, you do this by granting access in IPtables.
/sbin/iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT
And now we should be able to login to our server from our local machine:
mysql -h255.112.324.12 -uroot -pMyPASSWORD
You need to grant access to mysql: https://askubuntu.com/questions/159053/mysql-server-not-accessible-from-remote-machine
as root, open your /etc/mysql/my.cnf with your favorite editor
look for the [mysqld] section, and in there for the bind-address keyword. This usually is set to 127.0.0.1 -- change that to match your "normal" IP-address
save the file, and reload the service (e.g. using service mysql restart)
Last but not least you need to give remote access in mysql:
GRANT ALL ON mydb.* TO root#'%' IDENTIFIED BY 'MyPASSWORD';
Source: http://web.archive.org/web/20120930214828/http://chosencollective.com/technology/how-to-enable-remote-access-to-mysql + Many sleepless nights.

Categories