Can't connect remote MySql server - php

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)

Related

Use local network for DB connection

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'

MYSQL Connection not allowing IP Address

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.

How to connect to sql server to PHP for remote connection

I have use this code but not connect to sql serve for remote connection and error is fire.I get this error:
Couldn't connect to SQL Server on XX.XXX.XX.XX
code:
<?php
$myServer = "XX.XXX.XX.XX";
$myUser = "New";
$myPass = "123";
$myDB = "Live";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB"); ?>
What does this error message?
First try to telnet port on remote server, by default it's 3306.
If you can't access this port it's probably not allowed on firewall or the mysql server isn't listening to remote connections.
Then check mysql settings on server, by default it's accessible only from localhost. On linux servers it's usually in /etc/mysql/my.cnf
just comment this line:
bind-address = 127.0.0.1
Then check authorization of the user that you use for a login, note that you need both authorization to log in from where you are now and authorization to log in on the server (as localhost) as well..
Example from http://dev.mysql.com/doc/refman/5.1/en/adding-users.html :
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;
creates a user with username "monty" and password "some_pass" with all privileges on all tables in all databases (*.*)
from anywhere(localhost and % - as any ip).

Can't connect to MySQL server with php but can through HeidiSQL

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

Creating a Secure Connection to a Server using PHP

I am trying to query a database that's on a server that I have a Remote Desktop connection to. What is required to be able to query a database on this server using my php web application.
You need:
Database server application configured to allow connections from external hosts
Server-side network configuration tuned to allow connections from external hosts (firewall, NAT, ...)
Database user, which is granted access to database you are going to use
PHP application, which connects to your database server under appropriate user
Details depend on what database server are you using.
$link = mysql_connect($host, $username, $password);
There is nothing secure at all by the way.
I use something like this, but it's far from secure. I keep it in a seperate "connection.php" that are required once by other files.
$db_host = 'INSERT IP HERE';
$db_user = 'User';
//My sql password for testing purposes
$db_pass = 'PASSWORD';
//name of the database
$db_name = 'dbname';
//connection
$conn = new mysqli($db_host, $db_user, $db_pass ,$db_name);
if (mysqli_connect_errno())
{
printf("Error connecting to DB, Errorcode: %s\n", mysqli_connect_error());
exit;
}
use answer by Merch member - the php code above .
You need to grant privileges and to create user on server you are retrieving the database data from.
Connect to mysql :
in shell type (if you are root type root or whatever username):
mysql -u root -p
enter password,
when you are logged in :
create user someusername#your-client-ip identified by 'password';
grant all privileges on *.* to someusername#your-client-ip with grant option;
now you can use php code above to connect to remote server database (ip you just used to create mysql user your-client-ip)
When you are creating php variable for mysql connect - host use mysql port in the variable if just ip not connects your-client-ip:3306
good luck

Categories