mysql 密碼修改
在服務器搭建中我們提到了使用 skip-grant-tables 來進行無密碼登陸,這種方法在最初安裝好 mysql 的時候,對 mysql 的檢查,是否能夠正常運行。這種方法可以運用在我們忘記密碼的時候,進入數據庫來修改密碼。當我們對密碼進行了修改,就應該把 skip-grant-tables 註釋掉。
- 修改密碼執行的命令
**1、**進入數據庫,創建用戶 root 初次修改密碼(必須執行 ALTER 命令)
[root@centos-7 ~]# /usr/local/mysql57/bin/mysql -uroot
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql> flush privileges; #刷新使得修改後的密碼生效
mysql> exit
**2、**再一次修改密碼(後續修改密碼執行 update 命令)
mysql> update mysql.user set authentication_string=password("654321");
mysql> flush privileges; #刷新使得修改後的密碼生效
mysql> exit
**3、**使用新密碼登陸數據庫
[root@centos-7 ~]# /usr/local/mysql57/bin/mysql -uroot -p654321
4、把密碼寫入配置文件中,去掉skip-grant-tables ,可實行免密碼登陸
**注意:**此是 免密碼登陸,是指已經正確設置好密碼;和 skip-grant-tables 無密碼登陸不一樣,是基於無密碼的狀態
[root@centos-7 ~]# vim /etc/my.cnf
[client]
password=654321 #修改後的密碼加入到 [client] 客戶端中
......
[root@centos-7 ~]# /usr/local/mysql57/bin/mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
......
mysql>
[root@centos-7 rosen]# /usr/local/mysql57/support-files/mysql.server restart #重啓數據庫
Shutting down MySQL.... SUCCESS!
Starting MySQL.. SUCCESS!
**5、**修改主配置文件的權限,防止其他用戶進行有密碼或者免密碼登陸數據庫
[root@centos-7 ~]# chmod 600 /etc/my.cnf
當普通用戶登陸的時候,不管是 有密碼 還是 免密碼 登陸都會進行拒絕登陸
**注意:**此用戶是 linux 下的普通用戶,並非數據庫登陸的用戶,數據庫登陸的用戶在創建密碼的時候進行創建
bash-4.2$ /usr/local/mysql57/bin/mysql -uroot -p654321 #有密碼登陸
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
bash-4.2$ /usr/local/mysql57/bin/mysql -uroot #免密碼登陸
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- mysql 配置文件
mysqld 主服務程序: 啓動 | 停止 | 重啓 服務
[root@centos-7 ~]# /usr/local/mysql57/support-files/mysql.server start(stop|restart)
mysqld 服務啓動腳本: 進入數據庫 -uroot(用戶) -p654321(密碼)
[root@centos-7 ~]# /usr/local/mysql57/bin/mysql -uroot -p654321
mysql 主配置文件:
[root@centos-7 ~]# vim /etc/my.cnf
[client] #客戶端
port=3306 #端口3306
password=654321 #修改後的密碼寫到配置文件裏可以免密碼登陸
socket=/mysql/mysql.sock #mysql.sock 存放的位置(一般和其他mysql文件統一擺放)
[mysqld] #服務端
character-set-server=utf8
collation-server=utf8_general_ci
skip-name-resolve
#skip-grant-tables #開啓後可以無密碼登陸,修改密碼後要關閉(通常是忘記密碼的情況下)
user=mysql #用戶mysql
port=3306
basedir=/usr/local/mysql57 #啓動腳本、主程序等重要的文件存放所在的目錄
datadir=/mysql
tmpdir=/tmp
socket=/mysql/mysql.sock
log-error=/mysql/mysqld.log #錯誤日誌
pid-file=/mysql/mysqld.pid #運行的pid