前言:
因為工作中自己一直是用的微軟的mssql,所以幾乎沒接觸mysql,最近自己在做聊天程序離線消息保存時,
打算使用mysql持久化消息,所以想查詢下 .Net Core EFCore轉(zhuǎn)換的sql語句,所以才有了今天這篇記錄文章。
一.使用Navicat for MySQL連接上服務(wù),并且打開對應(yīng)的數(shù)據(jù)庫,
然后執(zhí)行 SHOW VARIABLES LIKE "%general_log%" 查看是否打開了監(jiān)聽。
我這里已經(jīng)是ON了,所以不用打開了。
二.如果general_log 是off,則執(zhí)行 SET GLOBAL general_log='ON',這樣就打開了日志跟蹤。
3.找到日志目錄(general_log_file),然后使用txt或者notepad++等工具d打開對應(yīng)的.log文件。
四.執(zhí)行插入語句和查詢語句試試:
總結(jié):
也可以使用CMD執(zhí)行指令查看,這里我使用了Navicat 和 notepad++。
機器 | 備注 |
機器GR2_Win10 | 個人電腦2,安裝了windows10操作系統(tǒng)機器k8s-master-1這是在機器GR2_Win10 上通過Vmware軟件實現(xiàn)的虛擬機 |
禪道系統(tǒng)(版本為12.3.3)內(nèi)置的mysql數(shù)據(jù)庫的端口號3306和宿主機機器k8s-master-1的端口號13306做了映射
機器GR2_Win10上的navicat軟件無法連接禪道系統(tǒng)(版本為12.3.3)內(nèi)置的mysql數(shù)據(jù)庫。
連接的時候報兩種錯誤
錯誤1:
錯誤2:
請找出錯誤原因并解決
C:\Users\fenglv>ping 192.168.152.100
正在 Ping 192.168.152.100 具有 32 字節(jié)的數(shù)據(jù):
來自 192.168.152.100 的回復(fù): 字節(jié)=32 時間<1ms TTL=64
來自 192.168.152.100 的回復(fù): 字節(jié)=32 時間<1ms TTL=64
來自 192.168.152.100 的回復(fù): 字節(jié)=32 時間<1ms TTL=64
來自 192.168.152.100 的回復(fù): 字節(jié)=32 時間<1ms TTL=64
192.168.152.100 的 Ping 統(tǒng)計信息:
數(shù)據(jù)包: 已發(fā)送=4,已接收=4,丟失=0 (0% 丟失),
往返行程的估計時間(以毫秒為單位):
最短=0ms,最長=0ms,平均=0ms
如果看到有TTL字樣,類似上面的輸出內(nèi)容,說明是可以ping通的,直接跳到步驟二。
如果不能ping通,那就要想辦法先解決ping通的問題,問題解決之后跳到步驟二。
C:\Users\fenglv>telnet 192.168.152.100 13306
如果看到的結(jié)果如下,說明無法連接上,繼續(xù)看步驟三。
執(zhí)行命令sudo iptables -n -L -v | grep DOCKER -C 5查看映射的端口號是否已經(jīng)添加到iptables的放行規(guī)則中,如果看到類似下面的結(jié)果,說明已經(jīng)已經(jīng)添加了。
請注意,docker容器在端口映射時,會自動把映射的宿主機的端口添加到iptables的放行規(guī)則中,所以我們可以直接telnet到宿主機的端口。
執(zhí)行命令sudo netstat -tunpl查看監(jiān)聽的端口號中是否監(jiān)聽了13306端口:
# 1.1 進入zentao_80容器
[fenglv@k8s-master-1 ~]$ sudo docker exec -it zentao_80 /bin/bash
[sudo] fenglv 的密碼:
# 1.2 編輯/etc/mysql/mariadb.conf.d/50-server.cnf文件
root@ecc2b53083ff:/# vim /etc/mysql/mariadb.conf.d/50-server.cnf
2.1 bind-address=127.0.0.1不注釋的時候表示mysql數(shù)據(jù)庫只能允許機器ip為127.0.0.1的機器來連接,而ip為127.0.0.1的機器就是安裝mysql數(shù)據(jù)庫本身的這臺機器, 這就導(dǎo)致只有安裝mysql數(shù)據(jù)庫本身的這臺機器才能連上mysql數(shù)據(jù)庫,所以其它的機器都無法連接上mysql數(shù)據(jù)庫。
2.2 注釋掉之后,表明不對連接mysql數(shù)據(jù)庫的機器做限制,所有的機器都可以連接上mysql數(shù)據(jù)庫。
"/etc/mysql/mariadb.conf.d/50-server.cnf" 134L, 3721C written
# 3.1 退出容器
root@ecc2b53083ff:/# exit
exit
# 3.2 重啟zentao_80容器
[fenglv@k8s-master-1 ~]$ sudo docker restart zentao_80
[sudo] fenglv 的密碼:
zentao_80
[fenglv@k8s-master-1 ~]$
以上的3步操作完畢之后,再次連接發(fā)現(xiàn)報的錯誤是錯誤2,錯誤2的解決方案請看步驟五。
錯誤2的原因是mysql本身的原因,mysql安裝完畢之后默認不允許除了localhost之外的主機連接,那怎么讓mysql允許其它主機連接它的?請根據(jù)下面的步驟操作:
# 1.1 進入zentao_80容器
[fenglv@k8s-master-1 ~]$ sudo docker exec -it zentao_80 /bin/bash
[sudo] fenglv 的密碼:
[fenglv@k8s-master-1 ~]$ sudo docker exec -it zentao_80 /bin/bash
[sudo] fenglv 的密碼:
# 2.1 進入mysql
root@ecc2b53083ff:/# mysql -uroot -p
# 2.2 輸入mysql的密碼
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.1.44-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 2.3 看一下有哪些數(shù)據(jù)庫
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| zentao |
+--------------------+
4 rows in set (0.00 sec)
# 2.4 選擇mysql庫
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
# 2.5 查詢root賬號所在的行
MariaDB [mysql]> select user,host,password from user where user='root';
+------+--------------+-------------------------------------------+
| user | host | password |
+------+--------------+-------------------------------------------+
| root | localhost | *6BB4******************************2CA2AD9 |
| root | 181c73dd6bcd | |
| root | 127.0.0.1 | |
| root | ::1 | |
+------+--------------+-------------------------------------------+
4 rows in set (0.00 sec)
# 3.1 更新host列的值為%
MariaDB [mysql]> update user set host='%' where user='root' and Host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
# 3.2 刷新權(quán)限,一定要刷新之后,navicat才可以連接成功
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 3.2 再次查詢
MariaDB [mysql]> select user,host,password from user where user='root';
+------+--------------+-------------------------------------------+
| user | host | password |
+------+--------------+-------------------------------------------+
| root | % | *6BB4******************************2CA2AD9 |
| root | 181c73dd6bcd | |
| root | 127.0.0.1 | |
| root | ::1 | |
+------+--------------+-------------------------------------------+
4 rows in set (0.00 sec)
以上的3步操作完畢之后,再次連接發(fā)現(xiàn)可以成功連接了。
但是在我的場景中,有一個坑點,navicat可以連上數(shù)據(jù)庫了,但是zentao_80容器里的禪道應(yīng)用無法連接上數(shù)據(jù)庫,網(wǎng)址打不開。
那就來分析下原因,發(fā)現(xiàn)用命令mysql -h 127.0.0.1 -uroot -p123456或者命令mysql -hlocalhost -uroot -p123456都無法連上數(shù)據(jù)庫
root@181c73dd6bcd:/# mysql -h 127.0.0.1 -uroot -p123456
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
而禪道應(yīng)用去連接數(shù)據(jù)庫時,正好是用root賬號,密碼123456去連,對于數(shù)據(jù)庫來說,禪道應(yīng)用所在機器的ip地址是localhost或127.0.0.1。
你關(guān)注下輸出的內(nèi)容Access denied for user 'root'@'localhost' (using password: YES) 中的 'root'@'localhost' 這個內(nèi)容,這表示是在機器localhost上用root賬號去連接遠程的數(shù)據(jù)庫被拒絕了, 提示密碼不正確,你可能會問?不是命中了user=root and host='%' and password='123456' 這行記錄了嗎?
是的,確實命中了,但是你別忘了,它應(yīng)該首選是匹配到 host='127.0.0.1' 的這行,所以如果輸入的密碼是空,那就可以正確的連上了,因為這正好命中了user表的user=root and host='127.0.0.1' and password='' 的這行記錄。
請注意,再次提醒,根據(jù)user表的內(nèi)容可知host為127.0.0.1的機器連接mysql數(shù)據(jù)庫時,user必須為root,password必須為空才可以連接上。
數(shù)據(jù)庫mysql的user表的查詢結(jié)果:
MariaDB [mysql]> select user,host,password from user where user='root';
+------+--------------+-------------------------------------------+
| user | host | password |
+------+--------------+-------------------------------------------+
| root | % | *6BB4******************************2CA2AD9 |
| root | 181c73dd6bcd | |
| root | 127.0.0.1 | |
| root | ::1 | |
+------+--------------+-------------------------------------------+
4 rows in set (0.00 sec)
對查詢出的結(jié)果做一個說明:
說明
第1行表示host為任何ip的機器連接mysql數(shù)據(jù)庫時,只有user為root,password為123456才可以連接上。
第2行表示host為181c73dd6bcd的機器連接mysql數(shù)據(jù)庫時,只有user為root,password為空才可以連接上。
第3行表示host為127.0.0.1或者localhost的機器連接mysql數(shù)據(jù)庫時,user必須為root,password必須為空才可以連接上。
所以啊,要想讓zentao_80容器里的禪道應(yīng)用連接上數(shù)據(jù)庫,一種方法是把第3行的password修改為123456,另外一種方法是把禪道應(yīng)用的數(shù)據(jù)庫密碼修改為空,隨便哪一種方法都行。
請注意,兩種方法只需執(zhí)行一個就行,不要兩個都執(zhí)行
方法1:把第3行的password修改為123456
# 把第3行的password修改為123456
MariaDB [mysql]> update user set password='*6BB4******************************2CA2AD9' where user='root' and host='127.0.0.1';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
方法2:禪道應(yīng)用的數(shù)據(jù)庫密碼修改為空
# 當(dāng)前目錄為/home/fenglv/chandao/data/pms
[fenglv@k8s-master-1 pms]$ pwd
/home/fenglv/chandao/data/pms
# 編輯文件config/my.php,把密碼修改為空,然后保存
[fenglv@k8s-master-1 pms]$ sudo vim config/my.php
[sudo] fenglv 的密碼:
我覺得步驟五的解決方案很麻煩,最省事的辦法是直接執(zhí)行grant命令之后會在表user上新增一行記錄,這行新增記錄的user值為root,host值為%,password值為123456
# 1、先查詢滿足user='root'的記錄
MariaDB [mysql]> select user,host,password from user where user='root';
+------+--------------+-------------------------------------------+
| user | host | password |
+------+--------------+-------------------------------------------+
| root | localhost | *6BB4******************************2CA2AD9 |
| root | 181c73dd6bcd | |
| root | 127.0.0.1 | |
| root | ::1 | |
+------+--------------+-------------------------------------------+
4 rows in set (0.00 sec)
# 2、執(zhí)行g(shù)rant命令
MariaDB [mysql]> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)
# 3、再次查詢滿足user='root'的記錄,發(fā)現(xiàn)多了一條
MariaDB [mysql]> select user,host,password from user where user='root';
+------+--------------+-------------------------------------------+
| user | host | password |
+------+--------------+-------------------------------------------+
| root | localhost | *6BB4******************************2CA2AD9 |
| root | 181c73dd6bcd | |
| root | 127.0.0.1 | |
| root | ::1 | |
| root | % | *6BB4******************************2CA2AD9 |
+------+--------------+-------------------------------------------+
5 rows in set (0.00 sec)
# 4、發(fā)現(xiàn)執(zhí)行完grant命令之后,無論刷不刷新都會生效
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
操作完畢之后,發(fā)現(xiàn)navicat能連上數(shù)據(jù)庫了,禪道頁面也可以正常訪問了。