When I try to connect to a website database using the code:
$server = "domain";
$login = "username";
$password = "password";
$database = "databasename";
$port = 3307;
$con = mysqli_connect($server, $login, $password, $database, $port);
and have all warnings and errors enabled I get a message saying
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on 'domain' (111) in 'path'
however when I change $server to localhost it works fine.
If it is blocking me through php unless I use localhost does anyone know why the HeidiSQL or MySQL Administrator clients can connect using the domain?
What I understand from your problem statement, you are unable to connect to MySQL server using domain or IP address.
To resolve it, first you need to give database access permission to user. Checkout below sql which will do work for you.
GRANT ALL PRIVILEGES ON `databasename`.* TO 'username'#'domain' IDENTIFIED BY 'password' WITH GRANT OPTION;
Once permissions are granted to user check your MySQL configuration settings. Edit your MySQL confifuration file my.cnf and set nbelow mentioned settings.
Set the bind-address to 0.0.0.0 or comment it out:
bind-address = 0.0.0.0
OR
# bind-address = 127.0.0.1
Related
We have a dedicated server with proxmox on OVH. The idea is to connect the containers locally but also trough internet. So far I have 2 containers.
I added a bridge network for local IP and that is working since I am able to ping the containers from each other.
Also added bind-address=192.168.1.3 to my.cnf.
1 container is running apache + php 7.2 (192.168.1.3)
The other container is running MySQL. (192.168.1.2)
Problem
My MySQL keeps saying SQLSTATE[HY000] [2002] Connection timed out
Here is my php code:
<?php
/**
* Configuration for database connection
*
*/
$host = "192.168.1.2";
$username = "root";
$password = "root";
$dbname = "test";
$dsn = "mysql:host=$host;dbname=$dbname";
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try
{
$connection = new PDO("mysql:host=$host", $username, $password, $options);
$sql = file_get_contents("data/init.sql");
$connection->exec($sql);
echo "Database and table users created successfully.";
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
From my understanding the code is correct so it must be something with my mysql configuration.
I'm sure that is something really simple but I'm losing to much time with this.
Try to telnet 192.168.1.2 3306 from the Apache, PHP container. Can you connect?
Ensure the listening port for MySQL is 3306, if other then adjust the PHP code accordingly. Also ensure that iptables is not blocking any incoming connections. Also ensure you have correct permissions for the root and any other users you need to have permissions from other hosts.
Also, please check when making any config changes to MySQL, that you restart the service.
After searching around for a while. Found out that I had to create a new user and grant permissions to it
mysql> CREATE USER 'monty'#'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
-> WITH GRANT OPTION;
mysql> CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
-> WITH GRANT OPTION;
Also on my server config /etc/mysql/mariadb.conf.d/50-server.conf I had to comment the
bind-address = 192.168.1.3
into
#bind-address = 192.168.1.3
Then restart mysql server and change my mysql details on my php code with username 'monty' and password 'some_pass'
This question already has answers here:
MYSQL Access denied for user 'root'#'localhost'
(3 answers)
Closed 5 years ago.
My php page and mysql server are in same server.
I can connect database using local IP , it works.
but, when trying to connect with localhost it show the following error
Connection failed: Access denied for user 'root'#'localhost'
changed mysqld.cnf file
as BIND ADDRESS = 0.0.0.0
I gave the following permission
GRANT ALL PRIVILEGES ON . TO root#localhost IDENTIFIED BY 'password' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON . TO root#my local IP IDENTIFIED BY 'password' WITH GRANT OPTION;
I don't know what is problem in my code and configuration ...
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database="db";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error){
die("Connection failed: " . mysqli_connect_error());
} else {
echo 'connected successfully';die;
}
mysqli_close($conn);
?>
Have you tried servername 127.0.0.1?
From the mysql manual:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server.1
When I run the following from my webserver it runs fine:
$ip = "localhost";
$uname = user
$pw = user password
$tb = table name
$dbconn = mysqli_connect($ip, $uname, $pw, $tb) or die("Couldn't connect");
However, when I make the following change, I get the "Couldn't connect" error:
$ip = "X.X.X.X";
Where X is the Public IP of my web server. Even when I change it to:
$ip = "127.0.0.1";
I get the couldn't connect error.
Can anybody think of a reason this would be refusing the connection?
Thanks
EDIT:
I have looked on the server logs and get the following (when I do 127.0.0.1):
[26-Nov-2015 23:51:38 Europe/Moscow] PHP Warning: mysqli_connect(): (28000/1045): Access denied for user '*USERNAME*'#'127.0.0.1' (using password: YES) in /filepath/
Where 'USERNAME' is my db username in the correct format, (cpname_dbuser)
If you are trying to access Mysql from remote location then you have to enable Remote Mysql or Whiltelist your ip
Use mysqli_connect_error() to return last connection error
You need to grant privileges from the IP address of your webserver, open port 3306 on the firewall to allow the IP you are connecting from access to the database and you will need to bind MySQL to the IP address you are connecting to. You can do this in the MySQL configuration file (search for bind-address).
To grant privileges you can log in as MySQL root and type the following...
grant all privileges on <database>.* to <username>#<ip address> identified by 'yourpassword'
Where ip address is the address of the machine you are accessing the database from.
I have some troubles to connect to a remote mysql server.
This is the situation :
I have 1 MySql server which is working (Installed phpMyAdmin, and can do actions on it).
I have 1 webserver
I try to access to my mysql server with the mysql_connect(); function :
$host = 'my_ip:ext_port';
$user = 'leUser';
$pass = 'lePassword';
$mydb = 'leDb';
$db = mysql_connect($host, $user, $pass);
Where ext_port is the port I open in my MySql server and which redirect to the port 3306.
I also give to my user the access with :
grant all privileges on *.* to 'leUser'#'%' IDENTIFIED BY PASSWORD '*lePasswordEncrypted' WITH GRANT OPTION
Tried a lot of things in vain (As comment the line bind-address in my.cnf, etc ...) but still get the error :
Can't connect to MySQL server on 'xxx.xxx.xx.xx' (4)
the script of connection =
<?
$server = "192.168.0.167";
$username = "root";
$password = "";
$database = "dbbook";
mysql_connect($server,$username,$password) or die("Koneksi gagal");
mysql_select_db($database) or die("Database tidak bisa dibuka");
?>
and i had add this script on my.cnf file after [mysqld] =
bind-address=192.168.0.167
but it didnt work with the following caption
mycomputer.mshome.net is not allowed to connect to this Mysql server
Please help me.How to remote database mysql XAMPP from another computer ?
You will have to create a new user in MySQL that is allowed to connect from a remote host.
By default, root can only connect from localhost.
You could try running the following commands from the MySQL server (make sure to substitute 192.168.0.99 with the IP or hostname of the PC that will be connecting) In your case, try 'php'#'mycomputer.mshome.net' for the user:
CREATE USER 'php'#'192.168.0.99' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON dbbook.* TO 'php'#'192.168.0.99';
It is possible to specify a wildcard host ('php'#'%'), but then if anyone got your MySQL password they could connect to the DB. You could also wildcard your subnet ('php'#'192.168.0.%') which is a little safer.