mysql安装后,需要允许外部ip访问数据库。修改加密配置与增加新用户,配置用户权限
修改配置文件,增加默认加密方式的配置项。

当连接数据库的时候会报验证方法不存在的错误,这是因为新版本mysql的加密规则有变化,所以连不上数据库,具体可以看官网文档。可以修改mysql的配置文件,修改加密规则为原来那种,然后重新加密下所使用用户的密码。
官网文档的地址:https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html

修改mysql用户的加密方式,编辑配置文件
/etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
default_authentication_plugin=mysql_native_password

允许外部ip访问,当使用root用户的时候,直接修改root用户的host字段
update user set host = ‘%’ where user = ‘root’;

创建一个用户并且赋予权限
create user ‘tsh’@’%’ identified by ‘tsh123’;
show grants;
grant all on *.* to ‘tsh’@’%’
flush privileges

视频地址:

php脚本:
$pdo=new pdo(“mysql:host=127.0.0.1;dbname=my_test”,”tsh”,”tsh123″);
var_dump($pdo);
$pdo->query(‘set names utf8’); 

$sth=$pdo->prepare(“select * from index_test”);
$sth->execute();
$res=$sth->fetchall();
print_r($res);