运维经–目录
修改MySQL初始密码
每次在linux上安装完mysql/mariadb之后都会遇到登录的问题,这时需要修改密码才能登录。
这时候问题来了,都登录不上了怎么修改密码?
今天说说在登录不了的情况下怎么修改mysql密码。
1.跳过权限验证
修改/etc/mysql/mariadb.conf.d/50-server.cnf 文件,添加跳过权限验证的配置项。
# 使用管理员权限打开
$ sudo vi 50-server.cnf
添加配置项
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
skip-grant-tables #跳过权限验证
2.登录并修改密码
$ mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.37-MariaDB-0+deb9u1 Debian 9.6
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> select host,user,password from user;
+-----------+------+----------+
| host | user | password |
+-----------+------+----------+
| localhost | root | |
+-----------+------+----------+
1 row in set (0.00 sec)
MariaDB [mysql]> UPDATE user SET Password = password ('123456') WHERE User = 'root' ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> select host,user,password from user;
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *6A6D8D007B938C064A178AB17AED64EFDC088C6D |
+-----------+------+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [mysql]>
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> exit
3.重新使用新密码登录
重要:先要重新启动mysql/mariadb
(base) frank@deepin:/etc/mysql/mariadb.conf.d$ mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.37-MariaDB-0+deb9u1 Debian 9.6
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
4.最后不要忘了把刚才跳过权限验证
的配置去掉然后重启mysql/mariadb
。