日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql镜像远程连接_Docker创建MySQL镜像并成功进行远程连接

發(fā)布時間:2025/4/5 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql镜像远程连接_Docker创建MySQL镜像并成功进行远程连接 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.安裝

1.1 拉取鏡像

docker pull mysql

拉取成功可以驗證一下

docker images

1.2 創(chuàng)建并啟動一個 mysql 容器

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

–name:給新創(chuàng)建的容器命名,此處命名為ly-mysql

-e:配置信息,此處配置mysql的root用戶的登陸密碼

-p:端口映射,此處映射主機3306端口到容器pwc-mysql的3306端口

-d:成功啟動容器后輸出容器的完整ID.

最后一個mysql指的是mysql鏡像名字

到這里我們查看容器運行狀態(tài):

$ sudo docker ps

可以看到容器的簡寫ID,容器的源鏡像,創(chuàng)建時間,狀態(tài),端口映射信息,容器名字等。

1.3 連接測試

使用navicat遠程連接,這里碰到幾個問題

1.3.1 mysql連接IP問題

首先這個IP肯定不是localhost,然后以為是mysql容器的IP

1.3.1.1 查看mysql容器的ip

docker inspect --format '{{ .NetworkSettings.IPAddress }}'

結果是:172.17.0.2

但是還是連接不上

1.3.1.2 獲取docker主機 IP

docker-machine ip

192.168.99.100

這個可以連接

結論:

當使用windows和macOS時,不應該使用localhost而應該使用docker-machine ip

1.3.2 連接mysql 8提示2059 - authentication plugin 'caching_sha2_password...

原因:由于myslq8不支持動態(tài)修改密碼驗證方式

解決方案:

進入mysql容器

docker exec -it ly-mysql bash

連接mysql

mysql -uroot -p

3.修改配置

use mysql;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new password';

FLUSH PRIVILEGES;

1.4 其他

1.4.1 記錄幾個命令

1.4.1.1 退出容器

如果要正常退出不關閉容器,請按Ctrl+P+Q進行退出容器

如果使用exit退出,那么在退出之后會關閉容器,可以使用下面的流程進行恢復

使用docker restart命令重啟容器

使用docker attach命令進入容器

1.4.1.2 修改MySQL配置文件有兩種方法:

一是進入容器,修改容器里的MySQL的配置文件,然后重新啟動容器,例如:

$ sudo docker exec -it ly-mysql /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.

總結

以上是生活随笔為你收集整理的mysql镜像远程连接_Docker创建MySQL镜像并成功进行远程连接的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。