问题
I can't seem to connect to mysql with a php script, even though I can connect fine with phpmyadmin. I created a user with a password, and gave it the proper priveleges for the db, but everytime it connects it dies saying access denied. I am using xampp on a windows xp box. Firewalls are all disabled, and I've checked the username nad password are correct. Here's the code:
$conn=mysql_connect('localhost','westbrookc16','megadots') || die (mysql_error());
Do usernames have to be in a specific format or something?
回答1:
I have a hunch that the problem here is the host you granted it to, though it's really not more than an educated guess. If you grant access myuser@'127.0.0.1' or the servers actual ip address, you won't be allowed to connect using localhost as host. This is due to the fact that when "localhost" is specified as host, php will assume that you want to use a unix socket instead of network sockets, and in that context 127.0.0.1 isn't the same as localhost.
From the manual entry for mysql_connect():
Note: Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as Runtime Configuration in your PHP configuration and leave the server field blank.
Hope this isn't totally redundant. :)
回答2:
I've seen this before, where one mysql user logs in via php and another does not. Sometimes the user even works from the commandline but not from php.
It's always been what Emil is reffering two. A mysql user is really user/host pair. so the user megadots@localhost and the user megadots@mycoolhost are listed in the mysql.user table as two seperate records.
If you can login from the command line run this query.
SELECT user, host FROM mysql.user
you should see the full list of users and thier hosts.
if you recognize the phpMyAdmin user that is working, look at the host column that's probably the host you want to use.
to reset the host run these queries (!be careful doing this your working with the privilages table for the entire mysql installation)
update mysql.user set host = 'hostname'
where user = 'username' and host = 'oldhostname';
flush privileges;
If you see a record with the username and % as the host that will have priority over everything else, if there are multiple records for the user and % as the host for one of them, it's possible that the username with % as the host has the wrong password and no matter how manytimes you reset username@localhost's password it's invalid because it'll be compared to username@% on login.
回答3:
If you are using Linux use the synaptic package manager to find and download all the libraries and mods that PHP and MySQL need so they can both connect. My problem was that PHP scripts running alone worked on the server side but when I tried using PHP scripts to connect to MySQL it would not connect I would only see a plain white page. Then I noticed I did not have the MySQL client libraries that PHP uses to conect to MySQL. I only had the MySQL server libraries. Once I installed the client side libraries and restarted the apache server my PHP scripts had no problem connecting. By default PHP 5 does not come installed with the MySQL client side libraries.
回答4:
In my case, it turned I had deleted the user that was used to create the database I was using. Normally this should give the error 'user specified definer does not exist', but for some reason it was simply returning the more general access denied to my PHP code. I'm not sure why I could still login through the command line and other interfaces. To correct my problem I recreated the user that was deleted.
回答5:
This will either be a typo or you have not granted privileges. Sometimes it can be alarmingly difficult to spot these.
I suggest don't use phpmyadmin but download a copy of SQLYOG (the free community edition is great) and try to login with your user via that. Once you have the issue diagnosed there copy/paste username/password back to your script.
Incidentally phpmyadmin is fine, but a program such as sqlyog is generally more convenient, so worth checking out anyway - I'm sure you'll be converted. If sqlyog isn't quite to your taste there are several other free and commercial alternatives.
回答6:
and gave it the proper priveleges for the db
How? What host did you use for the user when granting it?
This is how it is normally done:
GRANT ALL ON mydb.* TO 'someuser'@'somehost' identified by 'password';
FLUSH privileges;
in your case 'somehost' should probably be 'localhost' or '127.0.0.1' (see other post)
Syntax reference: http://dev.mysql.com/doc/refman/5.1/en/grant.html
回答7:
After having a similar problem I found that replacing '127.0.0.1' with 'localhost' did the trick (despite the fact that I was able to successfully connect via the terminal using 'localhost').
回答8:
I had the same problem, but then I figured it out that if you leave in the default accounts with '%' (any host), NO PASSWORD then you just cannot connect with a password. I am not sure what the problem is exactly but removing them solved it.
回答9:
- Be sure that you have configured PHP with the MySQL extensions. On MacPorts, you need to install it separately:
me@lastdance Sites % sudo port search php | grep mysql
php-mysql-xdevapi @8.0.17 (php, devel)
php52-mysql @5.2.17 (php, databases)
a PHP interface to MySQL databases, including the mysql, mysqli and pdo_mysql extensions
php53-mysql @5.3.29 (php, databases)
a PHP interface to MySQL databases, including the mysql, mysqli and pdo_mysql extensions
php54-mysql @5.4.45 (php, databases)
a PHP interface to MySQL databases, including the mysql, mysqli and pdo_mysql extensions
php55-mysql @5.5.38 (php, databases)
a PHP interface to MySQL databases, including the mysql, mysqli and pdo_mysql extensions
php56-mysql @5.6.40 (php, databases)
a PHP interface to MySQL databases, including the mysql, mysqli and pdo_mysql extensions
php70-mysql @7.0.33 (php, databases)
a PHP interface to MySQL databases, including the mysqli and pdo_mysql extensions
php71-mysql @7.1.32 (php, databases)
a PHP interface to MySQL databases, including the mysqli and pdo_mysql extensions
php71-mysql-xdevapi @8.0.17 (php, devel)
php72-mysql @7.2.23 (php, databases)
a PHP interface to MySQL databases, including the mysqli and pdo_mysql extensions
php72-mysql-xdevapi @8.0.17 (php, devel)
php73-mysql @7.3.10 (php, databases)
a PHP interface to MySQL databases, including the mysqli and pdo_mysql extensions
php73-mysql-xdevapi @8.0.17 (php, devel)
me@lastdance Sites % sudo port install php73-mysql
---> Computing dependencies for php73-mysql
---> Cleaning php73-mysql
---> Scanning binaries for linking errors
---> No broken files found.
---> No broken ports found.
me@lastdance Sites %
- Be sure that your localhost mysql socket is correctly specified in your php.ini file. See My database user exists, but I still get an (HY000/2002): No such file or directory
On my system, I had to edit the file /opt/local/etc/php73/php.ini
and add this:
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysqli.default-socket
mysqli.default_socket = /opt/local/var/run/mysql8/mysqld.sock
来源:https://stackoverflow.com/questions/668275/cant-connect-to-mysql-with-php