简体   繁体   中英

Can't connect to database on localhost

Ok, I have a really stupid problem here, and I'm starting to get really upset.

Just a few minutes ago, I could run my website on localhost with no problems. It's running in apache2 on the same machine as mySQL (an Ubuntu box).

I changed PHP's timezone and file upload size limit, then restarted both apache and mysql.

Now when I try to run the site from apache on the server, it can no longer select the database. As far as I can tell, it doesn't have a problem actually connecting to mysql, but when I run mysql_selectdb, it doesn't work.

If I run this site from my laptop, connecting to it via my home network, it works flawlessly.

I'm really really frustrated right now because it was working just fine minutes ago. I don't remember even changing any mySQL settings.

Can someone please help me figure out how this happened, and how to fix it? I really need to get this working as soon as possible.

EDIT: Here's the code I'm using to connect:

mysql_connect($serverUrl,'xx','xxx');
@mysql_selectdb('mydatabase') or die(mysql_error());

EDIT 2: When I use the root user login, it works just fine. To the best of my knowledge, I have given root access to the user I was using before, but I guess I could be wrong. Here is the command I was using in mySQL to give access:

GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost';

the user i'm using is definitely not 'www-data'.

My guess is you're using one of the mysql_* functions before you establish the connection. That will make the mySQL library use default login data - which, in your case, fail.

Check out the file name and line number of your error message to see where it gets triggered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM