{"id":2198,"date":"2015-05-20T10:32:15","date_gmt":"2015-05-20T10:32:15","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=2198"},"modified":"2023-04-30T17:27:55","modified_gmt":"2023-04-30T17:27:55","slug":"cannot-connect-to-mysql-server-on-localhost-cacti-error","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/cannot-connect-to-mysql-server-on-localhost-cacti-error","title":{"rendered":"Cannot connect to MySQL server on &#8216;localhost&#8217;: Cacti Error"},"content":{"rendered":"\n<p>I was installing and setting up Cacti recently and faced following error:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><i>&#8220;FATAL: Cannot connect to MySQL server on &#8216;localhost&#8217;.&#8221;<\/i><\/h2>\n\n\n\n<p>This post includes debugging steps and solution for the issue.<\/p>\n\n\n\n<p>Along with the Cacti&#8217;s web-interface, the poller.php was generating the same error as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@ubuntu:\/var\/www\/cacti# php \/var\/www\/cacti\/poller.php\nFATAL: Cannot connect to MySQL server on 'localhost'. Please make sure you have specified a valid MySQL database name in 'include\/config.php'\n<\/pre>\n\n\n\n<!--more-->\n\n\n\n<p>Also the apache error log expressed following errors:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tail -10f \/var\/log\/apache2\/error.log\n[Mon May 04 02:17:56 2015] [error] [client 127.0.0.1] PHP Warning:  include_once(\/var\/www\/cacti-0.8.8c\/lib\/adodb\/adodb.inc.php): failed to open stream: Permission denied in \/var\/www\/cacti-0.8.8c\/include\/global.php on line 205\n[Mon May 04 02:17:56 2015] [error] [client 127.0.0.1] PHP Warning:  include_once(): Failed opening '\/var\/www\/cacti-0.8.8c\/lib\/adodb\/adodb.inc.php' for inclusion (include_path='.:\/usr\/share\/php:\/usr\/share\/pear') in \/var\/www\/cacti-0.8.8c\/include\/global.php on line 205\n[Mon May 04 02:17:56 2015] [error] [client 127.0.0.1] PHP Warning:  include_once(\/var\/www\/cacti-0.8.8c\/lib\/database.php): failed to open stream: Permission denied in \/var\/www\/cacti-0.8.8c\/include\/global.php on line 206\n[Mon May 04 02:17:56 2015] [error] [client 127.0.0.1] PHP Warning:  include_once(): Failed opening '\/var\/www\/cacti-0.8.8c\/lib\/database.php' for inclusion (include_path='.:\/usr\/share\/php:\/usr\/share\/pear') in \/var\/www\/cacti-0.8.8c\/include\/global.php on line 206\n[Mon May 04 02:17:56 2015] [error] [client 127.0.0.1] PHP Fatal error:  Call to undefined function db_connect_real() in \/var\/www\/cacti-0.8.8c\/include\/global.php on line 209\n[Mon May 04 02:18:42 2015] [error] [client 127.0.0.1] PHP Notice:  Undefined variable: host in \/var\/www\/cacti-0.8.8c\/lib\/database.php on line 35\n[Mon May 04 02:18:42 2015] [error] [client 127.0.0.1] PHP Notice:  Undefined variable: user in \/var\/www\/cacti-0.8.8c\/lib\/database.php on line 35\n[Mon May 04 02:18:42 2015] [error] [client 127.0.0.1] PHP Notice:  Undefined variable: pass in \/var\/www\/cacti-0.8.8c\/lib\/database.php on line 35\n[Mon May 04 02:18:42 2015] [error] [client 127.0.0.1] PHP Notice:  Undefined variable: db_name in \/var\/www\/cacti-0.8.8c\/lib\/database.php on line 35\n[Mon May 04 02:18:42 2015] [error] [client 127.0.0.1] PHP Notice:  Undefined variable: db_type in \/var\/www\/cacti-0.8.8c\/lib\/database.php on line 35\n[Mon May 04 02:18:42 2015] [error] [client 127.0.0.1] PHP Notice:  Undefined variable: port in \/var\/www\/cacti-0.8.8c\/lib\/database.php on line 35\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting steps:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Verified we have correct dir permisssions (Gave permissions: ugo+rx).<\/li>\n\n\n\n<li>Verified ping to localhost (works). If you&#8217;re having other hostname check to see if it&#8217;s ping&#8217;able.<\/li>\n\n\n\n<li>Confirmed that connectivity to mysql-server using mysql client using same parameters works.<\/li>\n\n\n\n<li>There is no bind-address specified in MySQL config that may cause connectivity issues.<\/li>\n<\/ul>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p>After checking on above possibilities, the issue was still there. As said the Cacti&#8217;s web-interface on web-browser http:\/\/localhost\/cacti generated following errors:<br><em> FATAL: Cannot connect to MySQL server on &#8216;localhost&#8217;. Please make sure you have specified a valid MySQL database name in &#8216;include\/config.php&#8217;<\/em><\/p>\n\n\n\n<p>I checked the global.php configurations and found db_connect_read function:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># File: include\/global.php\n\n\/* connect to the database server *\/\ndb_connect_real($database_hostname, $database_username, $database_password, $database_default, $database_type, $database_port, $database_ssl);\n<\/pre>\n\n\n\n<p>The configuration in Cacti&#8217;s database config file is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">root@ubuntu:\/var\/www\/cacti# cat include\/config.php \n$database_type = \"mysql\";\n$database_default = \"cacti\";\n$database_hostname = \"localhost\";\n$database_username = \"cacti\";\n$database_password = \"xxxxx\";\n$database_port = \"3306\";\n$database_ssl = false;\n<\/pre>\n\n\n\n<p>Changing $database_hostname to 127.0.0.1 in include\/config.php made the Cacti setup work!!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$database_hostname = \"127.0.0.1\";\n<\/pre>\n\n\n\n<p>Though I decided to resolve this keeping database_hostname to &#8220;localhost&#8221;. I looked at possible issues related to db_connect_real and php.ini details.<\/p>\n\n\n\n<p>Added doing following change in php configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># vi \/etc\/php5\/apache2\/php.ini\nmysql.default_socket =\/var\/lib\/mysql\/mysqld.sock\n<\/pre>\n\n\n\n<p>Restart apache and boom&#8230; Connectivity worked with hostname as &#8220;localhost&#8221;, the Cacti admin page loaded!!<\/p>\n\n\n\n<p>But why did that socket file change worked for hostname &#8220;localhost&#8221;!!! and why it was not concerned if I use hostname as IP (127.0.0.1)!<\/p>\n\n\n\n<p>I debugged further and created a softlink to MySQL socket-file at \/var\/run\/mysqld\/mysqld.sock and removed &#8220;mysql.default_socket =\/var\/lib\/mysql\/mysqld.sock &#8221; from php.ini<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ln -s \/var\/lib\/mysql\/mysqld.sock \/var\/run\/mysqld\/mysqld.sock\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">cat php.ini | grep default_socket\n## mysql.default_socket=\/var\/lib\/mysql\/mysqld.sock\n<\/pre>\n\n\n\n<p>Restarted apache<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/etc\/init.d\/apache2 restart\n<\/pre>\n\n\n\n<p>and the Cacti worked for &#8220;localhost&#8221; database host!!<\/p>\n\n\n\n<p>So, for localhost as database_hostname, db_connect_real expect \/var\/run\/mysqld\/mysqld.sock as socket! I wonder if db_connect_real would like to buy a $database_socket variable in Cacti&#8217;s config.php?!!<\/p>\n\n\n\n<p>Anyways, there could be other issues that we checked earlier while &#8220;Troubleshooting Steps&#8221;, the issue here was MySQL&#8217;s socket file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you&#8217;re using location other than \/var\/run\/mysqld\/mysqld.sock for mysql socket, specify it in &#8220;mysql.default_socket&#8221; in php.ini<\/li>\n\n\n\n<li>If you can restart MySQL, change it to use socket=\/var\/run\/mysqld\/mysqld.sock.<\/li>\n<\/ul>\n\n\n\n<p>Do comment if you have more information on this behaviour!<\/p>\n","protected":false},"excerpt":{"rendered":"FATAL: Cannot connect to MySQL server on &#8216;localhost&#8217;: Cacti Error &#8211; Solution to the issue while setting-up.\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,6],"tags":[570,571,427,75],"class_list":{"0":"post-2198","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-technical","8":"tag-cacti-error","9":"tag-cannot-connect-to-mysql-on-localhost","10":"tag-mysql","11":"tag-mysql-server"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=2198"}],"version-history":[{"count":7,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2198\/revisions"}],"predecessor-version":[{"id":2863,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2198\/revisions\/2863"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=2198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=2198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=2198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}