安装

sudo apt install mysql-server

开启远程接入

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

找到bind-address注销

本地进入mysql

如果报错

在/etc/mysql/mysql.conf.d/mysqld.cnf加入该句

skip-grant-tables

输入下面三句sql

use mysql;
update user set host = '%' where user = 'root';
flush privileges;

完成

设置密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

如果报错

ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'

是因为上面把host修改成了%

故要用

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';

来修改密码

如果报错

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

进入/etc/mysql/mysql.conf.d/mysqld.cnf注销之前加入的skip-grant-tables字段