Docker运行MySQL和Redis
Docker是基于 Linux Container 技术的轻量级虚拟化环境。
Docker 可以让开发者打包他们创建的应用以及相应的依赖包到一个可移植、轻量级的容器中。 Docker 可大大简化在容器中管理应用程序的过程。容器使用沙盒机制,运行在其中的应用与主系统相互分离,类似与虚拟机。 但容器比虚拟机有更棒的可移植性、占用计算机资源更小。
不过,现在基本被k8s
打得找不到北了。Docker比较轻量,平时在本地基于docker部署一些服务做测试还是比较方便的。
下面记录一下Docker部署MySQL,Redis的步骤。
- 从 Docker 下载下来的叫镜像( images )
- 使用docker run 运行起来的镜像( images )叫容器( containers )
pull - download image
Downloading the server image in a separate step is not strictly necessary; however, performing this step before you create your Docker container ensures your local image is up to date.
To download the MySQL Community Edition image, run this command:
docker pull mysql/mysql-server:tag
# docker pull mysql/mysql-server:5.7
The tag is the label for the image version you want to pull (for example, 5.6, 5.7, 8.0, or latest). If :tag is omitted, the latest label is used, and the image for the latest GA version of MySQL Community Server is downloaded.
images - list images
You can list downloaded Docker images with this command:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql/mysql-server 5.7 8b7280d401c0 2 months ago 385MB
redis latest fad0ee7e917a 3 months ago 105MB
run - start MySQL instance
docker run --name=container_name --restart on-failure -d image_name:tag
For example, to start a new Docker container for the MySQL Community Server, use this command:
docker run --name=mysql57 --restart on-failure -d mysql/mysql-server:5.7
Start a container for MySQL with given port 3306
:
docker run --name mysql57 -p 3306:3306 -d mysql/mysql-server:5.7
Start a container for Redis with given port 6379
:
docker run -d --name redis-test -p 6379:6379 redis:latest
ps - list running containers
If the Docker image of the specified name and tag has not been downloaded by an earlier docker pull or docker run command, the image is now downloaded. Initialization for the container begins, and the container appears in the list of running containers when you run the docker ps command. For example:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d974455dd991 mysql/mysql-server:5.7 "/entrypoint.sh mysq…" 16 hours ago Up 16 hours (healthy) 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql57
d40cd9d3e32d redis:latest "docker-entrypoint.s…" 16 hours ago Up 16 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis-test
At local, can lsof
to check if MySQL is listening to the right port:
lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
com.docke 11726 username 81u IPv6 0xa28797e911596843 0t0 TCP *:mysql (LISTEN)
log - show MySQL logs
The -d option used in the docker run command above makes the container run in the background.
Use this command to monitor the output from the container:
docker logs mysql57
Once initialization is finished, the command’s output is going to contain the random password generated for the root user; check the password with, for example, this command:
docker logs mysql57 2>&1 | grep GENERATED
exec - jump into container
To have shell access to your MySQL Server container, use the docker exec -it command to start a bash shell inside the container:
docker exec -it mysql57 bash
bash-4.2#
login MySQL in container
Once the server is ready, you can run the mysql client within the MySQL Server container you just started, and connect it to the MySQL Server. Use the docker exec -it command to start a mysql client inside the Docker container you have started, like the following:
docker exec -it mysql57 mysql -uroot -p
When asked, enter the generated root password (see the last step in Starting a MySQL Server Instance above on how to find the password). Because the MYSQL_ONETIME_PASSWORD option is true by default, after you have connected a mysql client to the server, you must reset the server root password by issuing this statement:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';
Substitute newpassword with the password of your choice. Once the password is reset, the server is ready for use.
grant MySQL privileges
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| healthchecker | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)
mysql> update mysql.user set host='%' where user='root' and host = 'localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
redis container
For enter to container console and access redis, and kind in the console:
docker exec -it redis-test bash
root@d40cd9d3e32d:/data# redis-cli
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
stop - Stopping and Deleting a MySQL Container
docker stop mysql57
To stop and start again the MySQL Server container with a single command:
docker restart mysql57
To delete the MySQL container, stop it first, and then use the docker rm command:
docker stop mysql57
docker rm mysql57
cp - copy files
docker cp <src-path> <container>:<dest-path>
kubectl cp <src-path> <your-pod-name>:<dest-path>