MySQL 修改默認監聽的端口號3306 通用方法

Intro

  • 查看MySQL版本和當前的port端口號
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.19    |
+-----------+
1 row in set (0.00 sec)

mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)

mysql>
  • 在MySQL安裝目錄下查找有無配置文件和相關目錄
wuyujin@ubuntu18:/opt/mysql8$ ll
total 444K
drwxr-xr-x 10 wuyujin root    4.0K 3月   4 20:12 ./
drwxr-xr-x 17 root    root    4.0K 3月  14 09:50 ../
drwxr-xr-x  2 wuyujin   31415 4.0K 12月 10 07:32 bin/
drwxr-x---  7 wuyujin wuyujin 4.0K 4月   5 10:50 data/
drwxr-xr-x  2 wuyujin   31415 4.0K 12月 10 07:32 docs/
drwxr-xr-x  3 wuyujin   31415 4.0K 12月 10 07:32 include/
drwxr-xr-x  6 wuyujin   31415 4.0K 12月 10 07:32 lib/
-rw-r--r--  1 wuyujin   31415 397K 12月 10 03:53 LICENSE
drwxr-xr-x  4 wuyujin   31415 4.0K 12月 10 07:32 man/
-rw-r--r--  1 wuyujin   31415  687 12月 10 03:53 README
drwxr-xr-x 28 wuyujin   31415 4.0K 12月 10 07:32 share/
drwxr-xr-x  2 wuyujin   31415 4.0K 3月   4 20:17 support-files/
wuyujin@ubuntu18:/opt/mysql8$ 

別說my.cnf,連conf目錄都沒有的。

  • /etc目錄下查找有無配置文件和相關目錄
wuyujin@ubuntu18:~$ ll /etc | grep my.cnf
wuyujin@ubuntu18:~$ ll /etc | grep mysql
wuyujin@ubuntu18:~$ 

也沒有,那麼我要如何自己指定MySQL監聽的端口號呢?

查詢

./bin/mysqld --help --verbose 查看mysqld服務端(daemon)的幫助信息。
可以看到其中關於端口的選項:

  -P, --port=#        Port number to use for connection or 0 to default to,
                      my.cnf, $MYSQL_TCP_PORT, /etc/services, built-in default
                      (3306), whatever comes first

說了幾點:
- 默認監聽3306端口;
- 可以在my.cnf配置文件中配置(/etc/my.cnf$MYSQL_HOME/conf/my.cnf)
- 可以通過環境變量$MYSQL_TCP_PORT設置
- 可以通過/etc/services工具設置

我選擇設置環境變量MYSQL_TCP_PORT的方式,自定義MySQL服務器監聽端口。
我設置了這個環境變量,也使之生效,重啓MySQL服務器後,端口依舊是3306。

本質

MySQL的兩個最重要的命令工具:
- $MYSQL_HOME/bin/mysql.ere mysql客戶端,可以用它登錄MySQL,查看數據庫,表,寫SQL完成數據的增刪改查。
- $MYSQL_HOME/bin/mysql.exe mysql服務器端(daemon,守護進程),啓動MySQL實際上就是運行mysqld,並同時設置好參數
在Windows系統中,可執行(executable)的程序工具以.exe爲後綴。
而在Linux中,文件後綴名只是一個標示,可以友好的提示我們這個文件的類型。
但其實不需要文件後綴名也可以。
所以在Linux中,MySQL的這兩個命令就變成了:
- $MYSQL_HOME/bin/mysql
- $MYSQL_HOME/bin/mysqld

所以:想辦法給調用mysqld的命令中,加入參數--port 想要的端口號即可。
$MYSQL_HOME/support-files/mysql.server腳本中有這個地方。

  • 我的MySQL8啓動
    $MYSQL_HOME/support-files/mysql.server start會調用mysqld程序,完成MySQL服務器的啓動和參數的設置。
    編輯該腳本文件,找到
'start')
256     # Start daemon
257 
258     # Safeguard (relative paths, core dumps..)
259     cd $basedir
260 
261     echo $echo_n "Starting MySQL"
262     if test -x $bindir/mysqld_safe
263     then
264       # Give extra arguments to mysqld with the my.cnf file. This script
265       # may be overwritten at next upgrade.
266       $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
267       wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
268 
269       # Make lock for RedHat / SuSE
270       if test -w "$lockdir"
271       then
272         touch "$lock_file_path"
273       fi
274 
275       exit $return_value
276     else
277       log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
278     fi
279     ;;

當執行./support-files/mysql.server start時,加了start參數,就會執行這段命令。
而其中:$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &啓動了mysqld_safe
並同時設置了幾個參數:--datadir, --pid-file那麼我們只需要照着樣子再加一個參數--port=1234即可,端口自己換。
(可以認爲mysqld_safe是另一個mysqld,他也可以啓動MySQL服務器,且更加安全)
該行編輯結果:$bindir/mysqld_safe --port=1234 --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &

這樣處理有個問題,你自己改的配置,自己啓動的MySQL。
但是別人不知道你是在這個角落的啓動腳本里自定義的監聽端口號,所以,公示這些信息給共事的人,做好善後工作。

Other

如果說,之前的操作演示中,你在/etc中找到了mysql相關的配置目錄,那操作就不一樣。

wuyujin@ubuntu18:~$ ll /etc | grep my.cnf
wuyujin@ubuntu18:~$ ll /etc | grep mysql
drwxr-xr-x   3 root root   4.0K 2月  29 11:24 mysql/
wuyujin@ubuntu18:~$ ll /etc/mysql/
total 24K
drwxr-xr-x   3 root root 4.0K 2月  29 11:24 ./
drwxr-xr-x 135 root root  12K 4月   5 07:42 ../
drwxr-xr-x   2 root root 4.0K 2月  29 11:24 conf.d/
lrwxrwxrwx   1 root root   24 2月  29 11:24 my.cnf -> /etc/alternatives/my.cnf
-rw-r--r--   1 root root  839 8月   3  2016 my.cnf.fallback
wuyujin@ubuntu18:~$ 

這種情況下,修改my.cnf就好了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章