Unable to connect mysql database from php - php

<?php
$servername = "192.168.179.130";
$username = "root";
$password = "";
$dbname = "rawcdr";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
The above code gives a warning Warning: mysqli::mysqli(): (HY000/2002) A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\new_table\index.php on line 59.
The host is a different server (CentOS 7), and i have checked its reach ability.
I have also provided special privilege in mysql server by:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'192.168.%' IDENTIFIED BY PASSWORD '';
Also started the firewall service by:
service firewalld start
Checked the port 3306 is open by following command:
netstat -nlp | grep 3306

Of course the first thing to check is that the firewall on the remote machine is not blocking access on port 3306.
But:
The root user account on a MYSQL Server is by default configured so that this account can only be used from the machine running the MySQL Server instance. So by default you cannot use root to access the server remotely.
And that is as it should be. It is afterall the SuperUser account.
EG
`root`#`localhost`
So you are going to have to create a new user on the remote MYSQL Server instance that is allowed to connect from either any remote host dangerous or a specific remote host (your PC's ip address). This new account should also be setup to be allowed access with the required privilages on the database you are trying to access.
CREATE USER 'ayax'#'192.168.1.200' IDENTIFIED BY 'new_password'
Or if its 5.7 server dont forget the new password expire
CREATE USER 'ayax'#'your-ip-address' IDENTIFIED BY 'new_password'
PASSWORD EXPIRE NEVER;
If your ip address is allocated by a DHCP server and can change from day to day something like this would be more flexible
CREATE USER 'ayax'#'192.168.1.%' IDENTIFIED BY 'new_password'

Try this :
$conn = mysqli_connect('192.168.179.130', 'my_user', 'my_password', 'my_db');
if (!$conn) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}

Related

what is error when connecting database with localhost and i can connect database with host as my local IP address? [duplicate]

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

remote access to mysql on Ec2

I have two amazon ec2 instances. One I am using as a webserver and the other one has the mysql DB. I have the below php script that I run from the web Server. I cant seem to connect to my Database. When I run the php script on the mysql server with $host= localhost, it works. How can I access the mysql server?
<?php
$host="ipaddress"; // Host name
$username="root"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="myDB"; // Database name
$port=3306;
// Create connection
$conn = new mysqli($host, $username, $password, $db_name,$port);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else
{
echo "Connected";
}
?>
Have the user/host combo as a legitimate user in mysql.user
select user,host from mysql.user;
Have the aws ec2 security group open (the aws Firewall)
There are three choices here: my IP, custom, or anywhere for the ip addr CIDR
Have the mysql server configured to allow remote connections.
This is the bind-address and skip-networking setup. See the nixcraft document for those few changes if needed.

Having difficulties connecting to Amazon RDS MySql server with PHP

I'm just trying a basic connection with a PHP script and running into some issues, my PHP code is simply:
$host="endpoint:3306"; // Host name
$username="user"; // Mysql username
$password="pw"; // Mysql password
$db_name="RawData"; // Database name
$conn = new mysqli($host, $username, $password, $db_name, 3306);
//Check connection
if($conn->connect_errno > 0){
die('Unable to connect to database [' . $conn->connect_error . ']');
}
And getting an error Access denied for user 'user'#'cpe-174-109-76-241.nc.res.rr.com.' If it is to the point that is is an Access Denied error, is that based on the password? My password works because I can connect to the instance with MySQL Workbench on this same machine. Do I need to make an update to my security group for the PHP code to connect through localhost? Let me know if there is any more information I need to provide, I'm very new to PHP and very lost with this basic setup. Thanks!
You may have a security group preventing access.
Examine the allowable IP addr , adding yours as a test. Consider adding 0.0.0.0 and CIDR in the security group at play with port 3306
http://whatismyipaddress.com if needed

Connect to remote MySql server with XAMPP

I have an script that i want it to run for more that 5 minutes. But my current hosting service does not allow me to modify the max_time_limit in the php.ini (That is set to 2 minutes)...
So i thought that i could run the script with XAMPP and send the data to my database but i cant connect to the remote database.
This is the code for the connection:
$servername = "217.70.186.108"; //I've also tried with the name of the webpage (metagame.gg)
$username = "the_username"; //the username is not root since I've read that root can only connect from localhost. This user has all privileges. This user was created with the permision to be connected from any server (%)
$password = "the_password";
$dbname = "the_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
It displays this error:
Connection failed: An error occurred during the attempt to connect because the connected party did not properly responded after a period of time, or failed in the established connection because connected host has failed to respond.
Any help would be highly appreciated

How to access information from a SQL database to another server?

I have two servers, for the sake for my questions, lets name the first server A, and the second server B. Server A is a magento server that has a lot of user data, and server B is a basic web page.
I want to create an analytics dashboard in server B, that uses the SQL databases of server A.
Now the problem is having a remote connection from server B to server A.
I try to connect to server A, using this code in server B:
$servername = "104.2*****"; // Server A
$username = "root";
$password = "******";
$dbname = "magento";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
I get this error:
Connection failed: Can't connect to MySQL server on '104.2******'
Can someone please tell me what I'm doing wrong?
You may need to create a ssh tunnel to connect to a remote server hosted database if the settings on the database server do now allow unsecure connections which is likely why you're getting that error.
http://kb.mediatemple.net/questions/133/Tunnel+local+MySQL+server+through+SSH#gs
It might be that there is a firewall restriction on the server you want to connect to. The MySQL port connection might be restricted.
Try to connect from the server you run this script (or another server) to the ip/port of the server you want to connect to.
If you have access to a linux/windows console, try running:
telnet 104.2***** 3306
3306 is the default port for MySQL and telnet should work.

Categories