默认情况下,新安装的 mariadb 的密码为空,在shell终端直接输入 mysql 就能登陆数据库。
如果是刚安装第一次使用,请使用 mysql_secure_installation 命令初始化。
# mysql_secure_installation
这里针对的是知道 root 密码,而需要修改的情况
两种修改方法:
1、直接在shell命令行使用 mysqladm 命令修改。
# mysqladmin -uroot -poldpassword password newpassword
这种方法的弊端在于会明文显示密码。
2、登陆数据库修改密码。
# mysql -uroot -p
2.1 更新 mysql 库中 user 表的字段:
MariaDB [(none)]> use mysql;
MariaDB [mysql]> UPDATE user SET password=password('newpassword') WHERE user='root';
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> exit;
2.2 或者,使用 set 指令设置root密码:
MariaDB [(none)]> SET password for 'root'@'localhost'=password('newpassword');
MariaDB [(none)]> exit;
如果是忘记了 root 密码,则需要以跳过授权的方式启动 mariadb 来修改密码。
1、先停掉服务。
# systemctl stop mariadb
2、使用跳过授权的方式启动 mariadb。
# mysqld_safe --skip-grant-tables &
3、当跳过授权启动时,可以不需要密码直接登陆数据库。登陆更新密码即可。
4、关闭跳过授权启动的进程:
5、正常启动 mariadb:
--具体操作如下:
[root@TEST ~]# ps -ef|grep mariadb
mysql 69413 69250 0 15:53 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 69998 69933 0 16:20 pts/1 00:00:00 grep --color=auto mariadb
[root@TEST ~]#
[root@TEST ~]# systemctl stop mariadb
[root@TEST ~]# ps -ef|grep mariadb
root 70038 69933 0 16:20 pts/1 00:00:00 grep --color=auto mariadb
[root@TEST ~]#
[root@TEST ~]#
[root@TEST ~]# mysqld_safe --skip-grant-tables &
[1] 70039
[root@TEST ~]# 200426 16:21:00 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
200426 16:21:00 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@TEST ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 2
Server version: 5.5.64-MariaDB MariaDB Server
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)]>
MariaDB [(none)]> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye
[root@TEST ~]# ps -ef|grep mariadb
mysql 70185 70039 0 16:21 pts/1 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 70222 69933 0 16:22 pts/1 00:00:00 grep --color=auto mariadb
[root@TEST ~]#
[root@TEST ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 4
Server version: 5.5.64-MariaDB MariaDB Server
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)]>
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]>
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [mysql]>
MariaDB [mysql]> UPDATE user SET password=password('123456') WHERE user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]>
MariaDB [mysql]> exit
Bye
[root@TEST ~]#
[root@TEST ~]# ps -ef|grep mariadb
mysql 70185 70039 0 16:21 pts/1 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 70241 69933 0 16:25 pts/1 00:00:00 grep --color=auto mariadb
[root@TEST ~]#
[root@TEST ~]# kill -9 70185 70039
[root@TEST ~]#
[root@TEST ~]# ps -ef|grep mariadb
root 70433 69933 0 16:27 pts/1 00:00:00 grep --color=auto mariadb
[root@TEST ~]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)
[root@TEST ~]#
[root@TEST ~]# systemctl start mariadb
[root@TEST ~]# ps -ef|grep mariadb
mysql 70641 70477 0 16:27 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 70681 69933 0 16:28 pts/1 00:00:00 grep --color=auto mariadb
[root@TEST ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@TEST ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 3
Server version: 5.5.64-MariaDB MariaDB Server
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)]>
MariaDB [(none)]> exit
Bye
[root@TEST ~]# ps -ef|grep mariadb
mysql 70641 70477 0 16:27 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 70692 69933 0 16:29 pts/1 00:00:00 grep --color=auto mariadb
[root@TEST ~]#
[root@TEST ~]#