方式一:使用skip-grant-tables
sudo apt-get update
sudo apt-get install mysql
# 跳过授权表验证
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
最后添加:
skip-grant-tables
# 重启
sudo systemctl restart mysql
mysql -uroot -p
回车进入myslq
FLUSH PRIVILEGES;
# 创建远程连接账号
CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
GRANT ALL ON *.* TO 'root'@'%';
# 修改本地localhost密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '密码';
# 注释掉skip-grant-tables 和 bind
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# skip-grant-tables
# bind-address = 127.0.0.1
方式二:
sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation
# 直接登陆进去
sudo mysql
# 创建远程连接账号
CREATE USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '密码';
GRANT ALL ON *.* TO 'root'@'%';
# 修改本地localhost密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '密码';
# 注释掉bind
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# bind-address = 127.0.0.1