Quick question here, I'm new to web hosting and I'm just having a muck around to see what's what. I've created the following PHP document and hosted it on my free 000webhost domain:
<?php
$mysql_host = "mysqlXX.000webhost.com";
$mysql_database = "***********_people";
$mysql_user = "*************_admin";
$mysql_password = "******************";
// Create connection
$con=mysqli_connect($mysql_host,$mysql_database,$mysql_user,$mysql_password);
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
I have set up (hopefully correctly) a small table in my database with 3 fields:
ID - No Null, auto-increment, integer, primary
First Name - VarChar, Length 20
Last Name - VarChar, Length 20
And entered data for each one.
When I open the file from my control panel, I get the following error:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045):
Access denied for user 'a4935911_people'#'10.1.1.45' (using password:
YES) in /home/a4935911/public_html/MySQL+PHP.php on line 9
AND
Failed to connect to MySQL: Access denied for user
'a4935911_people'#'10.1.1.45' (using password: YES)
Any ideas, anyone?
Wrong argument sequence here:
$con=mysqli_connect($mysql_host,$mysql_database,$mysql_user,$mysql_password);
Look at the message: Access denied for user 'a4935911_people'#'10.1.1.45' (using password: YES) where a4935911_people is not a user, it is your database's name as set above $mysql_database = "***********_people";.
Use: Host, user, password, dbname as described in the Documentation.
Solution:
$con=mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
argument sequence is following order.
$host ="localhost" // change to your host
$username ="root" // change to your mysql user name by default root
$pass =" " //add password if any
mysql_connect($host,$username,$pass) //don't change the sequence
mysql_select("test") // create the test database in mysql
Two possible reasons:
Wrong username/password - check it carefully.
You can't connect remotely, connections are allowed only via localhost. Check your hosting website for such information. If it's not allowed, you can do nothing.
In Addition to the possible conclusions that #ElonThan provides...
it's also possible that your username lacks the proper privileges to the database you are connecting to.
You need to set up mysql to allow remote connection for a particular user in the MySQL database server i.e. mysqlXX.000webhost.com here . The default syntax is:
grant <permission> on <database> to <user>#<location> identified by <password>
So here you have to use(as you have specified the parameters)-
grant all on ***********_people.* to '*************_admin'#'10.1.1.45' identified by '******************'
So for your case run the following command-
grant all on database_name.* to 'username'#'your_ipaddress' identified by 'password'
Run this command in MySQL command prompt.
This will allow username to connect from your IP using that password and give all permissions on all tables in the specified database.
To allow any user to connect from any IP address and use all the tables in any database use the following syntax-
grant all on *.* to '%'#'%' identified by 'password'
And finally you have to use the following command-
FLUSH PRIVILEGES;
To reload all privileges.
For 000webhost.com (Free Hosting Service ) you should Test your code online by uploading it to 000webhost.com server.
That will solve your problem for sure.
Related
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 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.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unable to connect to the database mysql?
I was trying PHP web crawler for website http://astellar.com
I did everything step by step and also create user and database for MySQL. Then connect to database with user as well but it show me an error.
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'switsolu_kll'#'localhost' (using password: YES) in /home/switsolu/public_html/crawler/_db.php on line 43
Cannot connect to database server (Reason: Access denied for user 'switsolu_kll'#'localhost' (using password: YES))
where I am wrong in there?
This error almost always appears when the MySQL password is wrong. Check, double check and then triple check that it is correct.
Often when you created user you did not declare the proper hostname or the request is coming from a host other than 'localhost'. You can create the user for wildcard for all hosts, but sometimes the order of rules messes it up.
delete user and recreate
GRANT ALL PRIVILEGES ON switsolu_aaa.* TO 'switsolu_kll'#'localhost' IDENTIFIED BY 'abc123abc';
don't forget to flush privileges
FLUSH PRIVILEGES;
And depending on your app, you may need to restart it or the server to reset socket connection and retry but shouldn't have to, just retry.
If the localhost doesn't work, then delete user and recreate with 'switsolu_kll'#'%' instead. Don't forget flush privileges; after creating user.
Check User Permission. If you running local, and still have root try:
<?php
$localhost = "localhost";
$mysqlusername = "root";
$mysqlpassword = "";
?>
<?php
mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
This is the code I am using to check if I am able to connect to Mysql.
I am having problem connecting using this code. Should I change localhost to the name of the website?
I tried ("www.abc.com","login username", "password") even this is not working.
EDIT:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'mobile4_you'#'cl79.blahblah.com' (using password: YES) in /home/mobilew4/public_html/mysql.php on line 2 Access denied for user 'mobile4_you'#'cl79.blahblah.com' (using password: YES)
If you're using something like cPanel, it will prefix your username and your database with your username for cpanel, for example:
> cPanel login: foo
> Database User: bar
> Database: ey
Your database user will be foo_bar and your database will be called foo_ey
Localhost refers to where the database is running. If it's running on the same server as your PHP server localhost should work fine. What kind of problems are you having? Are you sure that your username/password is correct?
You should replace :
localhost by the name of the server on which your MySQL server is
if your MySQL server is on the same server as PHP, then it's localhost
username by the login that allows you to access your database
password by the corresponding password
Of course, this is supposing you actually have a MySQL server up and running, with a user account created on it.
Didn't you hosting company provide you with any information related to MySQL ? Using those informations, what error message does mysql_error() give ?
Do you have a mysql user which you've set up? You'll need to do that. In cPanel you can do it by clicking on MySQL databases, and on phpMyAdmin you can just add a new user on the start page. Put that users' username and password in.
You need to pay attention to the error, if any, reported by mysql_error(). This should give you some information you can use to diagnose the connection problem.
See this MySQL Forge wiki page about connection failure:
http://forge.mysql.com/wiki/Error2003-CantConnectToMySQLServer
Or this page from the MySQL Manual:
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html