mysql数据库安装完成默认管理员的登录密码为空,因此可以无需输入密码直接登录数据库,这样是不安全的,以下文章简单介绍如何设置管理员登录数据库的密码和如何修改登录密码,以及忘记登录密码如何处理。
一、环境介绍
操作系统:CentOS 6.5
数据库版本:MySQL 5.5.32
主机名称:mysql-singleton
二、数据库密码管理
1、设置数据库初始密码
[root@mysql-singleton ~]# mysqladmin -uoldcat password "123456"
2、命令行修改数据库登录密码
1)linux命令行修改mysql数据库密码
[root@mysql-singleton ~]# mysqladmin -uoldcat -p123456 password "oldcat123"
2)mysql命令行修改密码
mysql> select user,host,password from mysql.user; +--------+------+-------------------------------------------+ | user | host| password | +--------+------+-------------------------------------------+ | oldcat| % |*41A67287D4BD4E7159DD624068D666ADC8917813 | +--------+------+-------------------------------------------+ 1 row in set (0.00 sec) mysql> update mysql.user set password=password("123456") where user="oldcat"; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 注:通过password函数为密码明文加密 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) 注:修改密码执行flush privileges刷新缓存,使其立即失效 也可以直接通过set命令修改当前登录用户的密码 mysql> set password=password("oldcat123"); Query OK, 0 rows affected (0.00 sec)
3)如果忘记数据库密码,则通过忽略授权表的方法启动数据库并更新数据库登录密码
a、首先停止mysql数据库服务 [root@mysql-singleton ~]# /etc/init.d/mysqld stop Shutting down MySQL. SUCCESS! b、忽略授权表启动mysql数据库服务 [root@mysql-singleton ~]# mysqld_safe --skip-grant-tables --user=mysql & [1] 27447 [root@mysql-singleton ~]# 160308 23:03:44 mysqld_safe Logging to '/application/mysql-5.5.32/data/mysql-singleton.err'. 160308 23:03:44 mysqld_safe Starting mysqld daemon with databases from /application/mysql-5.5.32/data 注:本例使用的mysql数据库单实例,如果为多实例需要通过--defaults-file指定my.cnf文件 c、无需输入密码登录数据库 [root@mysql-singleton ~]# mysql -uoldcat Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.32 Source distribution Copyright (c) 2000, 2013, 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> d、通过update命令修改密码(通过忽略授权表的方式启动数据库是不允许使用set命令更新密码) mysql> update mysql.user set password=password("123456") where user="oldcat"; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) e、重新启动mysql数据库服务,并输入密码形式登录数据库 [root@mysql-singleton ~]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS! [root@mysql-singleton ~]# ss -lntup|grep 330 tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",28000,10)) [root@mysql-singleton ~]# mysql -uoldcat -p123456 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.32 Source distribution Copyright (c) 2000, 2013, 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>