Access denied for user ''#'localhost' to database 'database' [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - connecting to mysql database from different server
global $host, $username, $password, $database;
$host = "meshtelecom.com";
$username = "mesh_project";
$password = "password";
$database = "mesh_database";
$link = mysql_connect($host, $username, $password);
if(!$link)echo "Connection failed.";
$db = mysql_select_db($database)or die(mysql_error());
I made a database with phpmyadmin in meshtelecom.com. I can't access this database from another server.. it's giving me:
Connection failed.Access denied for user ''#'localhost' to database 'mesh_database'

Maybe your host must set to localhost, you can check this in your web panel of your hoster.

You have to create a new account for the user for accessing the host from other servers.
You currently have user 'mesh_project'#'localhost' but you also need to have 'mesh_project'#'%'.
Or if you want to restrict which hosts can be used to connect to the database, you can use hostname or IP instead of %.
Here's some documentation http://dev.mysql.com/doc/refman/5.5/en//adding-users.html
If you can log in from command line, run these commands:
CREATE USER 'mesh_project'#'%' IDENTIFIED BY 'password';
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON mesh_database.* TO 'mesh_project'#'%';
Change 'password' to something else and add only privileges you want the user to be able to perform. Giving too much power to be accessible from any host increases security risk.

Double check your host, in most cases websites and databases are on the same machine try localhost or ask your host provider for the database connection info.

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

Cannot connect to MySQL database on remote server using php mysqli

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?

How do I properly connect to a mysql database?

Here is my code:
<?php
require_once('config.php');
$link = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die ('Your DB connection is misconfigured. Enter the correct values and try again.');
?>
I have stored my host, username, password and database name in the separate file config.php. I know that the information is correct because I can connect to my database via putty, but I keep getting the error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user
'blank'#'T9AF3.WPA.blank.Ca' (using password: YES) in
C:\xampp\htdocs\temp2\index.php on line 3 Your DB connection is
misconfigured. Enter the correct values and try again.
Note: 'blank' is there to protect my identity and is not a typing error.
Edit: I only used putty to test that my login information was correct. Putty has very little to do with my actual question.
Edit2:
<?php
$db_host = 'host';
$db_user = 'user';
$db_pass = 'pass';
$db_name = 'dbname';
?>
These are filers.
if you can login from putty, by that i think you mean localhost, and you can't from php file, which is i think trying to connect remotely, the problem most probably lies on your user not defined for your servers ip. your server, which you are trying to connect from is 'T9AF3.WPA.blank.Ca'
in mysql a username has a password and a location and mysql uses both to authenticate the user. you can have permission to connect locally but not remotely i.e. from another server. check with your system admin to make sure you have proper access defined from 'T9AF3.WPA.blank.Ca'.
Or you have not specified an IP Address for the server to listen to. It may only be listening on localhost of your server, instead of allowing remote connections. Check your mysql configuration and which IP address it is listening on.
Your mysql user must have proper rights on the database you are using

Creating a Secure Connection to a Server using PHP

I am trying to query a database that's on a server that I have a Remote Desktop connection to. What is required to be able to query a database on this server using my php web application.
You need:
Database server application configured to allow connections from external hosts
Server-side network configuration tuned to allow connections from external hosts (firewall, NAT, ...)
Database user, which is granted access to database you are going to use
PHP application, which connects to your database server under appropriate user
Details depend on what database server are you using.
$link = mysql_connect($host, $username, $password);
There is nothing secure at all by the way.
I use something like this, but it's far from secure. I keep it in a seperate "connection.php" that are required once by other files.
$db_host = 'INSERT IP HERE';
$db_user = 'User';
//My sql password for testing purposes
$db_pass = 'PASSWORD';
//name of the database
$db_name = 'dbname';
//connection
$conn = new mysqli($db_host, $db_user, $db_pass ,$db_name);
if (mysqli_connect_errno())
{
printf("Error connecting to DB, Errorcode: %s\n", mysqli_connect_error());
exit;
}
use answer by Merch member - the php code above .
You need to grant privileges and to create user on server you are retrieving the database data from.
Connect to mysql :
in shell type (if you are root type root or whatever username):
mysql -u root -p
enter password,
when you are logged in :
create user someusername#your-client-ip identified by 'password';
grant all privileges on *.* to someusername#your-client-ip with grant option;
now you can use php code above to connect to remote server database (ip you just used to create mysql user your-client-ip)
When you are creating php variable for mysql connect - host use mysql port in the variable if just ip not connects your-client-ip:3306
good luck

Connect database from other computer

I'm using PHP with MySQL database. The PCs are having a network to each other. My problem is I want to connect to the MySQL database on another computer. I want to store data on that MySQL database from another computer. How could i possibly do on this? Thanks so much for any suggestions.
The MySQL server must be configured to accept connections externally, and its firewall must be configured to allow incoming connections on that port (TCP port 3306). This may or may not already be set up.
You must also account for this in the MySQL permissions as follows.
Often, when setting up your MySQL permissions, you'll set user access rights only for #'localhost'. You'll need to make sure that both the user account and its granted permissions are set for the appropriate hostname or IP address you will be connection from. For example, you could create a new authorised user with:
GRANT ALL PRIVILEGES ON somedatabase.* TO someuser#'somehostname' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
You have to do all of this before you can connect to that server remotely with PHP, using something like this:
mysql_connect('mysqlservername', 'someuser', 'password');
Point mysql_connect() to use the other computer's name / IP address:
$server = '192.168.0.3';
$user = "foo";
$password = "bar";
$conn = mysql_connect($server, $user, $password);
You'll need to make sure the DB in the other PC has enough rights to connect from a different host - i.e. your computer.
Set up MySQL as normal on that computer. Then, simply:
<?php mysql_connect('IP of 2nd computer', 'username', 'password'); ?>

Categories