docker部署redis
- 拉取鏡像
docker pull redis:7.0.10
- 拷貝一份redis.conf文件復(fù)制到/home/ubuntu/redis文件夾下,修改redis.conf配置文件
vim redis.conf
-----------------------------------------------------
# 內(nèi)容如下所示
#開啟持久化
appendonly yes
port 6379
#設(shè)置密碼
requirepass 1234
#允許訪問的地址,默認(rèn)是127.0.0.1,會(huì)導(dǎo)致只能在本地訪問。修改為0.0.0.0則可以在任意IP訪問,生產(chǎn)環(huán)境不要設(shè)置為0.0.0.0
bind 0.0.0.0
#默認(rèn)yes,要改為no,也是是保護(hù)模式,限制為本地訪問,修改為no后解除保護(hù)模式
protected-mode no
# 守護(hù)進(jìn)程,修改為yes后即可后臺運(yùn)行,但是這邊需要設(shè)置為no
daemonize no
- 創(chuàng)建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:把容器內(nèi)的6379端口映射到宿主機(jī)6379端口
–restart=always:啟動(dòng)docker時(shí)啟動(dòng)該容器
-v /home/xt/redis/redis.conf:/etc/redis/redis.conf:把宿主機(jī)配置好的redis.conf放到容器內(nèi)的這個(gè)位置中
-v /home/xt/redis/data:/data:把redis持久化的數(shù)據(jù)在宿主機(jī)內(nèi)顯示,做數(shù)據(jù)備份
redis-server /etc/redis/redis.conf:按照這個(gè)redis.conf的配置啟動(dòng)
----------注釋--------------------------------
docker部署mysql
MySQL 8.0.30
- 拉取鏡像
docker pull mysql:8.0.30
- 創(chuàng)建容器
systemctl stop mysqld#先暫停mysql服務(wù)
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#添加一段時(shí)區(qū)
docker安裝完成mysql8,如果使用sqlyog或者navite連接,需要修改密碼加密規(guī)則,因?yàn)榈桶姹究蛻舳斯ぞ卟恢С謒ysql8最新的加密規(guī)則。如果使用客戶端連接,需要修改:
- docker exec 進(jìn)入mysql容器
docker exec -it mysql /bin/bash
- mysql -uroot -p 登錄你的 MySQL 數(shù)據(jù)庫(密碼:1234),然后 執(zhí)行這條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時(shí)設(shè)置的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
解決:呃呃呃,在服務(wù)器防火墻配置3306端口對外開放…
bug:Access denied for user ‘root’@‘124.160.200.116’ (using password: YES)
原因:mysql中root權(quán)限為localhost,需要?jiǎng)?chuàng)建
root@%
參考博客:MySQL數(shù)據(jù)庫創(chuàng)建用戶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.啟動(dòng)nginx容器(工具人)
docker run --restart=always --name=nginx -p 80:80 -d nginx
3.訪問測試
訪問地址:http://ip+port
4.掛載準(zhǔn)備
-
宿主機(jī)創(chuàng)建掛載目錄
mkdir /home/ubuntu/nginx -p
-
復(fù)制配置文件到宿主機(jī)
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
-
刪除之前創(chuàng)建的nginx
docker stop nginx docker rm nginx
5.掛載啟動(dòng)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端口與宿主機(jī)進(jìn)行了映射,也就意味著只能使用80端口訪問nginx,如果要使用nginx啟動(dòng)多個(gè)前端項(xiàng)目的時(shí)候,則需要將容器里的多個(gè)端口映射到宿主機(jī)。有兩種方式可以實(shí)現(xiàn)。
參考:文章來源:http://www.zghlxwxcb.cn/news/detail-767806.html
【Docker】docker安裝nginx及端口映射相關(guān)配置文章來源地址http://www.zghlxwxcb.cn/news/detail-767806.html
到了這里,關(guān)于docker快速部署Redis、MySQL、Nginx的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!