Ubuntu 安装 MySQL 和远程连接
#安装MySQL
sudo apt install mysql-server
#进入数据库
sudo mysql
#修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123';
产生用户不能授权的原因是mysql 数据库中user 表中的特定用户(root) 的host 的属性值为localhost.
#查看
select host, user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| localhost | debian-sys-maint |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+------------------+
解决办法如下:
# 使用mysql 数据库
mysql > use mysql;
# 特定用户的host 修改
mysql > update user set host='%' where user='root';
# 指定用户的授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
#执行如下SQL语句,刷新权限。
flush privileges;
#修改配置文件
vim /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
port = 3306
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0