Cron Job unable to access MySQL Database when host is set to localhost

leland

Member
Jan 7, 2011
11
0
51
I have some cron jobs that suddenly stopped a few days ago. I traced it back to the host setting when connecting to the database. When I use localhost, the script fails giving me this error: 'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?'. When I use the server's IP address or 127.0.0.1, the script works as it should.

Also, this only seems to be happening to one account and if I were to access the script through a browser and the host is set to 'localhost' the script works fine.

What could cause localhost to not work only for crons and for only one account?

Running:
CENTOS 6.3 x86_64 standard
WHM 11.38.0 (build 19)
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,268
463
Hello :)

Do you have your MySQL data directory or one of it's parent directories on a separate mount? If so, you may need to utilize the "/var/cpanel/jailshell-additional-mounts" file as documented here:

Create Custom Jail Shell Mounts

Thank you.
 

quietFinn

Well-Known Member
Feb 4, 2006
2,042
553
493
Finland
cPanel Access Level
Root Administrator
I had similar problems in one server, when mySQL datadir was moved, and then /var/lib/mysql was symliked to the new dir.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,268
463
I had similar problems in one server, when mySQL datadir was moved, and then /var/lib/mysql was symliked to the new dir.
Try creating the "/var/cpanel/jailshell-additional-mounts" file and editing the file to include the path for the custom mount.

Thank you.
 

quietFinn

Well-Known Member
Feb 4, 2006
2,042
553
493
Finland
cPanel Access Level
Root Administrator
Try creating the "/var/cpanel/jailshell-additional-mounts" file and editing the file to include the path for the custom mount.
Thanks but I already fixed it by removing the symlink and adding datadir in /etc/my.cnf

Will try that next time.
 
Last edited:

leland

Member
Jan 7, 2011
11
0
51
Ya, I did move the database to a different mount. I will try it our. Thank you for the suggestion.
 

patchwork

Well-Known Member
Nov 2, 2001
95
0
316
I've got a lot of crons running in each account, I've just added a new job and all of a sudden all the crons for that account have suddenly all stopped working and giving the mysql connection problem.

MYSQL is on a separate drive and has been for a long time, why would adding a new cronjob all of a sudden cause all the crons in that account to start failing?

The crons in other accounts are still fine.

- - - Updated - - -

I've created /var/cpanel/jailshell-additional-mounts and added the mysql data directory path to it, and it seems to have fixed it.

This seems like its a bug in cpanel that should be checked and fixed by upcp
 

ksaied

Member
Dec 7, 2007
10
0
51
cPanel Access Level
Root Administrator
Dear Sir,

i use the following symlink

/var/lib/mysql -> /home/mysqldata/

What should i write in /var/cpanel/jailshell-additional-mounts ??

Regards,
 
Last edited:

ksaied

Member
Dec 7, 2007
10
0
51
cPanel Access Level
Root Administrator
Dear Sir,

Not working.

I moved /var/lib/mysql -> /home/mysqldata from about 6 months but didn't test cronjobs.

This is strange, localhost in mysql_connect is working when executed from the web or from shell as root but not in cronjobs.

I receive the following error only with cronjobs:

mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

127.0.0.1 is working, but i don't want to change the code of all my clients.

What should i do to make localhost work with cronjobs.

Kindly send me detailed steps.

Thanks,
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,268
463
Please post the output from:

Code:
cat /etc/my.cnf
Also, just to verify, did you complete the step referenced in my previous post?

Thank you.
 

ksaied

Member
Dec 7, 2007
10
0
51
cPanel Access Level
Root Administrator
Yes i made the step of putting (/home/mysqldata) in /var/cpanel/jailshell-additional-mounts

[mysqld]
datadir=/home/mysqldata/
socket=/home/mysqldata/mysql.sock
interactive_timeout=120
max_user_connections=70
table_open_cache=400
event_scheduler=on
open_files_limit=16000
max_allowed_packet=268435456
query_cache_limit=1M
innodb_file_per_table=1
thread_cache_size=4
innodb_buffer_pool_size=210M
wait_timeout=28800
query_cache_size=32M
max_connections=600
query_cache_type=1
default-storage-engine=MyISAM
[client]
socket=/home/mysqldata/mysql.sock
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,268
463
Feel free to open a support ticket using the link in my signature so we can take a closer look. You can post the ticket number here so we can update this thread with the outcome.

Thank you.