I'm presently reading the book "Head First jQuery" and came across this bit of code :
mysql_connect('127.0.0.1', 'runner_db_user', 'runner_db_password')
OR die( 'Could not connect to database.');
mysql_select_db('race_info');
But since mysql_connect is deprecated, I decided to use mysqli_connect via
mysqli_connect('127.0.0.1','runner_db_user','runner_db_password','race_info')
or die('Error connecting to Database');
However I get an error saying
Warning: mysqli_connect(): (HY000/1044): Access denied for user 'runner_db_user'#'localhost' to database 'race_info' in E:\wamp\www\Z_jquery\ch9\service.php on line 2
I created the user 'runner_db_user'#'localhost' and a database for it to use, using:
create database race_info;
CREATE USER 'runner_db_user'#'localhost' IDENTIFIED BY 'runner_db_password';
GRANT SELECT,INSERT,UPDATE,DELETE ON race_info.* TO 'runner_db_user'#'localhost';
I've confirmed using phpMyAdmin that both the user exists and it has the privileges granted by the above commands. Why then am I not able to connect to the database?
NOTE : I also was able to manually connect to the database using said login credentials via command line:
mysql -u runner_db_user -p
Why am I then unable to connect to the database via mysqli_connect()? Are there any further permission that I need to provide to the user to connect successfully?
EDIT : This is the present condition of my users. Are there any anonymous users that may cause this problem, according to the best answer here? If so, how do I delete these users?
Related
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'm trying so hard to make connection to mysql, and so far I end up with this error :
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'dev'#'localhost' (using password: YES) in D:\Programs\Xamp\htdocs\series\dynamic\AtomCMS\setup.php on line 5
Could not connect because: Access denied for user 'dev'#'localhost' (using password: YES)
I've just started developing a new web based system and here is the code I used to make connection to the database. Any help is appreciated.
$dbc = mysqli_connect('localhost','dev','123','pouya')OR die('Could not connect because: '.mysqli_connect_error());
Sincerely,
In case you are using XAMPP then open the phpmyadmin interface using [localhost/phpmyadmin]
1) On selecting the database you want to access.You get an option on right having a tab called privileges.
2) You need to check whether the user exist.If not Create a user dev.If the user exist try to recreate as you may be using a wrong password.
You need to make sure that the dev user exists, and that they are granted access rights to pouya.
I did read this tutorial
https://developers.google.com/appengine/docs/php/cloud-sql/
I had created a Instance with name schoolnbd and my project id is myapplication-2013
After reading the tutorial I tried the code below
<?php
$dhost=":/cloudsql/myapplication-2013:schoolnbd";
$duser="root";
$dpassword="xxxyyy";
$database="schoolnbd";
$connection=mysql_connect($dhost, $duser, $dpassword) or die("Could not Connect to SQL Server Suleman");
$db=mysql_select_db($database, $connection) or die(" Check the Database Name from Config.php , wrong database entered ");
?>
But it is showing “Could not Connect to SQL Server Suleman”.
I had created one user by name suleman and made several changes , but still it is not connecting to SQL server, i am seeing same message.
I had tried doing many changes such as
try 1
$dhost=":/cloudsql/myapplication-2013:schoolnbd";
$duser="suleman";
try 2
$dhost="localhost";
$duser="root";
try 3
$dhost=":/myapplication-2013:schoolnbd";
$duser="root";
try 4
$dhost=":/cloudsql/myapplication-2013:myapplication-2013:schoolnbd";
$duser="root";
try 5
$dhost=":/cloudsql/myapplication-2013:myapplication-2013:schoolnbd";
$duser="suleman";
Error from the error log is
it is Showing mysql_connect(): Access denied for user 'root'#'localhost' (using password: YES) in /base/data/home/apps/s~myapplication-2013/1.371632998623546850/test.php on line 6
when I am using
$dhost=":/cloudsql/myapplication-2013:schoolnbd";
$duser="root";
i also faced the same problem, try without using password, it like this it will most probably work
$dhost=":/cloudsql/myapplication-2013:schoolnbd";
$duser="root";
$dpassword="";
but keep in mind , if you had set your root password then you should update root password, by typing this in console.
SET PASSWORD FOR 'root' = PASSWORD('');
If you are connecting using the root user, from AppEngine, you should not provide a password.
how to connect cpanel db from Localhost.
i know connect from localhost. But i need to connect from server.
i am trying like this
<?php
mysql_connect("208.91.199.141","username","password") or die(mysql_error());
mysql_select_db("db");
?>
i get an Error
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'glansade'#'49.206.54.187' (using password: YES) in C:\xampp\htdocs\calendar\db_server.php on line 2
Access denied for user 'glansade'#'49.206.54.187' (using password: YES).
How to solve this....? Please Help me. Thanks
Go to RemoteMysql from cpanel
you have to add %.%.
Then you will be able to connect from remote Db.
You will not want to add %.% to Remote Database Access Hosts as it is a security concern. The %.% is a wildcard and using that wildcard in access hosts like that will expose mysql to the internet and the possibility of unauthorized database access is greatly increased.
Based on your error, the IP you are connecting from is 49.206.54.187. You will want to use this IP address in Remote Database Access Hosts. If you do not know the IP address to use, you can check your IP at http://cpanel.net/myip
I also see that you are connecting with your cPanel account username. While this should work, it is the preference of many to setup a separate account for each Database. To accomplish this, follow these instructions.
In cPanel, select MySQL Databases
In the Databases section, under "MySQL Users", create a New MySQL user.
After creating that user, add that user to the Database in the section "Add User To Database"
Make sure to select all privileges.
Thanks!
I'm not an expert, but I think you should try :
mysql_connect("localhost","username","password") or die(mysql_error());
Instead of :
mysql_connect("208.91.199.141","username","password") or die(mysql_error());
<?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