Docker安装mysql
1、下载mysql
[root@VM_177_101_centos xieshuang]# docker pull mysql:5.7.18
5.7.18: Pulling from library/mysql
9f0706ba7422: Pull complete
2290e155d2d0: Pull complete
547981b8269f: Pull complete
2c9d42ed2f48: Pull complete
55e3122f1297: Pull complete
abc10bd84060: Pull complete
c0a5ce64f2b0: Pull complete
c4595eab8e90: Pull complete
098988cead35: Pull complete
300ca5fa5eea: Pull complete
43fdc4e3e690: Pull complete
Digest: sha256:d178dffba8d81afedc251498e227607934636e06228ac63d58b72f9e9ec271a6
Status: Downloaded newer image for mysql:5.7.18
docker.io/library/mysql:5.7.18
2、启动mysql并挂载数据卷
[root@VM_177_101_centos /]# docker run --restart=always --privileged=true -it -d -p 3306:3306 -v /data/docker/mysql/conf:/etc/mysql/conf.d -v /data/docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=dx123456 --name mysql mysql:5.7.18
启动成功控制台会自动打印容器id,这里可以检查一下容器状态
[root@VM_177_101_centos /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e0b9431cdec mysql:5.7.18 "docker-entrypoint.s…" 35 seconds ago Up 34 seconds 0.0.0.0:3307->3306/tcp mysql
尝试连接一下
[root@VM_177_101_centos src]# mysql -h 127.0.0.1 -P 3307 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.18 MySQL Community Server (GPL)
此时已经连接成功了,也可以通过docker命令docker inspect congtainerId
查看挂载内容
"Mounts": [
{
"Type": "bind",
"Source": "/data/docker/mysql/conf",
"Destination": "/etc/mysql/conf.d",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
{
"Type": "bind",
"Source": "/data/docker/mysql/data",
"Destination": "/var/lib/mysql",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
到这里安装过程基本结束,最后再设置一下数据库服务器默认编码格式。
mysql> show variables like 'charac%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
在挂载的宿主机目录(/data/docker/mysql/conf/
)下新建my.cnf
配置文件,内容如下:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
collation_server = utf8_general_ci
character_set_server = utf8
[client]
default_character_set=utf8
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
或者我们通过docker cp
命令将容器内的配置文件拷贝出来,然后增加编码设置也可以
docker cp a843b1adb96e:/etc/mysql/mysql.conf.d/mysqld.cnf /data/docker/mysql/conf/my.cnf
重启容器 docker restart containerId,再查看一下编码是否正常
mysql> show variables like 'charac%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
mysql> exit
好了 剩下可以用navicate等工具连接体验了