Docker 独立安装mysql 8.0 镜像 (latest version)
[root@VM-0-16-centos dafufoto]#docker pull mysql
启动镜像:
docker run -d --name dafu_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<here is password> mysql --character-set-server=utf8 --collation-server=utf8_general_ci
参数说明:
-p 3306:3306 :映射容器服务的 3306 端口到宿主机的 3306 端口,外部主机可以直接通过 宿主机ip:3306 访问到 MySQL 的服务。
MYSQL_ROOT_PASSWORD=<here is your password>:设置 MySQL 服务 root 用户的密码。
--character-set-server=utf8:设置字符集为utf8
--collation-server=utf8_general_cli:设置字符比较规则为utf8_general_cli
[root@VM-0-16-centos dafufoto]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1344b2ae55ee dafu_image "nginx -g 'daemon ..." 37 hours ago Up 24 hours 0.0.0.0:80->80/tcp dafu_container
[root@VM-0-16-centos dafufoto]# docker run -d --name dafu_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<here is your password> mysql --character-set-server=utf8 --collation-server=utf8_general_ci
5c692a337f9889704f31516a68bb14318fe2b1adb4b7b633c630cec21cf4191f
[root@VM-0-16-centos dafufoto]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c692a337f98 mysql "docker-entrypoint..." 5 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp dafu_mysql
1344b2ae55ee dafu_image "nginx -g 'daemon ..." 37 hours ago Up 24 hours 0.0.0.0:80->80/tcp dafu_container
[root@VM-0-16-centos dafufoto]#
在本地用SQL客户端进行远程连接测试一下是否OK,在连接之前要打开访问3306端口,测试完毕后,一定要切记关闭宿主主机的3306端口,我们网站是从内部 访问,不需要从宿主主机外部访问。
由于数据库是类似于独立的主机,所以,在应用服务器上需要设置mysql docker 的IP地址,而不能用127.0.0.1作为地址。
查看 mysql server的IP地址:
[root@VM-0-16-centos dafufoto]# docker inspect bridge
[
{
"Name": "bridge",
"Id": "f97ecc252beb1bb51c183b4a6e0ef9bba476e8131a58c10b6e5d5a8eae55edbf",
"Created": "2020-06-18T19:44:28.195697942+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {
"1344b2ae55eea86d01e0729c892b8115a91efc44de81d54644787afc690f4c2e": {
"Name": "dafu_container",
"EndpointID": "edbe9699cbb6c695d73e1c94f73db5eea3f9e5223be0f83b780bf7cfe0538da4",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"5c692a337f9889704f31516a68bb14318fe2b1adb4b7b633c630cec21cf4191f": {
"Name": "dafu_mysql",
"EndpointID": "94e26d97ff26d9fdf97a93253a6bb0c52392ae4633e2da74946001a82e1e47f8",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
[root@VM-0-16-centos dafufoto]#
接下来再把 django 的配置文件进行两个重要参数的修改,即 mysql的IP地址,以及root的password
将django运行对数据库的所依赖的数据迁移到mysql。
执行 python manage.py migrate 时出错,我们再看看服务器配置是不是有问题。
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '<here is your password>';
[root@VM-0-16-centos ~]# docker exec -it dafu_mysql /bin/bash
root@5c692a337f98:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database dafufoto ;
Query OK, 1 row affected (0.02 sec)
mysql> quit
Bye
root@5c692a337f98:/#
回到 dafu_container进行数据迁移操作。
数据迁移成功。
接下来我们用django自带的测试用的webserver测试一下是否可以用。
80端口已被nginx 占用,所以第一次指定用80测试会报错,第二次指定8000,运行成功。
到此,最后端的django与mysql的配合已经没有问题了。下一节我们将调整uWSGI与nginx两个服务器,用来接管django自带的runserver服务器,实现专业生产服务。
最后修改于:4年前