1. 程式人生 > >Docker配置mysql

Docker配置mysql

首先需要從docker上拉取mysql映象

一:拉取命令操作
[[email protected] my.Shells]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
f49cf87b52c1: Pull complete
78032de49d65: Pull complete
837546b20bc4: Pull complete
9b8316af6cc6: Pull complete
1056cf29b9f1: Pull complete
86f3913b029a: Pull complete
f98eea8321ca: Pull complete
3a8e3ebdeaf5: Pull complete
4be06ac1c51e: Pull complete
920c7ffb7747: Pull complete
Digest: sha256:7cdb08f30a54d109ddded59525937592cb6852ff635a546626a8960d9ec34c30

 

二:檢視docker的映象
[[email protected] my.Shells]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest f008d8ff927d 9 days ago 408.5 MB


三:執行docker的mysql命令
[[email protected] my.Shells]# docker run --name docker-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=wzy123 -d mysql //使用者預設是root,密碼預設是root的密碼
283a1fa17fef310d9e329e11f10b8179e5be5fd88310be64fb0b4fa75ab5d80f


四:檢視mysql容器是否啟動
[[email protected] my.Shells]# docker ps //mysql容器已經啟動了
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
283a1fa17fef mysql "docker-entrypoint.sh" 7 seconds ago Up 6 seconds 0.0.0.0:3306->3306/tcp docker-mysql


五:docker使用命令列登入mysql
[[email protected] my.Shells]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
283a1fa17fef mysql "docker-entrypoint.sh" 18 hours ago Up 3 seconds 0.0.0.0:3306->3306/tcp docker-mysql
[

[email protected] my.Shells]# docker exec -it docker-mysql bash docker-mysql(映象容器的名稱)
[email protected]:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var


[email protected]:/# mysql -u root -p
Enter password: //wzy123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>

注意:當使用docker rm [id]後,mysql資料庫的所有資料會全部清除,再重新開啟一個映象是一個嶄新的資料庫。

 

 

如果發現navicat遠端連線mysql出現

 


1. 首先在容器中登入檢視mysql版本
mysql> status;
--------------
mysql Ver 8.0.11 for Linux on x86_64 (MySQL Community Server - GPL)

2. 進行授權遠端連線(注意mysql8.0跟之前的授權方式不同)
授權
GRANT ALL ON *.* TO 'root'@'%';
重新整理許可權
flush privileges;

此時,還不能遠端訪問,因為Navicat只支援舊版本的加密,需要更改mysql的加密規則
3. 更改加密規則
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;

4. 更新root使用者密碼
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';

重新整理許可權
flush privileges;
OK,設定完成,再次使用 Navicat 連線資料庫