报错
跟着各种博客一步步安装cacti监控,结果在浏览器输入IP/cacti/一直报错,连接不到数据库。
FATAL: Connection to Cacti database failed. Please ensure:
the PHP MySQL module is installed and enabled.
the database is running.
the credentials in config.php are valid.
我的安装环境:
centos 7
mysql 8.0.11
httpd-2.4.6
cacti-1.2.23
php-5.4.16
php-mysql-5.4.16
想要查看cacti的错误日志,但cacti/log文件夹下没有生成log文件,输入地址http://cacti.java-er.com/ 查看,也报错
System log file is not available for writing, please enable write access Log: /usr/share/cacti/log/cacti.log
问题排查
- 给log赋予各种权限
- 检查数据库cacti用户的权限,也一切正常
3.修改cacti/include/config.php,确保用户名密码与在mysql中创建的一致
$database_type = 'mysql';
$database_default = 'cacti';
$database_hostname = 'localhost';
$database_username = 'cacti'; #这里我的用户名,密码都为cacti
$database_password = 'cacti';
$database_port = '3306';
$database_retries = 5;
解决
1.先确保php安装正常,然后修改var/www/html/index.php 检测mysql连接状态
<?php
#phpinfo()
#在这里修改数据库host,用户名,密码
$link = mysql_connect('localhost', 'cacti', 'cacti');
if($link)
echo "<h1>Success!</h1>"; #连接成功
else
echo "Fail了";
echo "检测连接"
?>
~
在浏览器中输入IP地址,如果连接失败,新打开一个ssh窗口,动态查看Apache的错误日志
tailf /var/log/httpd/error_log
发现输出报错:
[Mon Apr 24 11:43:28.737720 2023] [:error] [pid 5835] [client 192.168.101.1:51901] PHP Warning: mysql_connect(): C an't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /var/www/html/index.php on lin e 6
因为cacti通过/var/lib/mysql/mysql.sock 来连接mysql的,找不到就报错了,修改/etc/my.cnf中所有的sock路径,然后重启mysql重试。
然后重启后,依然连接失败,报错:
[Mon Apr 24 12:04:32.555200 2023] [:error] [pid 9351] [client 192.168.101.1:52771] PHP Warning: mysql_connect(): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory in /var/www/html/index.php on line 6
原因
mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password。
登录mysql后,查询
mysql> select user, host, plugin from mysql.user;
+------------------+-----------------+-----------------------+
| user | host | plugin |
+------------------+-----------------+-----------------------+
| admin | % | caching_sha2_password |
| zabbix | % | mysql_native_password |
| slave | 192.168.101.131 | mysql_native_password |
| cacti | localhost | caching_sha2_password |
| mysql.infoschema | localhost | mysql_native_password |
| mysql.session | localhost | mysql_native_password |
| mysql.sys | localhost | mysql_native_password |
| root | localhost | caching_sha2_password |
+------------------+-----------------+-----------------------+
8 rows in set (0.00 sec)
发现cacti用户的加密方式是caching_sha2_password ,我的mysql版本是8的,修改其对应的plugin为 mysql_native_password 即可。
ALTER USER 'cacti'@'localhost' IDENTIFIED WITH mysql_native_password BY 'cacti';
重试
打开浏览器重试,数据库连接成功
登录cacti,登录界面也出来了
ps:这错误我排查了一天,能搜到的相关报错解决很少,总结下排查流程