標籤:mysql資料庫 密碼 忘記密碼 資料庫丟失密碼
mysql資料庫安裝完成預設管理員的登入密碼為空白,因此可以無需輸入密碼直接登入資料庫,這樣是不安全的,以下文章簡單介紹如何設定管理員登入資料庫的密碼和如何修改登入密碼,以及忘記登入密碼如何處理。
一、環境介紹
作業系統:CentOS 6.5
資料庫版本:MySQL 5.5.32
主機名稱:mysql-singleton
二、資料庫密碼管理
1、設定資料庫初始密碼
[[email protected] ~]# mysqladmin -uoldcat password "123456"
2、命令列修改資料庫登入密碼
1)linux命令列修改mysql資料庫密碼
[[email protected] ~]# 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資料庫服務[[email protected] ~]# /etc/init.d/mysqld stopShutting down MySQL. SUCCESS!b、忽略授權表啟動mysql資料庫服務[[email protected] ~]# mysqld_safe --skip-grant-tables --user=mysql &[1] 27447[[email protected] ~]# 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、無需輸入密碼登入資料庫[[email protected] ~]# mysql -uoldcatWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.5.32 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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: 0mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)e、重新啟動mysql資料庫服務,並輸入密碼形式登入資料庫[[email protected] ~]# /etc/init.d/mysqld startStarting MySQL.. SUCCESS! [[email protected] ~]# ss -lntup|grep 330tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",28000,10))[[email protected] ~]# mysql -uoldcat -p123456Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.5.32 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql>
MySQL資料庫登入密碼管理