Ubuntu安装MySQL并支持ROOT远程登录

Ubuntu安装MySQL

1.查看系统有没有安装MySQL

$ dpkg -l | grep mysql

2.下载MySQL

$ apt install mysql-server

3.查看MySQl运行状态

$ netstat -nltp
$ ps -ef | grep mysql

Ubuntu安装MySQL并支持ROOT远程登录_第1张图片

4.登录

此时登录提示: ERROR 1045 (28000): Access denied for user ‘root’@'localhost’
解决办法:

1)查看/etc/mysql/debian.cnf,即可看到密码

$ sudo cat debian.cnf 
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = ApOiIr4jQa9lEXRC
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = ApOiIr4jQa9lEXRC
socket   = /var/run/mysqld/mysqld.sock

3)关闭valid_password

修改/etc/mysql/mysql.conf.d/mysqld.cnf,添加如下内容

validate_password.check_user_name    = OFF
validate_password.dictionary_file    =
validate_password.length             = 6
validate_password.mixed_case_count   = 0
validate_password.number_count       = 0
validate_password.policy             = LOW

重启服务

$ systemctl restart mysql
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'mysql.service'.
Authenticating as: user
Password: 
==== AUTHENTICATION COMPLETE ===

2)登录数据库,并修改密码

$ mysql -u debian-sys-maint  -pApOiIr4jQa9lEXRC
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.17-0ubuntu2 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> 
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
mysql> Query OK, 0 rows affected (0.00 sec)

3)使用root登录

$ mysql -u root -p123456

Ubuntu安装MySQL并支持ROOT远程登录_第2张图片

5.添加远程访问权限

1)修改root的host,把需要远程访问的用户的host改成%

mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | localhost |
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
+------------------+-----------+
mysql> update user set host='%' where user='root';
mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             |    %      |
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
+------------------+-----------+

2)修改监听IP地址

已和mysql的监听地址是127.0.0.1,只能本地访问
Ubuntu安装MySQL并支持ROOT远程登录_第3张图片

修改/etc/mysql/mysql.conf.d/mysqld.cnf,修改绑定地址为0.0.0.0

# localhost which is more compatible and is not less secure.
#bind-address		= 127.0.0.1
bind-address		= 0.0.0.0

3)重启服务

$ systemctl restart mysql.service

你可能感兴趣的:(Ubuntu)