MySQL(MariaDB)默认密码和修改方法

因为笔者只测试过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链接,而不验证密码。数据库

修改root密码

若是须要修改先使用debian-sys-maint登陆数据库,完成如下操做网络

  1. select user, plugin from mysql.user;查看默认的链接方式。
  2. 若是是auth_socket(MySQL)的链接方式,则继续下面得步骤,若是是unix_socket(MariaDB),则转到MariaDB的处理方法
  3. update mysql.user set authentication_string=password('root'), plugin = 'mysql_native_password' where user = 'root';使用这一行明令将root密码修改成root。
  4. flush privileges;应用权限。
  5. 退出并重启MySQL。

MariaDB的处理方法

上面的方法仅针对MySQL测试。
MariaDB的root默认链接方式是unix_socket(MariaDB)
在Debian中软件包mysql已经替换成了mariadb了。在安装后/etc/mysql/debian.cnf预设了root用户使用socket的链接方式,因此不输入密码也可在命令行直接使用mysql命令登陆。
要想使用密码链接须要修改链接方式socket

  1. 输入mysql进入客户端,若没法进入则跳过。
  2. 输入select user, plugin from mysql.user查看默认的链接方式。
  3. 若是是unix_socket(MariaDB),则继续下面得步骤。
  4. 退出mysql,并kill掉进程。
  5. 打开mysqld_safe --skip-grant-tables放入后台,并进入mysql
  6. update mysql.user set authentication_string = password('root'), plugin = 'mysql_native_password' where user = 'root';
  7. flush privileges;应用权限。
  8. 修改/etc/mysql/mariadb.conf.d/50-server.cnf中的bind-address为容许的网络地址,若为整个网络则填入0.0.0.0或注释掉
  9. 重启服务
  10. 退出并重启MySQL。

为MySQL添加远程访问

  1. 修改/etc/mysql/mysql.conf.d/mysqld.cnf``/etc/mysql/my.cnf中的bind-address为容许的网络地址,若为整个网络则填入0.0.0.0或注释掉
  2. 重启服务
  3. 启动mysql,输入use mysql;进入mysql表
  4. 修改地址grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;容许root用户远程使用root为密码链接。
  5. 使修改生效flush privileges;
  6. 退出MySQL。
相关文章
相关标签/搜索