Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to mysql using Bitnami lamp stack through php

Ok, this will be a long question.

I'm trying to get something up and running on my university account. We have a public_html folder that we can use as web space to host anything we want there.

I've installed Bitnami lamp stack in the public_html folder (probably not the best idea, security-wise, but I'm only going to test this application for a couple of days and pull it down so I really don't care so long I can get this up and running fast) and the site I want to host is working fine and accessible via (http:// (uni address)/(my account name)/public_html/lamp/apache2/htdocs/(etc..). However, certain parts of the code that connects to the databse gives me the following warning:

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I'm connecting to the database using the following code:

mysql_connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass);

Where dbhost is 'localhost'.

Since it's referencing /var/run/mysqld/mysqld.sock - it obviously means it's trying to connect to the wrong thing as the mysql in the lamp stack I've installed have the following in its my.cnf:

[mysqladmin]
user=root

[mysqld]
basedir=(my account folder)/public_html/lamp/mysql
datadir=(my account folder)/public_html/lamp/mysql/data
port=3306
socket=(my account folder)/public_html/lamp/mysql/tmp/mysql.sock
tmpdir=(my account folder)/public_html/lamp/mysql/tmp

[mysqld_safe]
mysqld=mysqld.bin

[client]
port=3306
socket=(my account folder)/public_html/lamp/mysql/tmp/mysql.sock

[manager]
port=3306
socket=(my account folder)/public_html/lamp/mysql/tmp/mysql.sock
pid-file=(my account folder)/public_html/lamp/mysql/tmp/manager.pid
default-mysqld-path=(my account folder)/public_html/lamp/mysql/bin/mysqld.bin

So my question is, anyone know how to get it to connect to the correct socket? Also, var/run/mysqld/mysqld.sock doesn't exist at all and I obviously don't have the permission to create it (not that I see how it serves my purpose at all).

This had been plaguing me since yesterday. Any help would be greatly appreciated.

Cheers!

like image 976
MikiRei Avatar asked Nov 21 '25 14:11

MikiRei


1 Answers

You might have to specify the socket defined in my.cnf using mysql_connect (you should be able to see the functional expample in example #3) instead of localhost by making $CFG->dbhost point to the socket file

$CFG->dbhost = ':(my account folder)/public_html/lamp/mysql/tmp/mysql.sock';
$link = mysql_connect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass);

See if that works!

Another word of advice you should also make sure that the socket file is actually being created by your mysql service in that location "(my account folder)/public_html/lamp/mysql/tmp/mysql.sock"


Edit:

We just needed to confirm that the path to your mysql sock file was valid and the file has been created which it appears to have been.

Can you also confirm that you can manually connect to the mysql server from the command line that would be another good step to help with the diagnosis.

mysql --socket=<my account folder>/public_html/lamp/mysql/tmp/mysql.sock -u <username> -p

Edit 2:

Just a quick confirmation that you are using the full path to your the file when you are substituting you account folder in the examples above.

e.g.

/home/<username>/public_html/lamp/mysql/tmp/mysql.sock
like image 129
houmam Avatar answered Nov 24 '25 04:11

houmam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!