docker安装mysql

下载镜像

1
docker pull mysql:5.6

运行镜像

1
2
3
4
5
6
docker run -p 3306:3306 --name mysql \
-v /usr/local/mysql/conf:/etc/mysql/conf.d \
-v /usr/local/mysql/logs:/logs \
-v /usr/local/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.6

命令说明:
-p 3306:3306:将主机的3306端口映射到docker容器的3306端口。
--name mysql:运行服务名字
-v /usr/local/mysql/conf:/etc/mysql/conf.d :将主机/usr/local/mysql录下的conf/my.cnf 挂载到容器的 /etc/mysql/conf.d
以下my.cnf配置文件可以根据需要进行配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

-v /usr/local/mysql/logs:/logs:将主机/usr/local/mysql目录下的 logs 目录挂载到容器的 /logs。
-v /usr/local/mysql/data:/var/lib/mysql :将主机/usr/local/mysql目录下的data目录挂载到容器的 /var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
-d mysql:5.6 : 后台程序运行mysql5.6

windows连接数据库

1.使用Navicat新建连接

2.点击确定即可连接成功

如果连接失败检查端口是否开放

1
firewall-cmd --list-all

添加3306端口

1
firewall-cmd --add-port=3306/tcp --permanent 
1
firewall-cmd --reload

评论