Reputation: 65
In my script on de webserver I'm trying connect to mysql through the (good old) mysql_connect() to an ip (db server) in the same network.
MySQL keeps throwing me the error: Access denied for user ''@'localhost' to database 'dbname'
This seems like the db is being searched on the localhost (webserver) instead the IP I've entered (db server).
I've checked the my.cnf and can't find a bind-address or whatever.
When I connect through mysqli_connect(), the connection can be made, so there shouldn't be a firewall issue I guess.
Reason I'm still using mysql_connect is because i'm transferring a big website to a new server, and there is no time to change the function through all the script.
Anybody familiar with this problem and got any suggestion? Thanks in advance!
Update: piece of code
$link = mysql_connect("12.34.56.78", "username", "password");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname', $link);
if (!$db_selected) {
die ('Can\'t use dbname : ' . mysql_error());
}
Upvotes: 1
Views: 188
Reputation: 20909
Make sure you have "SQL safe mode" disabled in your php.ini file.
From the documentation of mysql_connect on the server
parameter:
server
The MySQL server. It can also include a port number. e.g. "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for the localhost.
If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.
Similar filtering applies to the username
and password
parameters:
The username
paramter defaults to the name of the user that owns the server process and password
defaults to an empty string.
You can read more on SQL safe mode from here:
https://www.php.net/manual/en/ini.core.php#ini.sql.safe-mode
Upvotes: 1
Reputation: 180
Looks like the php is searching on the localhost but is using the username "". I think you have a problem with your user/pass combination and are also putting the ip address in the wrong place. Could you paste your code?
Upvotes: 0