我的服务器启用的nginx,配置了php的连接mysql的配置文件connect.php:
1 2 3 4 5 6 7 | connect_error){ die ( "can't connect" . $conn ->connect_error); //如果链接失败输出错误 } if (!mysqli_select_db( $conn , 'users' )) { die ( "选择数据库失败: " . mysqli_error( $conn )); } |
其中一个页面include了这个文件,按理来说可以直接连接的,当然你需要在本地mysql建立这个users库和user表。
但是调试过程中 include函数并不起作用: include(‘/var/www/html/phpTest/connect.php’);
我们查看nginx的错误日志 cat /var/log/nginx/error.log:
下面这两个报错分别是两个不同的问题:
一、未加载php的mysqli文件
1 2 3 4 5 | 2024/10/16 15:58:08 [error] 370320#370320: *18 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Class " mysqli " not found in /var/www/html/phpTest/connect.php:7 Stack trace: #0 /var/www/html/phpTest/signup.php(32): include() #1 {main} thrown in /var/www/html/phpTest/connect.php on line 7" while reading response header from upstream, client: 113.132.177.30, server: _, request: "POST /phpTest/signup.php HTTP/1.1" , upstream: "fastcgi://unix:/run/php/php8.1-fpm.sock:" , host: "116.205.127.142" , referrer: "http://116.205.127.142/phpTest/signup.php" |
这个是因为没有加载php的mysqli文件导致的,我们需要在服务器上下载:
1 2 3 4 5 | sudo apt-get install php-mysqli sudo apt-get install php8.1-mysqli // 根据你的php版本选择msqli版本 sudo systemctl restart php8.1-fpm // 重启fpm和nginx sudo systemctl restart nginx |
还需要确保php的配置文件php.ini中没有禁用mysqli,找到;extension=mysqli 去掉前面分号注释符,重启fpm。
二、本地数据库拒绝连接
1 2 3 4 5 6 | 2024/10/16 16:27:26 [error] 371624#371624: *10 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' in /var/www/html/phpTest/connect.php:7 Stack trace: #0 /var/www/html/phpTest/connect.php(7): mysqli->__construct() #1 /var/www/html/phpTest/signup.php(32): include('...') #2 {main} thrown in /var/www/html/phpTest/connect.php on line 7" while reading response header from upstream, client: 113.132.177.30, server: _, request: "POST /phpTest/signup.php HTTP/1.1" , upstream: "fastcgi://unix:/run/php/php8.1-fpm.sock:" , host: "116.205.127.142" , referrer: "http://116.205.127.142/phpTest/signup.php" |
需要确认 root
用户是否有权限从 localhost
访问数据库。执行以下 MySQL 命令来查看权限:
登录成功后,检查用户权限:
1 | SELECT host, user FROM mysql. user ; |
如果没有 root@localhost
,或者 root
用户没有适当的权限,你可以通过以下命令为 root
赋予权限:
1 2 | GRANT ALL PRIVILEGES ON *.* TO 'root' @ 'localhost' IDENTIFIED BY 'your_password' ; FLUSH PRIVILEGES ; |
确保 MySQL 配置文件 (/etc/mysql/my.cnf
或 /etc/mysql/mysql.conf.d/mysqld.cnf
) 中允许本地连接:默认是开启的
1 | bind-address = 127.0.0.1 |
修改完之后重启mysql即可
三、权限问题
确保root用户的权限
1 | SELECT User , Host FROM mysql. user WHERE User = 'root' ; |
1 2 3 4 5 6 7 | mysql> SELECT User , Host FROM mysql. user WHERE User = 'root' ; + ------+-----------+ | User | Host | + ------+-----------+ | root | localhost | + ------+-----------+ 1 row in set (0.00 sec) |
然后检查mysql认证插件:某些情况下,MySQL 可能使用了 auth_socket
插件,这会阻止使用密码登录。检查 root
是否使用 auth_socket
插件:
1 | SELECT user , host, plugin FROM mysql. user WHERE user = 'root' ; |
如果 plugin
显示为 auth_socket
,修改为 mysql_native_password
:
1 2 | ALTER USER 'root' @ 'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password' ; FLUSH PRIVILEGES ; |
1 2 3 4 5 6 7 | mysql> SELECT user , host, plugin FROM mysql. user WHERE user = 'root' ; + ------+-----------+-----------------------+ | user | host | plugin | + ------+-----------+-----------------------+ | root | localhost | mysql_native_password | + ------+-----------+-----------------------+ 1 row in set (0.00 sec) |
尤其是最后一步,如果不修改还是连不上的
到此这篇关于php连接本地mysql连接错误的问题解决方法的文章就介绍到这了,更多相关php连接mysql连接错误内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!