# How do you get Apache and MySQL to work?

## TazG

Hello. I'm a newbie. I have Apache2 and MySQL. Apache is working but   MySQL won't. I get an error every time a page tries to use MySQL. mysqld is running. What do I need to do to set this up?

----------

## jjasghar

it took me forever to figure out that.  trying to get my phpBB up and running.....

have you created an actual database? 

```
/usr/bin/mysqladmin -u root create (databasename)
```

----------

## Zannox

I'd suggest pick the parts from this topic that applies to what you want to do. Most likely more then you need but it will get you going and should shed some light on how to get it to work as you want it to.

----------

## TazG

Whenever I type mysql it says

ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO)

?!

----------

## Nossie

Type: man mysql

and read the instructions

----------

## Zannox

You can also try looking over this topic. The link is just for the mysql part but the guide does cover Apache as well  :Smile: 

----------

## TazG

Ahh, it won't even let me into mysqladmin, or mysql. I set a password in my.cnf and I type:

mysql -u root -p

and type the password but it says 'Access denied for user: 'root@localhost'

If I try to do anything with mysqladmin it does the same thing. I unmerged and remerged mysql but it didn't help.

Apache is working fine, it just can't use MySQL.

----------

## jkt

"mysql password" (probably you meant password for root in mysql) isn't set in my.cnf, but in mysql.user table, default is afaik empty password, so:

a) ssh to your mysqld box (skip if you're running mysql on your local computer

b) `mysql -u root`

----------

## TazG

The same thing still happens with no password.

----------

## jkt

`/usr/sbin/mysql_install_db`?

----------

## TazG

I did that, there didn't seem to be any problem with it.

----------

## jkt

well, quick and dirty fix should be to unmerge mysql, wipe out your databases (don't know exactly where are they located, look at mysql ebuild) and then re-emerge mysql. and tell us what does it say.

----------

## TazG

Well that worked, sort of. I can get into mysql now but I still get errors on my pages. This is what I did:

mysql> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> grant all on *.* to web@localhost identified by '$password';

Query OK, 0 rows affected (0.04 sec)

Then I restarted mysql and apache. I go to http://localhost and get this:

Access denied for user: 'web@localhost' (Using password: YES)

----------

## Zannox

How are you using apache to interface with mysql? PHP?

----------

## TazG

Yes, PHP. The error is coming from this line:

$link = mysql_pconnect("localhost",$user,$pass);

----------

## Banzai_san

I had problem with mysql_connect() when trying to access mysql through php. Now Im able to access mysql date from php.

what I did was I reemerged mod_php again ie

#USE="apache2 mysql" emerge mod_php

stop apache2 & mysql

#/etc/init.d/mysql stop

#/etc/init.d/apache2 stop

restart apache2 & mysql

#/etc/init.d/mysql start

#/etc/init.d/apache2 start

That was all that I did.

----------

## jkt

please, do

```

SELECT * from mysql.user

```

----------

## TazG

I tried remerging php, and that command, and it still won't work!

----------

## Mr_Maniac

Hmm... Try if

 *Quote:*   

> grant all on *.* to web@'%' identified by '$password';

 

works...

----------

## Zannox

 *TazG wrote:*   

> Yes, PHP. The error is coming from this line:
> 
> $link = mysql_pconnect("localhost",$user,$pass);

 

Look in that same file, you should find something similar to

$user = "bla";

$pass = "blabla";

make sure that you change bla & blabla to a working user and password. For testing puroses ONLY you can use 

$user = "root";

$pass = "rootpassword_to_mysql_database";

You can also stop mysql and emerge -e mysql, then delete /var/lib/mysql/* (to remove all the databases). Emerge mysql again and this time pay attention and note what you set the root user to and the password. Once that is done and you can login using mysqladmin to the mysql database, use the instructions already posted to create the database the Apache will be using. Also remembering to create a user "web" and set it's password and permissions to that database.

Hope this helps.

----------

## TazG

Well none of that seems to be working but I fixed it myself:

I had this working before in Windows (with the same pages)... so I copied the files that used to be in C:\mysql to /var/lib/mysql. Now everything's working perfect. Why didn't I do that in the first place?

----------

## Zannox

I'd have to agree with Mr_Manic, it was most likely permssion errors, on the database the web page needed.

For example, if you copied JUST the database over from your windows machine, and did not create a web user within mysql, it would fail. If you created a web user within mysql, and didn't grant it proper permissions on the database it was accessing, it would fail.

I am a bit surprised that just coping the files from the windows box fixed the problem. Just goes to show you the flexibility of mysql  :Smile: 

----------

