I want to run PHP script in local system (localhost) and the data to be stored in the server database. I'm getting an error while connecting to remote mysql database from localhost through PHP script.
Warning: mysql_connect(): Access denied for user 'XXXX'#'ip address' (using password: YES) in E:\xampp\htdocs\New\example\include\config.php on line 13
I tried using
$con= mysql_connect("example.com:3306","db username","password");
mysql_select_db('db', $con);
I tried using mysqli_connect(...) also but I couldn't connet.
somebody please guide me how can I resolve this?
If you are running it on Localhost then do it like this
$connection = mysqli_connect('localhost','username', 'password', 'database');
When you use mysqli you don't need to use the mysql_select_db as this information is passed in when you create the connection.
The main fix that I will stress here is using your first credential passed into the connect variable as 'Localhost' if its local on your machine and you are using xampp or mamp etc then use localhost.
this syntax that you have done mysql_select_db('db', '$con') is wrong when using mysql you DO NOT need to pass a $connection variable into mysql as this only applies for the new mysqli.
Last word of advice as soon as your comfortable (preferably asap) move away from mysql functions and use mysqli, the move is not too different you just have to learn where to pass in the $conn variables.
Seams like you have not granted privileges to user.
Try this on your remote database.
~$ mysql -u root -p
Enter Password:
mysql> grant all privileges on *.* to user identified by 'pass' with grant option;
Oh, check out Kenziiee Flavius answer first - that might already solve your problem as you are on localhost.
Log in to your Server via commandline:
mysql -u username -p
Create a new user for your remote host:
CREATE USER 'username'#'192.168.0.1' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'#'192.168.0.1';
Replace 192.168.0.1 with the ip or hostname of your remote host.
Related
I am attempting to connect to MySQL server on my personal computer (Ubuntu 18.04.1 LTS) from a web browser, using url: http://localhost/Test.php
In Test.php I have the following code:
$pdo = new \PDO("mysql:host=localhost;dbname=mydatabase", "root", "password");
I can connect successfully using command line mysql -u root -p (and use USE, SELECT, UPDATE...). Mysql service is running.
Any idea of what's wrong?
Solved. I just created a new user, example:
CREATE USER 'select_user'#'localhost' IDENTIFIED BY 'password';
GRANT SELECT ON *.* TO 'select_user'#'localhost';
And changed user "root" to "select_user" in my php code.
Using user "root" was the problem.
I am trying to connect to an external server on a fresh MySQL install, but it just seems to timeout. Access has been granted by the other servers admin to the particular account and ip address. I try running the following:
mysql --host=hostname -u username -ppassword database
But just get the following:
ERROR 2003 (HY000): Can't connect to MySQL server on 'hostname' (110)
I have adjusted the following in my /etc/mysql/my.cnf file:
bind-address = 0.0.0.0
and restarted, but it didn't help. Any recommendations?
If you have checked that the firewall is letting MySQL traffic (usually port 3306) pass through and the service is running, the most probable cause is fine grained permission on the server. The user may not have been allowed to connect from any host, probably the permissions to connect are IP based or something similar.
I would try to use that code but not providing the password, like:
mysql -h hostname -u username -p database
and intert the password after that command (it will ask for it).
Also you could try to provide a port: --port=3306 for example.
You can also try to provide the protocol --protocol=TCP.
The only last idea that i have is that you dont provide the database and enter the database after you access the server...
sorry, can't do anything else from here with that data.
Try to grant the user to access database from the remote computer.
to do that:
mysql> GRANT all on database.* to 'username'#'%' IDENTIFIED BY 'user_password';
this is not recomended becouse this will allow connection to your mysql server from anywhere.
for allow connection to user in a certain ip address (we use 8.8.8.8 as example) use code below
mysql> GRANT all on database.* to 'username'#'8.8.8.8' IDENTIFIED BY 'user_password';
The problem lies in your permisions granted to your user. try changing permisions and hope it will help you .
I've tried to search for an existing answer to this problem, but the answers I find have not worked thus far.
I've been attempting to use PHP to connect to a MySql database. My web host uses cPanel on Linux. The code I'm using to do this seems standard enough:
$mysqli = new mysqli("localhost", "cPanelUsername_dbUsername", "dbPassword", "cPanelUsername_dbName");
I've been getting the following error:
Failed to connect to MySQL: (1045) Access denied for user 'cPanelUsername_dbUsername'#'localhost' (using password: YES)Access denied for user 'cPanelUsername'#'localhost' (using password: NO)
"localhost" is the host server where the MySql server is located (it seems like this works)
"cPanelUsername" is my cpanel username
"dbUsername" is the database user, which I added to the database with all permissions granted
"dbPassword" is the database password for dbUsername
"dbName" is the database name
I ended up adding my cPanel username before the dbName and dbUsername after searching for answers to this issue elsewhere.
It looks like I have everything set up correctly but it's not connecting (with the error above). I don't have any direct control over the server that I wouldn't have to ask my web host about, which may take a few days to get sorted out. Do I have something wrong with my connection code?
First check the database that you gave the proper user access to your database, which is given from Add User to databases from Mysql database section in cpanel.
after that check it again,
first try normal connection code in php,
$con = mysql_connect("localhost","cpanel_username","cpanel_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
In cPanel, make sure that:
The database user cPanelUsername_dbName exists, with the password dbPassword
The database you want to use exists.
The user cPanelUsername_dbName is allowed to access the database.
The user cPanelUsername_dbName is allowed to access the database from localhost, 127.0.0.1, and the IP address of your server.
Your MySQL connections may use 127.0.0.1 or the IP address of your server, and MySQL will reject the connection if access isn't granted for the specific IP address used.
check the database name spelling at your phpMyAdmin. Usually the name is in format user_dbname.
For example:
cpanel username: jack,
database created: student
In your php script, the dbname should be jack_student
This worked for me:
Depending on what MySQL version you have, make sure you have matching password and hostname on your PHP file, config.inc.php file.
If you need to change the password for MySQL 5.7.6 and later:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
If you are trying to access your localhost server from a different machine i.e. (emulator, another computer), make sure you are using the actual IP address of the the localhost, DO NOT USE localhost as the hostname. Because this is like telling the machine to connect to itself - but the server is on a different IP address.
I've Installed mysql, php, apache using a wamp configuration
Where to access localhost is would be http://localhost:81
the PHP script I'm using is
if(mysql_connect('localhost', 'root', 'exobytes15')) {
mysql_select_db('testDB');
}
else {
echo 'Could not Connect to the database';
}
But this gives me the error
1045: Access denied for user 'root'#'localhost' (using password: YES)
What should I do to fix this problem?
Either you didn't grant root#localhost the necessary rights to access the database or you're providing the wrong password.
Note : granting access to root#`%` does NOT grant access to root#localhost...
Make sure you have that your database is running, and that the user has access to it.
You can use either use the plain old mysql commandline client for this or phpmyadmin if your wamp stack has that installed.
You're connecting with the wrong password: Reset the password
or more likely: you don't have privileges to connect from localhost.
Run the command:
GRANT ALL PRIVILEGES ON *.* TO 'root'#localhost WITH GRANT OPTION;
Test to see if you can connect.
Never do: GRANT ALL PRIVILEGES ON *.* TO 'root'#% WITH GRANT OPTION;
Because that will put your database server at risk from remote login attempts.
Maybe u are running mysql on a port other than the default port.
If it is the case use this:
mysql_connect('localhost:PORT', 'root', 'exobytes15')
I have set up an Apache server on mandriva linux 2010.1. But the problem is whenever I'm trying to connect with the database, it's giving me the following error:
Error:Database error: SQLSTATE[28000]
[1045] Access denied for user
'root'#'myhostname' (using password:
YES)
Normally for a web application, you shouldn't connect to the database as root. However you tagged your post as [phpmyadmin] so I assume your issue is with, well, phpMyAdmin, in which case you might be connecting as root.
If this is the case, I see that you mentioned myhostname in your error message. Have you tried connecting to localhost instead? Sometimes the MySQL root user cannot connect from remote hosts.
you need to set some configuration variables for phpmyadmin to work:
http://www.phpmyadmin.net/documentation/Documentation.html#config
GRANT ALL PRIVILEGES ON *.* TO monty#localhost
IDENTIFIED BY 'indian' WITH GRANT OPTION;
Replace your username and password and execute it in your phpmyadmin by selecting your database.
Take a look at "5.4.7. Causes of Access-Denied Errors" in the MySQL online documentation and Using authentication modes" in the PHPMyAdmin documentation.