php7.2连接mysql8中遇到的坑

版权声明:转载请注明出处 https://blog.csdn.net/zizhilianqiu/article/details/88429264

php5中通过create_function来创建匿名函数捕获异常。

set_error_handler(
            create_function(
                '$severity, $message, $file, $line',
                'throw new ErrorException($message, $severity, $severity, $file, $line);'
            )
        );

在php7.2中已经废弃了该函数,故需要改成新的写法,希望有帮助。

set_error_handler(
            function($severity, $message, $file, $line){
                throw new ErrorException($message, $severity, $severity, $file, $line);
            }
        );

在连接mysql8的过程中

需要注意以下几点:

1、创建用户

//首先切到mysql库
use mysql;

//创建用户
create user 'guzi'@'localhost' identified by '7e63de77';

//授权
grant select,insert,update,create on 'guzi'@'localhost';

//更新权限
flush privileges;

问题

1、用PHP进行连接的时候 PDO连接数据库报错:SQLSTATE[HY000] [2002] No such file or directory

在这里需要检查下你配置文件里host是不是localhost,如果是需要改成127.0.0.1(一定要改成ip)

2、PHP报SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

发生这种错误,是由于MySQL 8默认使用了新的密码验证插件:caching_sha2_password,而之前的PHP版本中所带的mysqlnd无法支持这种验证。解决这个问题,我采用的是
ALTER USER 'guzi'@'localhost' IDENTIFIED WITH mysql_native_password BY '7e63de77';

flush privileges;

猜你喜欢

转载自blog.csdn.net/zizhilianqiu/article/details/88429264