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的端口
进入容器
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连接成功