debian docker+mysql8端口修改

MySQL8 端口修改

下载mysql镜像

docker pull mysql:latest

创建并启动容器

docker run --name mysql -v $PWD/mysql/conf:/etc/mysql/conf.d -v $PWD/mysql/logs:/logs -v $PWD/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d -i -p 3309:3309 mysql:latest 

参数说明
–name: 容器名称
-v: 目录映射, 格式为:主机(宿主)目录/文件:容器目录/文件
-p: 端口映射,格式为:主机(宿主)端口:容器端口
-e: 传递环境变量
MYSQL_ROOT_PASSWORD=123456: 设置mysql的root密码
-d: 后台启动
-i: 即使没有连接,也要保持标准输入保持打开状态,一般与 -t 连用。

查看容器是否启动成功

命令: docker ps
执行结果
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                                         NAMES
126cd90f0b29   mysql:latest   "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp, 33060/tcp, 0.0.0.0:3309->3309/tcp   mysql

可以看到容器启动成功

使用Navicat Premium 连接

mysql
可以查看连接失败

查看容器内mysql的端口

进入容器
docker exec -it xxxx bash
xxxx: 容器名|容器ID
进入容器成功后执行
mysql -u root -p
提示输入密码(输入创建容器时设置的root密码即可)
在这里插入图片描述
查看mysql启动的端口

mysql> show global variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.02 sec)

可以看端口号为3306 ,和我们设置的端口不同。

修改端口配置

容器内为安装vim编辑器

apt-get update
apt-get install -y vim

安装完成后,打开mysql 配置
vim /etc/mysql/my.cnf
文件内容

# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
port = 3309 
# Custom config should go here
!includedir /etc/mysql/conf.d/

在[mysqld]段后增加 port = 3309
:wq保存即可
exit 退出容器
重启容器
docker restart mysql

再次查看mysql端口

mysql> show global variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3309  |
+---------------+-------+
1 row in set (0.01 sec)

至此端口修改成功
再次使用工具连接mysql,mysql连接成功
ml-success

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值