今天需要在本地測試系統(tǒng)功能,因為本地沒有數(shù)據庫,就需要在程序里面連接遠程數(shù)據庫;
先用ssh登錄遠程服務器,用root連上數(shù)據庫看看情況;
mysql> select Host,User,Password from mysql.user;+----------------+------------------+-------------------------------------------+| Host | User | Password |+----------------+------------------+-------------------------------------------+| localhost | root | *836E233974EBE6EA32F95F890A91363F8427F78B || iz94926clkiz | root | *836E233974EBE6EA32F95F890A91363F8427F78B || 127.0.0.1 | root | *836E233974EBE6EA32F95F890A91363F8427F78B || ::1 | root | *836E233974EBE6EA32F95F890A91363F8427F78B || localhost | debian-sys-maint | *1460ED3535ABDBB887F9E5F57F40A2354610CDF3 |+----------------+------------------+-------------------------------------------+5 rows in set (0.00 sec)
一共有5個mysql賬號,Host列可以看出來,這些賬號都只支持服務器本機連接,現(xiàn)在我們來創(chuàng)建一個遠程用戶;
create user test identified by '123456';
+----------------+------------------+-------------------------------------------+| Host | User | Password |+----------------+------------------+-------------------------------------------+| localhost | root | *836E283974EBE6EA32F95F890A91363F8427F78B || iz949s6clkiz | root | *836E283974EBE6EA32F95F890A91363F8427F78B || 127.0.0.1 | root | *836E283974EBE6EA32F95F890A91363F8427F78B || ::1 | root | *836E283974EBE6EA32F95F890A91363F8427F78B || localhost | debian-sys-maint | *1460ED35E5ABDBB887F9E5F57F40A2354610CDF3 || % | test | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |+----------------+------------------+-------------------------------------------+6 rows in set (0.00 sec)
創(chuàng)建完成了,在程序里面連接發(fā)現(xiàn)還是沒有權限,剛才我們只是創(chuàng)建了用戶,還沒有對這個用戶分配權限;
grant all privileges on *.* to 'test'@'%'identified by '123456' with grant option;
all代表接受所有操作,比如 select,insert,delete....; *.* 代表所有庫下面的所有表;% 代表這個用戶允許從任何地方登錄;為了安全期間,這個%可以替換為你允許的ip地址;
然后刷新mysql用戶權限相關表;
flush privileges ;
我以為就此收工了,等等,程序怎么還是連不上去,還是 access deny ;
難道端口不是3306嗎,打開 mysql配置文件,是默認的3306,接著往下面看,發(fā)現(xiàn)一個關鍵地方;
bind-address = 127.0.0.1
原來這里mysql默認綁定了本地ip,不接受其他來源;注釋掉,重啟mysql 一切OK;
還有兩個常用操作;
修改指定用戶密碼
update mysql.user set password=password('新密碼') where User="test" and Host="localhost";
刪除用戶
delete from user where User='test' and Host='localhost';
聯(lián)系客服