Forum Moderators: coopster

Message Too Old, No Replies

Problems creating Database MySQL in PHP

I can Connect to MySQL with PHP but not Create Database

         

percy05

9:53 am on Aug 12, 2005 (gmt 0)

10+ Year Member



Hello, here is an interesting problem: I can connect to MySQL and create there normally databases via command line. The problem is when I want to create a database in PHP, I have problems apparently with the configuration of username and password in MySQL. I have PHP 4.3.9 as a module Apache2. and MySQL 4.1. I repeat, I can connect normally to MySQL in PHP, using mysql_connect() with the right hostname, username, password values. but when I write a script to create a database in PHP, it has these problems. This is the simple script:
<?
$hostname = 'localhost';
$username = 'a_username';
$password = 'a_password';

$conex = mysql_connect($hostname, $username, $password);

if (!$conex) {
die('Can not connect to MySQL: '. mysql_error());
}
echo 'Connection ok';

$query = 'CREATE DATABASE dbname';
if (mysql_query($query, $conex)) {
echo 'Database created;
} else {
echo 'Error: ' . mysql_error();
}

mysql_close($conex);

?>

But with this script the database is not created. Instead of it, it appears:
Connection okError: Access denied for user 'a_username'@'localhost' to database 'dbname'

**So I do this:
STEP 1: I write in MySQL environment:
mysql>Grant all on dbname.* to a_username@localhost
->identified by 'a_password';

but unfortunately this message appears:
Warning: mysql_connect(): Client does not support authentication protocol requested by server; consider upgrading MySQL client in C:\Archivos de programa\Apache Group\Apache2\htdocs\xx1.php on line 7
Cannot connect to MySQL: Client does not support authentication protocol requested by server; consider upgrading MySQL client

STEP 2: So I write this also in MySQL environment:
mysql> UPDATE mysql.user SET Password = OLD_PASSWORD('a_password')
-> WHERE Host = 'localhost' AND User = 'a_username';
mysql> FLUSH PRIVILEGES;

**And then finally the database is created!

But here begins the rare thing: If I want to create a second database in PHP, I cannot: I must do again all the above MySQL commands, that is Step 1 and 2 together. If I want to create a third database, I must do ALL Step 1 and 2 again, and so no...

Dear folks: Why does it happen and, the most important, how can I solve it? I remember that some time ago I had a similar problem: I could connect to MySQL via command line but could not connect in PHP, and in this Forum you recommend to use STEP 2 (Update mysql.user and so on...) and it worked well, although i have never known why and for what I had to do it, if my PHP and MySQL versions were apparently ok. But now I can only connect but not create databases...
Finally I must say that I connect to MySQL in DOS in this way:
Shell>mysqld.exe
shell>mysql -u=a_username -p=a_password

I have read that the normal is connecting with mysql.exe (without D, daemon), but when I did do I could not connect well, and I have never understood why...
Is this type of connection affecting in a some way? I have read a lot in manuals but I do not find the way to solve it. Must I Upgrade to MySql 4.4.0 or 5.0.4? What do you think? - Thank you very much

stuartc1

12:10 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



Try:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'
FLUSH PRIVILEGES

coopster

1:08 pm on Aug 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's probably this known issue:
Connecting to mySQL 4.1.1 [webmasterworld.com]

percy05

4:11 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



Thanks, but I have done all this like I said in the post... And my MySQL is 4.1 and I have not migrated from a prior version (I have installed it directly) , so it should not exist an error as "old client". Thanks for your help...