因为笔者只测试过Ubuntu 16.04.四、Ubuntu 19.04和Debian 9,此方法不肯定在其余版本下适用。mysql
本文章介绍的方法一样适用于这样的错误信息。sql
➜ ~ mysql -u root -p Enter password: ERROR 1698 (28000): Access denied for user 'root'@'localhost'
某些发行版本,使用包管理器安装MySQL后,查看这个文件能够看到安装后的默认密码/etc/mysql/debian.cnf
,这个密码是属于debian-sys-maint
的,而非root
。
即便在这里你可能可使用root用户登陆,由于root用户默认登陆方式是使用socket链接,而不验证密码。数据库
若是须要修改先使用debian-sys-maint
登陆数据库,完成如下操做网络
select user, plugin from mysql.user;
查看默认的链接方式。auth_socket
(MySQL)的链接方式,则继续下面得步骤,若是是unix_socket
(MariaDB),则转到MariaDB的处理方法。update mysql.user set authentication_string=password('root'), plugin = 'mysql_native_password' where user = 'root';
使用这一行明令将root密码修改成root。flush privileges;
应用权限。上面的方法仅针对MySQL测试。
MariaDB的root默认链接方式是unix_socket
(MariaDB)
在Debian中软件包mysql已经替换成了mariadb了。在安装后/etc/mysql/debian.cnf
预设了root用户使用socket的链接方式,因此不输入密码也可在命令行直接使用mysql命令登陆。
要想使用密码链接须要修改链接方式socket
select user, plugin from mysql.user
查看默认的链接方式。unix_socket
(MariaDB),则继续下面得步骤。mysqld_safe --skip-grant-tables
放入后台,并进入mysql
。update mysql.user set authentication_string = password('root'), plugin = 'mysql_native_password' where user = 'root';
。flush privileges;
应用权限。/etc/mysql/mariadb.conf.d/50-server.cnf
中的bind-address
为容许的网络地址,若为整个网络则填入0.0.0.0
或注释掉/etc/mysql/mysql.conf.d/mysqld.cnf``/etc/mysql/my.cnf
中的bind-address
为容许的网络地址,若为整个网络则填入0.0.0.0
或注释掉use mysql;
进入mysql表grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
容许root用户远程使用root为密码链接。flush privileges;