1. 程式人生 > >mysql 服務密碼的修改以及對服務配置的認識

mysql 服務密碼的修改以及對服務配置的認識

mysql 密碼修改

在伺服器搭建中我們提到了使用 skip-grant-tables 來進行無密碼登陸,這種方法在最初安裝好 mysql 的時候,對 mysql 的檢查,是否能夠正常執行。這種方法可以運用在我們忘記密碼的時候,進入資料庫來修改密碼。當我們對密碼進行了修改,就應該把 skip-grant-tables 註釋掉。

  • 修改密碼執行的命令

**1、**進入資料庫,建立使用者 root 初次修改密碼(必須執行 ALTER 命令)

[[email protected] ~]# /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、**使用新密碼登陸資料庫

[[email protected] ~]# /usr/local/mysql57/bin/mysql -uroot -p654321

4、把密碼寫入配置檔案中,去掉skip-grant-tables ,可實行免密碼登陸

**注意:**此是 免密碼登陸

,是指已經正確設定好密碼;和 skip-grant-tables 無密碼登陸不一樣,是基於無密碼的狀態

[[email protected] ~]# vim /etc/my.cnf                      
[client]		                                   
password=654321      #修改後的密碼加入到 [client] 客戶端中
	......

[[email protected] ~]# /usr/local/mysql57/bin/mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
	......
mysql> 
	
[
[email protected]
rosen]# /usr/local/mysql57/support-files/mysql.server restart #重啟資料庫 Shutting down MySQL.... SUCCESS! Starting MySQL.. SUCCESS!

**5、**修改主配置檔案的許可權,防止其他使用者進行有密碼或者免密碼登陸資料庫

[[email protected] ~]# 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 主服務程式: 啟動 | 停止 | 重啟 服務

[[email protected] ~]# /usr/local/mysql57/support-files/mysql.server start(stop|restart)

mysqld 服務啟動指令碼: 進入資料庫 -uroot(使用者) -p654321(密碼)

[[email protected] ~]# /usr/local/mysql57/bin/mysql -uroot -p654321

mysql 主配置檔案:

[[email protected] ~]# 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