下載MySQL鏡像
首先需下載MySQL鏡像文件,可通過國內加速鏡像倉庫下載,下載成功後系統鏡像顯示:
[jamza@A23488809 master]$
[jamza@A23488809 master]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 2151acc12881 2 weeks ago 444.6 MB
consul latest 9c9974471250 3 months ago 108.3 MB
busybox/x86_64 latest 3a093384ac30 7 months ago 1.199 MB
busybox latest 3a093384ac30 7 months ago 1.199 MB
centos latest 1e1148e4cc2c 8 months ago 201.8 MB
django latest eb40dcf64078 2 years ago 436 MB
[jamza@A23488809 master]$
創建MySQL容器並登錄
創建MySQL容器並登錄:
#創建好mysql容器
sudo docker run -p 3306:3306 --name mysql -v /home/jamza/test/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
#進入容器
sudo docker exec -it mysql /bin/bash
#登陸容器內數據庫
mysql -u root -p 設置的密碼
此時,MySQL的服務端與客戶端是運行在同一個主機上,mysql是客戶端的應用程序。
##遠程登錄服務端
由於MySQL的設計爲服務端和客戶端的模式,因此無論服務端運行在何處,通過客戶端程序與可達的網絡,即可登錄服務端。
比如,在10.46.75.9主機上運行mysql容器,在容器中運行的是mysql服務端,容器通過暴露3306端口給主機,即可通過10.46.75.9主機的3306端口訪問MySQL服務端。
在另一臺主機10.46.75.60上運行mysql客戶端程序。並連接到10.46.75.9主機上的MySQL服務端:
[jamza@localhost ~]$ mysql -h 10.46.75.9 -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.0.17 MySQL Community Server - GPL
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]>
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| jamza |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set (0.00 sec)
MySQL [(none)]>
密碼插件導致的連接錯誤
在Mysql的8.0中默認的密碼驗證插件變化了,之前是mysql_native_password,現在是caching_sha2_password。caching_sha2_password提供了更好的性能和更高的密碼安全性。
如果mysql版本太低會,則無法連接到Mysql8.0。連接時出現報錯:
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/local/mysql/lib/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
而如果你的客戶端版本低於5.7.23,而連接用戶又使用了默認的密碼驗證插件,那麼就無法通過該用戶連接,只能修改該用戶的密碼驗證插件。
比如,修改root用戶的驗證插件,如下所示,將root用戶的mysql_native_password密碼修改爲root,其他主機的客戶端使用root用戶連接服務端時,使用的密碼即是這裏設置的“root”密碼:
mysql>
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';
Query OK, 0 rows affected (0.07 sec)
mysql>
mysql>
mysql>
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.03 sec)
查看數據庫中的密碼插件更改情況:
mysql> 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
mysql> select user,host,plugin from user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| root | % | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)
mysql>