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
Related
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
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.
<?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());
}
I am trying to use mysqli to insert some data into a MySQL database (let's call the schema myDatabase), but cannot successfully connect. Here's the code snippet to connect:
...
$config = parse_ini_file('../includes/config.ini');
$username = $config['username'];
$password = $config['password'];
$dbname = $config['dbname'];
$server = $config['server'];
$conn = new mysqli($server, $username, $password, $dbname);
if (!$conn || $conn->connect_error) {
die( 'Connection Failed: ('.$conn->connect_errno.') '.$conn->connect_error);
}
...
I get the following result:
Connection Failed: (1045) Access denied for user 'myUser'#'my.laptop.ip.address' (using password: YES)
Here's some details on the set-up, in case they are relevant:
The code is on my laptop running Windows 7 and using PHP 5.3.5 that came with xammpp.
The database is hosted on a remote server with MySQL5.1.52. I created a user to which I granted all privileges on myDatabase.*. No host was specified for the user (e.g. 'myUser'#'%'), as I am still in development and don't know the ip address where the code for the live application will be hosted.
If I ssh onto the database server, I can connect to mysql using the credentials for myUser and access the tables in myDatabase. I have another schema on this same server which is accessed by a different user, and have been able to use mysqli to connect without any problems.
Just to be sure it wasn't a typo, I dropped the user, and created it again, copying and pasting the username and password from the config.ini file used in my php code (and flushed privileges, of course). I did this again, except this time the host was specified, e.g. CREATE USER 'myUser'#'my.laptop.ip.address' IDENTIFIED BY 'myPassword'. I keep getting the same error and now I'm completely stumped.
Help, please.
On your mysql machine hit:
GRANT ALL PRIVILEGES ON database.* TO 'myUser'#'%' IDENTIFIED BY 'newpassword';
FLUSH PRIVILEGES;
This will allow the user to connect from any host. Once it works, you can limit it to just a specific host and database.
Okay, this is strange, but it appears the problem had to do with the password I was using. The original one contained some special characters ($, & +). When I changed it so that it only contained numbers, letters and underscore, it worked.
Is this real, or did I accidentally do something else without realizing that turned out to be the actual solution?
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.