Docker 安裝 mysql


概念解釋

Docker鏡像:可以理解成安裝操作系統的鏡像文件
Docker容器:可以理解為運行的操作系統。也有人比喻docker鏡像為類,docker容器為對象

第零步,查看Docker MySQL文檔

MySQL文檔地址:
https://hub.docker.com/_/mysql/

第一步,拉取MySQL鏡像

docker pull mysql

之后docker會自動拉取(下載)MySQL鏡像。
拉取成功后我們查看一下:

docker images

第二步,創建並啟動一個MySQL容器

輸入以下命令:

docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql

–name:給新創建的容器命名,此處命名為mysqlserver
-e:配置信息,此處配置mysql的root用戶的登陸密碼
-p:端口映射,表示在這個容器中使用3306端口(第二個)映射到本機的端口號也為3306(第一個)
-d:成功啟動容器后輸出容器的完整ID
最后一個mysql指的是mysql鏡像名字
到這里我們查看容器運行狀態:

docker ps

上圖可以看到容器的簡寫ID,容器的源鏡像,創建時間,狀態,端口映射信息,容器名字等。

第三步,測試連接MySQL

1、命令連接

docker exec -it  mysqlserver /bin/bash

docker exec :在運行的容器中執行命令
語法
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
OPTIONS說明:
-d :分離模式: 在后台運行
-i :即使沒有附加也保持STDIN 打開
-t :分配一個偽終端

2、使用navicat遠程連接
我是在本機上使用navicat。另外,我本機上已經安裝了mysql,所以要先把本機上安裝的mysql關掉,否則會因為端口沖突而導致無法連接。
使用 net stop mysql 關閉本機mysql服務就行了。
然后遇到2059報錯

原因是我的navicat不支持mysql新版本的加密規則,mysql8 之前的版本中加密規則是mysql_native_password,而在mysql8之后,加密規則是caching_sha2_password, 解決問題方法有兩種,一種是升級navicat驅動,一種是把mysql用戶登錄密碼加密規則還原成mysql_native_password. 我用的第二種方式:
操作如下:

ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #修改加密規則 ,'password'改成你的密碼
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; #更新一下用戶的密碼 ,'password'是你的密碼
FLUSH PRIVILEGES; #刷新權限


可以看到已經能夠正常登陸了。

其他

1.可以啟動多個MySQL服務,因為我們啟動的是容器,容器可以有多個,只要容器名字映射段端口不一樣就可以了,例如:

docker run --name mysqlserver2 -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql

下圖就是把mysqlserver2的3306綁定到了本機上的3307,使用navicat訪問的時候只要填寫成3307即可訪問。

2.查看所有容器(啟動狀態或者關閉狀態)

docker ps -a

3.啟動和關閉容器
啟動命令:

docker start mysqlserver   //通過指定容器名字sudo docker start 73f8811f669e  //通過指定容器ID

關閉命令:

docker stop mysqlserver   //通過指定容器名字
docker stop 73f8811f669e  //通過指定容器ID

3.修改MySQL配置文件有兩種方法:
一是進入容器,修改容器里的MySQL的配置文件,然后重新啟動容器,例如:

docker exec -it mysqlserver /usr/bin/bash

然后可以進入容器的命令行模式,接着修改 /etc/mysql/my.cnf 文件即可
二是掛載主機的mysql配置文件,官方文檔如下:
The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.

If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):

docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.

最后添加一點


如果遠程連接有問題,可以參考下這個https://www.cnblogs.com/hanxue53/p/5850263.html

參考:https://www.cnblogs.com/pwc1996/p/5425234.htmlhttps://www.w3cschool.cn/docker/docker-install-mysql.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM