ABHIONLINUX
Site useful for linux administration and web hosting

2009/11/16

Mysql database connection testing

Please put the following code in a php file in the server and access it through a browser to view the result.

Change the username of database user from "root" to your "database-user", followed by the password of the database user from "testuser" to your "database-access-password".

If the connection was successful the page will display Success. Other wise it will display Could not select DB.

Code:

php
$con=mysql_connect("localhost","root","testuser") or die("Could not connect");
$db=mysql_select_db("test",$con) or die("Could not select DB");
echo "Success";

Reset password for mysql

Error:
--------------
connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
mysql has failed, please contact the sysadmin (result was "mysql has failed").

Please follow the steps to reseset password for mysql.

Try the below steps :-
1) Set the passwd for mysql in /root/.my.cnf
2) killall mysqld
3) Restart mysqld with the --skip-grant-tables option as below :-
/usr/bin/safe_mysqld --skip-grant-tables &
4) Connect to Mysql :- Just type 'mysql' at the prompt without those quotes.
5) Update passwd for root user with the below command :-
> update user set Password = Password('pass') where User ='root';
6) exit