連接MySQL
這里我使用navicat遠(yuǎn)程連接,連接MySQL前需要防火墻開(kāi)放端口或者關(guān)閉防火墻。
開(kāi)放端口
firewall-cmd --add-port=3306/tcp --permanent
// --permanent 永久生效,沒(méi)有此參數(shù)重啟后失效
firewall-cmd --reload
// 重新載入
關(guān)閉防火墻
systemctl enable iptables
// 設(shè)置開(kāi)機(jī)啟動(dòng)
firewall-cmd --reload
// 重新載入
firewalld的基本使用
啟動(dòng): systemctl start firewalld
關(guān)閉: systemctl stop firewalld
查看狀態(tài): systemctl status firewalld
開(kāi)機(jī)禁用 : systemctl disable firewalld
開(kāi)機(jī)啟用 : systemctl enable firewalld
systemctl是CentOS7的服務(wù)管理工具中主要的工具,它融合之前service和chkconfig的功能于一體。
啟動(dòng)一個(gè)服務(wù):systemctl start firewalld.service
關(guān)閉一個(gè)服務(wù):systemctl stop firewalld.service
重啟一個(gè)服務(wù):systemctl restart firewalld.service
顯示一個(gè)服務(wù)的狀態(tài):systemctl status firewalld.service
在開(kāi)機(jī)時(shí)啟用一個(gè)服務(wù):systemctl enable firewalld.service
在開(kāi)機(jī)時(shí)禁用一個(gè)服務(wù):systemctl disable firewalld.service
查看服務(wù)是否開(kāi)機(jī)啟動(dòng):systemctl is-enabled firewalld.service
查看已啟動(dòng)的服務(wù)列表:systemctl list-unit-files|grep enabled
查看啟動(dòng)失敗的服務(wù)列表:systemctl --failed
配置firewalld-cmd
查看版本: firewall-cmd --version
查看幫助: firewall-cmd --help
顯示狀態(tài): firewall-cmd --state
查看所有打開(kāi)的端口: firewall-cmd --zone=public --list-ports
更新防火墻規(guī)則: firewall-cmd --reload
查看區(qū)域信息: firewall-cmd --get-active-zones
查看指定接口所屬區(qū)域: firewall-cmd --get-zone-of-interface=eth0
拒絕所有包:firewall-cmd --panic-on
取消拒絕狀態(tài): firewall-cmd --panic-off
查看是否拒絕: firewall-cmd --query-panic
補(bǔ)充:Docker部署mysql遠(yuǎn)程連接 解決1251
原因:
mysql 8.0 默認(rèn)使用 caching_sha2_password 身份驗(yàn)證機(jī)制,客戶端不支持新的加密方式。
解決方案:
修改用戶(root)的加密方式
步驟:
1. 進(jìn)入mysql容器內(nèi)部
[root@localhost ~]# docker exec -it javakfmysql bash ## javakfmysql是容器的別名,這里也可以用容器的id代替
2. 登錄mysql
root@e285125c99d6:/# mysql -u root -p
3. 設(shè)置用戶配置項(xiàng)
查看用戶信息
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | caching_sha2_password | $A$005$HF7;krfwhkKHp5fPenQm4J2dm/RJtbbyjtCUVdDCcboXQw3ALxsif/sS1 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
備注:host為 % 表示不限制ip localhost表示本機(jī)使用 plugin非mysql_native_password
修改加密方式
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; ### 123456 mysql的登錄密碼
flush privileges;
然后再查看用戶信息
mysql> select host,user,plugin,authentication_string from mysql.user;
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| host | user | plugin | authentication_string |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
| % | root | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+------------------+-----------------------+------------------------------------------------------------------------+
5 rows in set (0.00 sec)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。