|Mysql login problem? General Question|
I have been working on my website and to make a long story short when I code using php/mysql I login to my database using 'localhost'... I simply use it for testing, to run scripts, etc...
However, when I upload my website to the hosting website to try to make my website accessible on the world wide web I get an error that I was unable to login successfully using the same credentials that I normally use to test my scripts out.
I am almost 100% sure this has to do with localhost not being an appropriate parameter once I upload it to the world wide web, I am guessing I will need some ip address to replace that but I am unsure on how to obtaining the right one...
Any ideas on how to fix this problem?
go to your hosing provider and find out the details IP address/server address, login etc, I assume you have created your db on your hosting account and are not trying to connect back to you local one?
When you created the database, did you also create a user with those same credentials? Seems like a dumb question but that would be the problem.
The exception might be if you have an old version of mySQL locally and are using old password methods. If the error is "Client does not support authentication protocol" that would be it: Client does not support authentication protocol [dev.mysql.com]
Thanks for the responses, I am still unable to get it to work.
I am really confused when it comes to databases and being able to work it through my free hosting service (000webhost.com).
Basically, I keep getting an error saying:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'localhost' (using password: YES) in /home/a5177208/public_html/results.php on line 3
Error: Unable to connect.
Right now it is set up to login to mysql through localhost, root (username), and my password... When I run my scripts locally... (Example: [localhost...] www/ databases/index.php) it works perfect.
When I try to upload my scripts I get the error specified above.
I found out the IP address of the 000webhost website, but it still fails.
@rocknbil --- thats not the error im getting, I dont think thats the problem.
I really dont have any experience with this...
and, idk if this makes a difference but I using this software called 'xampp' to help me run my scripts locally through 'localhost'.
Any help would be appreciated. Thanks.
I repeat my post go to your hosting providor and ask them for the location/address of your db server (it could be anything ie: db1.000webhost.com or lljknsdf.000webhost.com or even just an IP address). Until you have that info you aren't going anywhere. Then I assume you have logged onto db server and created your database with the appropriate tables etc, 000webhost should give you an admin login to your db server to allow you to do this, create a public user with password and grant permissions, put these credentials including host into your mysql_connect and robert is your fathers brother.
i far as I can tell you-re just uploading the php code and trying to connect to the local server of your pc. It doesn't work that way localhost refers to your machine and doesn't resolve in the www. the 000webhost has no idea where to look for your db server. *(although I have to say your error says you're using localhost as a username?)
**Most** shared hosting uses localhost for mysql, but that could definately be a problem. Additionally,
|Access denied for user 'localhost' (using password: YES)... |
|Right now it is set up to login to mysql through localhost, root (username), and my password... |
Leads me to believe you have something switched up in your variables. There are four basic things required to connect to mySQL. The database server, the database name, the mySQL user name, and password. I have used arbitrary $variables below, as an example.
$dbhost = 'localhost';
// or it could be mysql.example.com, as arms suggests
$db = 'The_name_of_the_database';
$db_user = 'the_mysql_user_name';
$db_pass = 'the pass';
Many things can cause this error - trying to connect to a database that doesn't exist, or one that this user doesn't have permissions for, or connecting to an incorrect server, or of course typos in the user name and password (there are others too.) But in your case, you are somehow using "localhost" as a user name. In my example, if you have this,
$dbhost = 'localhost';
$db_user = 'localhost';
.. it's likely going to generate that error.
If you check your database creation, you will probably already know the database server name if you look for it. Most hosting CP's have a "create database" function, and if you don't see "connect to your database at mysql.example.com" it's likely localhost.
BUT- once the database is created, usually you will find a place to create a database user and set that user's password. Still not done - once you've created the database user, you have to follow any links on the user name (or checkboxes and submit to modify) to assign access permissions to a given database and set the table permissions for that user.
(A note - not only is it dangerous to allow the user root to connect to an online database, most control panels won't let you. You can do it via SSH on a dedicated server, but it's a Very Bad Idea. Choose some other user name to connect to mySQL.)
One more caveat - the creation process through some control panels such as WHM, Cpanel, etc. will give you confusing names, check it very carefully. So if, for example, you create a database "test", it may prepend the name with the hosting account user name. So if your hosting account for some domain is "abc123", the actual database name will be abc123_test. The same would be true of user names - abc123_username. These are fairly easy to spot as (most of them) they won't allow you to use underscores in the DB/user names. Just be sure to use the prepended versions in your scripts.
Review all that, check your script variables, it should just work. :-)
It's as simple as this, when you are working with your local machine, your local machine is your Localhost. So now when connecting to your database, the database server name, username and password by default have to 'Localhost', 'root', and an empty string ''. By setting it to this you'll be connected successfully to your database without any error. But after uploading your website, you upload to a webserver and not a Local machine so the database server name, username and password won't be the same, and any connection to the webserver database using your local machine details won't connect and would give an error message. To correct this, you have to replace the database server name, username, and password with the ones given to you by your host service provider before uploading them, when this is done you won't get any error message when connecting to your database online.