先配置阿里雲鏡像加速器,不然下載鏡像很慢
1、拉取 mysql 鏡像,Docker pull mysql:8.0.20
mysql 版本 8.0.20;
root@ubuntu:/# root@ubuntu:/# docker pull mysql:8.0.208.0.20: Pulling from library/mysql8559a31e96f4: Pull complete d51ce1c2e575: Pull complete c2344adc4858: Pull complete fcf3ceff18fc: Pull complete 16da0c38dc5b: Pull complete b905d1797e97: Pull complete 4b50d1c6b05c: Pull complete c75914a65ca2: Pull complete 1ae8042bdd09: Pull complete 453ac13c00a3: Pull complete 9e680cd72f08: Pull complete a6b5dc864b6c: Pull complete Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6Status: Downloaded newer image for mysql:8.0.20docker.io/library/mysql:8.0.20root@ubuntu:/#
2、根據鏡像創建 mysql 數據容器
docker run -p 3306:3306 --name zmf_mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.20
root@ubuntu:/# root@ubuntu:/# docker run -p 3306:3306 --name zmf_mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0.20aaadf63ee8b8ce25e6911fad4078e5468f8bbb50da3761d9561805338d21749croot@ubuntu:/# root@ubuntu:/# docker container lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaaadf63ee8b8 mysql:8.0.20 "docker-entrypoint.s…" 12 seconds ago Up 10 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp zmf_mysqlroot@ubuntu:/# root@ubuntu:/# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESaaadf63ee8b8 mysql:8.0.20 "docker-entrypoint.s…" 30 seconds ago Up 29 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp zmf_mysqlroot@ubuntu:/# root@ubuntu:/#
3、連接資料庫
①、使用命令連接
docker exec -it zmf_mysql bash
mysql -uroot -p123456
root@ubuntu:/# root@ubuntu:/# docker exec -it zmf_mysql bashroot@aaadf63ee8b8:/# root@aaadf63ee8b8:/# mysql -uroot -p123456mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 10Server version: 8.0.20 MySQL Community Server - GPLCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.00 sec)mysql>
②、使用 navicat 遠程連接
1)配置好後,測試連接報錯,如下圖:
2)解決 2059: Authentication plugin 'caching_sha2_password' cannot be loaded 錯誤,按如下步驟:
mysql> mysql> select version();+-----------+| version() |+-----------+| 8.0.20 |+-----------+1 row in set (0.00 sec)mysql> show variables like 'default_authentication_plugin';+-------------------------------+-----------------------+| Variable_name | Value |+-------------------------------+-----------------------+| default_authentication_plugin | caching_sha2_password |+-------------------------------+-----------------------+1 row in set (0.00 sec)mysql> select host,user,plugin from mysql.user;+-----------+------------------+-----------------------+| host | user | plugin |+-----------+------------------+-----------------------+| % | root | caching_sha2_password || localhost | mysql.infoschema | caching_sha2_password || localhost | mysql.session | caching_sha2_password || localhost | mysql.sys | caching_sha2_password || localhost | root | caching_sha2_password |+-----------+------------------+-----------------------+5 rows in set (0.00 sec)mysql> ALTER USER 'root'@'%' IDENTIFIED BY 'root' PASSWORD EXPIRE NEVER;Query OK, 0 rows affected (0.01 sec)mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';Query OK, 0 rows affected (0.00 sec)mysql> grant all privileges on *.* to root@'%' with grant option;Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql>
3)再連接就可以了