MySQL密碼設定和重設,以及遠程登入資料庫

來源:互聯網
上載者:User

標籤:mysql


1、配置MySQL的root密碼

預設情況下MySQL沒有密碼,直接就可以進入:

[[email protected] ~]# mysql -uroot

下面我們設定root密碼:

[[email protected] ~]# mysqladmin -uroot password ‘123456‘

再進入時需要輸入密碼(p選項後無空格):

[[email protected] ~]# mysql -uroot -p123456

重啟生效

[[email protected] ~]# /etc/init.d/mysqld restart


2、重設密碼

如果我們忘記了密碼,則需要初始化來重設;編輯設定檔

[[email protected] ~]# vim /etc/my.cnf   //添加下面命令

skip-grant

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/A0/wKioL1XpqZDRfZUQAABy0rx_WQs605.jpg" style="float:none;" title="6.png" alt="wKioL1XpqZDRfZUQAABy0rx_WQs605.jpg" />

儲存退出後重啟MySQL服務

[[email protected] ~]# /etc/init.d/mysqld restart

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/A3/wKiom1Xpp2yy9WeRAACLoIyW1s8397.jpg" style="float:none;" title="7.png" alt="wKiom1Xpp2yy9WeRAACLoIyW1s8397.jpg" />

[[email protected] ~]# mysql -uroot    //再次進入無須輸入密碼

mysql> use mysql           //使用mysql庫

mysql> update user set password=password(‘654321‘) where user=‘root‘; //更新表(修改密碼)

mysql>quit

然後找到/etc/my.cnf設定檔,刪除之前的skip-grant命令

重啟生效

[[email protected] ~]# /etc/init.d/mysqld restart

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/A0/wKioL1XprNPyv4UQAAJnOAZPRDY881.jpg" title="8.png" alt="wKioL1XprNPyv4UQAAJnOAZPRDY881.jpg" />


3、設定遠程登入

首先給客戶機授權

mysql> grant all on *.* to ‘root‘@‘192.168.0.104‘ identified by ‘123aaa‘;

Query OK, 0 rows affected (0.01 sec)

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/72/A0/wKioL1XpsVvwtde9AADGOBw6VIY274.jpg" title="9.png" alt="wKioL1XpsVvwtde9AADGOBw6VIY274.jpg" />

註:192.168.0.104是用戶端的IP,登入密碼:123aaa

我們來測試登入

[[email protected] ~]# mysql -uroot -h192.168.0.104 -P3306 -p123aaa

(登入成功):

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/72/A0/wKioL1XpsmeCoAL_AAKRNf5-zuU178.jpg" title="10.png" alt="wKioL1XpsmeCoAL_AAKRNf5-zuU178.jpg" />


我們可以查看下目前使用者

mysql> select user();

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/72/A0/wKioL1XpsvjCzIcWAACSnZ8QSgc406.jpg" title="11.png" alt="wKioL1XpsvjCzIcWAACSnZ8QSgc406.jpg" />


有時候可能伺服器上有多個資料庫,在伺服器上我們就可以使用sock登入

[[email protected] ~]# mysql -uroot -S /tmp/mysql.sock -p654321

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/72/A0/wKioL1Xps7Wyr51GAAJ_FU-dN3k795.jpg" title="12.png" alt="wKioL1Xps7Wyr51GAAJ_FU-dN3k795.jpg" />




MySQL密碼設定和重設,以及遠程登入資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.