docker快速部署Redis、MySQL、Nginx

文章目录

  • docker部署redis
  • docker部署mysql
  • docker部署nginx


docker部署redis

  1. 拉取镜像
docker pull redis:7.0.10
  1. 拷贝一份redis.conf文件复制到/home/ubuntu/redis文件夹下,修改redis.conf配置文件
vim redis.conf
-----------------------------------------------------
# 内容如下所示
#开启持久化
appendonly yes
port 6379
#设置密码
requirepass 1234
#允许访问的地址,默认是127.0.0.1,会导致只能在本地访问。修改为0.0.0.0则可以在任意IP访问,生产环境不要设置为0.0.0.0
bind 0.0.0.0
#默认yes,要改为no,也是是保护模式,限制为本地访问,修改为no后解除保护模式
protected-mode no
# 守护进程,修改为yes后即可后台运行,但是这边需要设置为no
daemonize no
  1. 创建redis容器
docker run -d -p 6379:6379 --restart=always \
-v /home/ubuntu/redis/redis.conf:/etc/redis/redis.conf \
-v /home/ubuntu/redis/data:/data \
--name redis redis:7.0.10 \
redis-server /etc/redis/redis.conf

-------------注释------------------------
docker run -itd --name redis -p 6379:6379 \
--restart=always \
-v /home/xt/redis/redis.conf:/etc/redis/redis.conf \
-v /home/xt/redis/data:/data \
redis redis-server /etc/redis/redis.conf 

-p 6379:6379:把容器内的6379端口映射到宿主机6379端口
–restart=always:启动docker时启动该容器
-v /home/xt/redis/redis.conf:/etc/redis/redis.conf:把宿主机配置好的redis.conf放到容器内的这个位置中
-v /home/xt/redis/data:/data:把redis持久化的数据在宿主机内显示,做数据备份
redis-server /etc/redis/redis.conf:按照这个redis.conf的配置启动
----------注释--------------------------------

docker部署mysql

MySQL 8.0.30

  • 拉取镜像
docker pull mysql:8.0.30
  • 创建容器
systemctl stop mysqld#先暂停mysql服务
docker run -d --name mysql -p 3306:3306 -v mysql_data:/var/lib/mysql -v mysql_conf:/etc/mysql --restart=always --privileged=true -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=1234 mysql:8.0.30#添加一段时区

docker安装完成mysql8,如果使用sqlyog或者navite连接,需要修改密码加密规则,因为低版本客户端工具不支持mysql8最新的加密规则。如果使用客户端连接,需要修改:

  • docker exec 进入mysql容器
docker exec -it mysql /bin/bash
  • mysql -uroot -p 登录你的 MySQL 数据库(密码:1234),然后 执行这条SQL:
mysql -u root -p
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '1234';
ubuntu@VM-4-2-ubuntu:~$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
ubuntu@VM-4-2-ubuntu:~$ sudo docker run -d --name mysql -p 3306:3306 -v mysql_data:/var/lib/mysql -v mysql_conf:/etc/mysql --restart=always --privileged=true -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=1234 mysql:8.0.30
282bf80df823e4533b3220b53ec9551dba9bf462e3b4cb75c628c9eb2452f72b
ubuntu@VM-4-2-ubuntu:~$ sudo docker exec -it mysql /bin/bash
bash-4.4# mysql -u root -p
Enter password: #(注意!!! 这里填docker run时设置的mysql密码——1234)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '1234';
Query OK, 0 rows affected (0.00 sec)

mysql> 

bug:连接不上 docker中的mysql

解决:呃呃呃,在服务器防火墙配置3306端口对外开放…

bug:Access denied for user ‘root’@‘124.160.200.116’ (using password: YES)

原因:mysql中root权限为localhost,需要创建root@%

参考博客:MySQL数据库创建用户root@%

create user 'root'@'%' identified with mysql_native_password by '1234';

ubuntu@VM-4-2-ubuntu:~$ sudo docker exec -it mysql /bin/bash
bash-4.4# use mysql;
bash: use: command not found
bash-4.4# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.30 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SELECT User, Host FROM mysql.user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| server           | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

mysql> CREATE USER 'root'@'%' IDENTIFIED BY '1234';
ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'%'
mysql> drop user 'root'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> create user 'root'@'%' identified with mysql_native_password by '1234';
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT User, Host FROM mysql.user;
+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| root             | %         |
| server           | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)


mysql> grant all on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

docker部署nginx

参考:docker部署nginx

1.拉取镜像

docker pull nginx

2.启动nginx容器(工具人)

docker run --restart=always --name=nginx -p 80:80 -d nginx

3.访问测试

访问地址:http://ip+port

4.挂载准备

  • 宿主机创建挂载目录

    mkdir /home/ubuntu/nginx -p
    
  • 复制配置文件到宿主机

    docker cp nginx:/etc/nginx /home/ubuntu/nginx/conf
    docker cp nginx:/usr/share/nginx/html /home/ubuntu/nginx/html
    docker cp nginx:/var/log/nginx /home/ubuntu/nginx/logs
    
  • 删除之前创建的nginx

    docker stop nginx
    docker rm nginx
    

5.挂载启动nginx容器

docker run --restart=always --name=nginx -p 80:80 \
-v /home/ubuntu/nginx/conf:/etc/nginx \
-v /home/ubuntu/nginx/html:/usr/share/nginx/html \
-v /home/ubuntu/nginx/logs:/var/log/nginx \
-d nginx

6.修改/home/ubuntu/nginx/html/index.html,访问测试

访问地址:http://ip+port

注意:在上述操作步骤中,我只将80端口与宿主机进行了映射,也就意味着只能使用80端口访问nginx,如果要使用nginx启动多个前端项目的时候,则需要将容器里的多个端口映射到宿主机。有两种方式可以实现。

参考:

【Docker】docker安装nginx及端口映射相关配置

你可能感兴趣的:(Linux,docker,redis,mysql,nginx,linux,服务器,微服务)