Docker 安裝 mysql

概念解釋

Docker鏡像:能夠理解成安裝操做系統的鏡像文件
Docker容器:能夠理解爲運行的操做系統。也有人比喻docker鏡像爲類,docker容器爲對象html

第零步,查看Docker MySQL文檔

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

第一步,拉取MySQL鏡像

docker pull mysql

以後docker會自動拉取(下載)MySQL鏡像。
拉取成功後咱們查看一下:sql

docker images

第二步,建立並啓動一個MySQL容器

輸入如下命令:docker

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鏡像名字
到這裏咱們查看容器運行狀態:bash

docker ps

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

第三步,測試鏈接MySQL

一、命令鏈接sqlserver

docker exec -it  mysqlserver /bin/bash

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

二、使用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; #刷新權限


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

其餘

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.html 、https://www.w3cschool.cn/docker/docker-install-mysql.html

相關文章
相關標籤/搜索