Use local network for DB connection - php

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'

Related

Connecting database in another server

For a web project we have been provided two servers, one for web and another for database. When i try to connecting to the database in another server i get "Cannot connect permission denied" error. I wanted to know if i have to change any settings or modify any files like php.ini or cnf etc for it to work? I am running php 7.2 on rhel 7.5 servers. Also i changed root to allow all hosts(%) in users.
$db_host = '172.xx.xx.xx';
$db_user = 'root';
$db_pass = 'passwrd';
$db_database = 'some_db';
/* End config */
$db_conx = new mysqli($db_host, $db_user, $db_pass, $db_database);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
You need to ensure that the "root" user has admin privileges or try creating a new user and grant it full admin right privileges.
% mysql --user=root mysql
CREATE USER 'youruser'#'yourserver' IDENTIFIED BY 'yourpass';
GRANT ALL PRIVILEGES ON *.* TO 'youruser'#'yourserver' WITH GRANT OPTION;
FLUSH PRIVILEGES;
By default you cannot access mysql from remote ip address. First of all, you need to grant all access to root,
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Then you need to unbind the ip address part in mysql ini file, which should look like bind-address = 127.0.0.1 , either delete it or comment it by #bind-address = 127.0.0.1.
After changing all these, restart mysql service to take effect of changes.
Note: THe above location is for windows environment, if you are in linux, then likely location may be /etc/mysql/my.cnf, some cases it may be in /etc/mysql/mysql.conf.d/mysqld.cnf.

Can't connect remote MySql server

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)

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

Giving permissions to user doesn’t work in MySQL

I executed this in phpMyAdmin.
I executed these lines:
CREATE USER root IDENTIFIED BY PASSWORD '';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION;
I get this when trying to connect:
Connect failed: SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)
$dsn = 'mysql:dbname=one';
$user = 'root';
$password = '';
$pdo = "Not set";
try{
$pdo=new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch(Exception $ex){
echo "Connect failed: " . $ex->getMessage();
}
Note that the connection above used to work..now it doesn’t (since I switched to XAMPP).
Are you sure that the root user doesn't have a password? Do you have PhpMyAdmin installed to check this?
If the problem isn't the privileges, then it's probably the port. The default port of MySQL is 3306, so if your port for MySQL is now 82 that you have to add the port in the connection.
Please, check create user specification, seems you should use another syntax for account name
Syntax for account names is 'user_name'#'host_name'.
So, something like CREATE USER 'root'#'localhost' IDENTIFIED BY PASSWORD '';.
In your case CREATE USER 'root'#'%'; but I really did not recommend use '%' for root user.
Are you flushing privileges after altering permissions?
You should execute FLUSH PRIVILEGES; to tell the server to reload the grant tables.
The host % does not apply to localhost. You need to grand permissions for localhost separately.

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