Tweet
Bookmark this on Delicious
Normally, you should be able to connect to your MySQL server via your Apache server through your favorite scripting language (we saw how to install PHP on Apache running Linux in our previous tutorials, and by configuring the makefile with the option --with-mysql, we enabled MySQL support from PHP). We would like now to ascertain that we can indeed connect to a MySQL datase of our choice from a PHP script.
In order to do this, we must first create a MySQL user and give him the adequate MySQL rights. In order to list all current MySQL users, you can run the following command (in MySQL):
SELECT host, user, password from mysql.user;

If you want to create a different user named "john" whose password is "doe" at "localhost", you can enter the MySQL command:
CREATE USER 'john'@'localhost' IDENTIFIED BY 'doe';
Querying the list of users now gives:

If you do not want to store plain text MySQL passwords, you can first computer the md5 hashsums of the passwords and store them in the >mysql.user table by running the query:
CREATE USER 'john'@'localhost'
IDENTIFIED BY PASSWORD '2829fc16ad8ca5a79da932f910afad1c';
where the md5sum of "doe" was computed by using the command:
echo -n "my string" | md5sum
In order to test MySQL connectivity, I will be MySQL credentials created above. To this end, let's create a php file test.php in the htdocs (or www, public_html ...) directory of your Apache server. In this file, we will include a few basic MySQL queries in order to connect the database mysql and retrieve the different (host, user, password) as was done above by the query via the command line. The content of test.php is as follows:
< ?php
$connection = mysql_connect("localhost","root","");
mysql_select_db("mysql");
$result = mysql_query("SELECT host, user, password from mysql.user; ");
mysql_close($connection);
$display = mysql_fetch_array($result);
print_r($display);
? >
The php page test.php should display the first combination (host, user, password) above:
Array ( [0] => localhost [host] => localhost [1] => root [user] => root [2] => [password] => )
Back to computer forums
Installing MySQL with an Apache server - Connectivity of MySQL with your Apache server
Normally, you should be able to connect to your MySQL server via your Apache server through your favorite scripting language (we saw how to install PHP on Apache running Linux in our previous tutorials, and by configuring the makefile with the option --with-mysql, we enabled MySQL support from PHP). We would like now to ascertain that we can indeed connect to a MySQL datase of our choice from a PHP script.
In order to do this, we must first create a MySQL user and give him the adequate MySQL rights. In order to list all current MySQL users, you can run the following command (in MySQL):
SELECT host, user, password from mysql.user;

If you want to create a different user named "john" whose password is "doe" at "localhost", you can enter the MySQL command:
CREATE USER 'john'@'localhost' IDENTIFIED BY 'doe';
Querying the list of users now gives:

If you do not want to store plain text MySQL passwords, you can first computer the md5 hashsums of the passwords and store them in the >mysql.user table by running the query:
CREATE USER 'john'@'localhost'
IDENTIFIED BY PASSWORD '2829fc16ad8ca5a79da932f910afad1c';
where the md5sum of "doe" was computed by using the command:
echo -n "my string" | md5sum
In order to test MySQL connectivity, I will be MySQL credentials created above. To this end, let's create a php file test.php in the htdocs (or www, public_html ...) directory of your Apache server. In this file, we will include a few basic MySQL queries in order to connect the database mysql and retrieve the different (host, user, password) as was done above by the query via the command line. The content of test.php is as follows:
< ?php
$connection = mysql_connect("localhost","root","");
mysql_select_db("mysql");
$result = mysql_query("SELECT host, user, password from mysql.user; ");
mysql_close($connection);
$display = mysql_fetch_array($result);
print_r($display);
? >
The php page test.php should display the first combination (host, user, password) above:
Array ( [0] => localhost [host] => localhost [1] => root [user] => root [2] => [password] => )
In this tutorial we have seen how to install MySQL on Linux and how to use it on an Apache server running PHP (here Ubuntu 10). You will then need to learn how configure MySQL for Apache and PHP.
Back to computer forums
