IPv6 for MySQL
MySQL對IPv6的支援包括這些功能:
.MySQL伺服器可以接受客戶端透過IPv6進行TCP/IP連線。例如下面的命令是在本機透過IPv6來連線MySQL伺服器:
[mysql@localhost ~]$ mysql -h ::1 -uusername -ppassword
要使用這個功能必須滿足以下兩個條件;
.伺服器作業系統必須被配置支援IPv6
.MySQL伺服器預設配置必須除了支援IPv4的連線之外還要支援IPv6的連線。為了改變預設配置可以在啟動MySQL伺服器時使用合適的--bind-address選項。
.MySQL賬號名允許使用IPv6地址能讓DBA為透過IPv6連線到伺服器的客戶端指定許可權。例如:
mysql> create user 'v6root'@'::1' identified by "abcd"; mysql> grant select on mysql.* to 'v6root'@'::1';
.IPv6功能支援在字串和內部格式IPv6地址格式之間進行轉換並且檢查其是否為有效的IPv6地址。例如
INET6_ATON()和INET6_NTOA()類似於INET_ATON()和INET_NTOA(),但除了能處理IPv4地址之外還能處理IPv6地址。
驗證作業系統是否支援IPv6
在MySQL伺服器可以接受IPv6的連線之前,伺服器所在作業系統主機必須支援IPv6。例如在Linux主機上執行以下命令:
[root@localhost ~]# ping6 ::1 PING ::1(::1) 56 data bytes 64 bytes from ::1: icmp_seq=1 ttl=64 time=0.100 ms 64 bytes from ::1: icmp_seq=2 ttl=64 time=0.056 ms 64 bytes from ::1: icmp_seq=3 ttl=64 time=0.035 ms 64 bytes from ::1: icmp_seq=4 ttl=64 time=0.039 ms ....
為了對作業系統網路介面生成詳細的描述,執行ifconfig -a並檢視輸出中的IPv6地址。
[root@localhost ~]# ifconfig -a docker0: flags=4163 mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:15ff:fe1d:e05a prefixlen 64 scopeid 0x20 ether 02:42:15:1d:e0:5a txqueuelen 0 (Ethernet) RX packets 15 bytes 3638 (3.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 33 bytes 3825 (3.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens160: flags=4163 mtu 1500 inet 192.168.1.250 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::250:56ff:fea3:90e6 prefixlen 64 scopeid 0x20 ether 00:50:56:a3:90:e6 txqueuelen 1000 (Ethernet) RX packets 5634313 bytes 364548809 (347.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 19054 bytes 2337541 (2.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
如果你的作業系統不支援IPv6,檢視作業系統文件查詢相關指令來啟用IPv6。這可能只需要重新配置現有的網路介面來增加一個IPv6地址。或者需要執行更多的修改,比如使用IPv6選項來重建核心。
配置MySQL伺服器允許IPv6連線
MySQL伺服器對於TCP/IP連線會監聽單個網路套接字。這種套接字被繫結到單個地址,但是可能一個地址對映到多個網路介面。為了指定一個地址,在伺服器啟動時使用--bind-address=addr選項,addr是IPv4或IPv6地址或主機名。(在MySQL 5.5.3之前不支援IPv6地址)。如果addr是主機名,伺服器會解析這個主機名為一個IP地址並繫結到這個地址。
伺服器對不同型別地址的處理如下:
.如果地址為*,如果伺服器主機支援IPv6那麼伺服器可以接受來自所有伺服器上所有IPv6和IPv4網路介面的TCP/IP連線,或者否則只會接受來自所有伺服器上IPv4地址的TCP/IP連線。使用這個地址允許接受來自所有伺服器網路介面的IPv4和IPv6連線。這個值是預設值。
.如果地址為0.0.0.0,伺服器只能接受來自所有伺服器上IPv4網路介面的TCP/IP連線。
.如果地址為::,伺服器接受來自所有伺服器上的IPv4和IPv6網路介面的TCP/IP連線,這使用這個地址允許接受來自所有伺服器網路介面的IPv4和IPv6連線。
.如果地址為IPv4對映地址,伺服器接受來自這個地址的TCP/IP連線,這個地址是IPv4或IPv6格式。例如,如果伺服器被繫結到::ffff:127.0.0.1,客戶端可以使用--host=127.0.0.1或--host=::ffff:127.0.0.1來進行連線。
.如果地址為常規的IPv4或IPv6地址(比如127.0.0.1或::1),伺服器只能接受IPv4或IPv6地址的TCP/IP連線。
如果傾向於繫結伺服器到一個特定地址,確保mysql.user授權表包含一個有管理許可權的使用者可以使用這個地址進行連線。否則你將不能關閉伺服器。例如,如果你傾向繫結伺服器到*,你可以使用現有的所有賬號來連線。但是如果你繫結伺服器到::1,它只能接受這個地址的連線。在這種情況下,首先確保'root'@'::1'賬號在mysql.user表中存在,所以可以仍然連線到伺服器並關閉它。
使用IPv6本地主機地址連線MySQL伺服器
下面介紹如何配置MySQL允許客戶端透過IPv6(::1本地主機地址)連線到本地伺服器。
1.在啟動MySQL伺服器時使用合適的--bind-address選項來允許伺服器接受IPv6連線。例如,在my.cnf選項檔案中設定bind-address並重啟伺服器
[mysqld] ...... bind-address=* ...... [root@localhost ~]# systemctl stop mysqld [root@localhost ~]# systemctl start mysqld [root@localhost ~]# systemctl status mysqld * mysqld.service - LSB: start and stop MySQL Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled) Active: active (running) since 二 2020-01-14 16:56:48 CST; 5s ago Docs: man:systemd-sysv-generator(8) Process: 23257 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS) Tasks: 28 Memory: 168.2M CGroup: /system.slice/mysqld.service |-23273 /bin/sh /mysqlsoft/mysql/bin/mysqld_safe --datadir=/mysqldata/mysql --pid-file=/mysqldata/mysql/mysqld.pid `-23591 /mysqlsoft/mysql/bin/mysqld --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql --plugin-dir=/mysqlsoft/mysql/lib/plugin --user=mysql --log-error=/mysqldata/mysql/mysql.err --pid-file=/mysqldata/mysql/mysqld.pid ... 1月 14 16:56:46 localhost.localdomain systemd[1]: Starting LSB: start and stop MySQL... 1月 14 16:56:48 localhost.localdomain mysqld[23257]: Starting MySQL.. SUCCESS! 1月 14 16:56:48 localhost.localdomain systemd[1]: Started LSB: start and stop MySQL.
另外,你也可以繫結伺服器到::1,但這會使用伺服器更加受限於TCP/IP連線。它只能接受單個IPv6地址的連線並且會拒絕IPv4的連線。
2.作為管理員,連線到伺服器建立一個本地使用者賬號它將使用::1本地IPv6主機地址進行連線
mysql> create user 'ipv6root'@'::1' identified by "abcd"; Query OK, 0 rows affected (0.01 sec) mysql> grant select on *.* to 'ipv6root'@'::1'; Query OK, 0 rows affected (0.02 sec)
3.呼叫mysql客戶端工具使用新建立的賬號連線伺服器
[mysql@localhost mysql]$ mysql -h ::1 -uipv6root -pabcd mysql mysql: [Warning] Using a password on the command line interface can be insecure. Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.7.26-log Source distribution Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> select host,user from user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | csroot | | % | root | | ::1 | ipv6root | | localhost | mysql.session | | localhost | mysql.sys | +-----------+---------------+ 5 rows in set (0.00 sec)
4.執行status命令來顯示連線資訊
mysql> status -------------- mysql Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using EditLine wrapper Connection id: 7 Current database: mysql Current user: ipv6root@localhost SSL: Cipher in use is DHE-RSA-AES256-SHA Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.26-log Source distribution Protocol version: 10 Connection: ::1 via TCP/IP Server characterset: utf8mb4 Db characterset: utf8mb4 Client characterset: gb2312 Conn. characterset: gb2312 TCP port: 3306 Uptime: 11 min 47 sec Threads: 1 Questions: 263 Slow queries: 0 Opens: 336 Flush tables: 1 Open tables: 329 Queries per second avg: 0.371 -------------- mysql> SELECT CURRENT_USER(), @@bind_address; +----------------+----------------+ | CURRENT_USER() | @@bind_address | +----------------+----------------+ | ipv6root@::1 | * | +----------------+----------------+ 1 row in set (0.00 sec)
使用IPv6非本地主機地址連線MySQL伺服器
下面將介紹如何配置MySQL伺服器允許遠端主機透過IPv6來連線MySQL伺服器。這類似於在本地主機使用IPv6連線MySQL伺服器一樣,但伺服器與客戶端在不同的主機上,每臺主機有不同的IPv6地址。
伺服器主機的IPv6地址為2018::1
[root@shard1 network-scripts]# ifconfig -a ens160: flags=4163 mtu 1500 inet6 2018::1 prefixlen 64 scopeid 0x0 ether 00:50:56:a0:51:0c txqueuelen 1000 (Ethernet) RX packets 27089788 bytes 2770300693 (2.5 GiB) RX errors 0 dropped 6 overruns 0 frame 0 TX packets 6720001 bytes 532979878 (508.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
客戶端主機的IPv6地址為2018::2
ens160: flags=4163 mtu 1500 inet6 2018::2 prefixlen 64 scopeid 0x0 ether 00:50:56:a0:4e:69 txqueuelen 1000 (Ethernet) RX packets 25337245 bytes 1913014541 (1.7 GiB) RX errors 0 dropped 127 overruns 0 frame 0 TX packets 1536650 bytes 166036937 (158.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
具體操作如下:
1.在啟動MySQL伺服器時使用合適的--bind-address選項來允許伺服器接受IPv6連線。例如,在my.cnf選項檔案中設定bind-address並重啟伺服器
[mysqld] ...... bind-address=* ...... [root@localhost ~]# systemctl stop mysqld [root@localhost ~]# systemctl start mysqld [root@localhost ~]# systemctl status mysqld * mysqld.service - LSB: start and stop MySQL Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled) Active: active (running) since 二 2020-01-14 16:56:48 CST; 5s ago Docs: man:systemd-sysv-generator(8) Process: 23257 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS) Tasks: 28 Memory: 168.2M CGroup: /system.slice/mysqld.service |-23273 /bin/sh /mysqlsoft/mysql/bin/mysqld_safe --datadir=/mysqldata/mysql --pid-file=/mysqldata/mysql/mysqld.pid `-23591 /mysqlsoft/mysql/bin/mysqld --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql --plugin-dir=/mysqlsoft/mysql/lib/plugin --user=mysql --log-error=/mysqldata/mysql/mysql.err --pid-file=/mysqldata/mysql/mysqld.pid ... 1月 14 16:56:46 localhost.localdomain systemd[1]: Starting LSB: start and stop MySQL... 1月 14 16:56:48 localhost.localdomain mysqld[23257]: Starting MySQL.. SUCCESS! 1月 14 16:56:48 localhost.localdomain systemd[1]: Started LSB: start and stop MySQL.
另外,可以繫結伺服器到2018:db1:0:f01::1,但這會使用伺服器更加受限於TCP/IP連線。它只能接受單個IPv6地址的連線並且會拒絕IPv4的連線。
2.在伺服器中(2018::1)建立一個賬號將從客戶端主機(2018::2)上連線伺服器
mysql> create user 'csroot'@'%' identified by "123456"; Query OK, 0 rows affected (0.01 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'csroot'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec)
3.在客戶端主機上(2018::2),呼叫mysql客戶端使用建立的新賬號來連線伺服器(我這時使用MariaDB作為客戶端工具)
-bash-4.2$ mysql -h 2018::1 -ucsroot -p123456 mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.6.23 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [mysql]>
4.執行status命令來顯示連線資訊
MySQL [mysql]> status -------------- mysql Ver 15.1 Distrib 10.0.38-MariaDB, for Linux (x86_64) using readline 5.1 Connection id: 6 Current database: mysql Current user: csroot@2018::2 SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server: MySQL Server version: 5.6.23 MySQL Community Server (GPL) Protocol version: 10 Connection: 2018::1 via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: gb2312 Conn. characterset: gb2312 TCP port: 3306 Uptime: 34 days 50 min 24 sec Threads: 2 Questions: 175 Slow queries: 0 Opens: 87 Flush tables: 1 Open tables: 80 Queries per second avg: 0.000 -------------- MySQL [mysql]> select current_user(),@@bind_address; +----------------+----------------+ | current_user() | @@bind_address | +----------------+----------------+ | csroot@% | * | +----------------+----------------+ 1 row in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26015009/viewspace-2673372/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- IPv6 Rapid Deployment, IPv6 6rd初探API
- IPV6介紹已經IPV6改造基本步驟
- IPv6 和 RustRust
- IPv6筆記筆記
- mac關ipv6Mac
- 如何禁止IPV6
- IPv6的NAT原理
- IPv6入門教程
- IPv6的優勢
- 什麼是ipv6
- ClickHouse連線IPv6
- Redhat 5禁止IPv6Redhat
- 開啟 IPv6 新時代,升級後的 IPv6 厲害在哪?
- 什麼是IPv6?企業為什麼要升級IPv6?
- IPv6是什麼網路?IPv6網路是什麼意思
- 光貓和路由器都支援ipv6,卻無法使用ipv6?路由器
- IPv6升級有幾種方式?淺談淺談IPv6改造方案
- IPv6 VS IPv4,談談升級 IPv6 的必要性
- OpenWrt之關閉IPv6
- debian 關閉ipv6
- ipv6 udp socket例項UDP
- centos6禁用ipv6CentOS
- IPv6普及度近況
- RHEL6.6禁用IPV6
- IPv4與IPv6
- IPv6最新資訊,明確未來三年IPv6任務目標
- IPv6是什麼意思?哪款堡壘機支援IPv6資產納管?
- 中國的IPv6部署
- Azure Load Balancer : 支援 IPv6
- IPv6,看不見的全面戰爭
- IPv6改造方案:隧道技術
- iOS上線IPv6被拒iOS
- CocoaAsyncSocket (GCDAsyncSocket)適配IPv6GC
- debian下關閉ipv6
- RFC6666 IPv6黑洞路由路由
- IPv4升級IPv6,IPv6優點之層次化的地址結構
- IPv4如何向IPv6過渡?IPv6過渡轉換技術有哪些?
- IPv4到IPv6的轉換技術(上):IPv6和IPv4對比