目錄
?一、安裝docker
1.洗掉舊版本(不一定要做,我就沒做這步)
2.安裝yum-utils包并設定存盤庫
3.安裝 Docker 引擎
4.啟動Docker
5.創建docker組并添加用戶
6.設定開機啟動
7.卸載
二、使用Docker部署MySQL資料庫
1.先查找mysql,可以看到很多,
2.選擇第一個,將鏡像拉下來,就鍵入命令
3.接著創建自己的鏡像容器
4.在Windows下 用MySQL Workbench訪問
5.后續再啟動 這個鏡像容器
6.進入容器,并測驗,用MySQL代碼建一個資料庫,
三、使用Docker部署Redis資料庫
1.先搜索并下拉鏡像redis
2.建立自己的容器
3.進入容器中
4.用redis-desktop-manager查看遠程連接
四、使用Docker部署MongoDB資料庫
1.搜索并下拉mongo
2.建立自己的容器,并使用mongo 或 mongosh 登錄mongodb
3.再做一個有密碼的
最近配Linux下的軟體環境有點暈,MySQL怎么也做不出來遠程訪問,突然發現有個叫Docker的好東西,做了點筆記,分享給大家,
系統是Centos8.3,開機進入第二個選項CentOS Stream(4.18.0-240.e18.x86_64) 8,系統之家上面下載的,百度盤鏈接是CentOS-8.3.2011-x86_64-dvd1,用rufus-3.14p制作U盤安裝,我的提取碼都是1234,主要是可以遠程連接,方便本機操作, 系統安裝沒啥好講的,基本配置做完,主要是對幾個 系統盤的空間分配 要大一點,我的上一個Linux系統就是/var/盤太小了,又怎么也調整不過來,就索性借此機會重裝了,操作程序中確保電腦都可以連接互聯網,不然好多東西是下不下來的,
一、安裝docker
安裝操作 官方一點的檔案在https://docs.docker.com/engine/install/centos/#install-docker-engine,里面有各種不同系統的,我篩選我用到的步驟給大家借鑒,
1.洗掉舊版本(不一定要做,我就沒做這步)
?
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
?
2.安裝yum-utils包并設定存盤庫
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
3.安裝 Docker 引擎
sudo yum install docker-ce docker-ce-cli containerd.io
如果提示有沖突不能裝 就把沖突提示里的那個忽略沖突的引數帶上 ,這里官方檔案沒寫,估計不是每個人都能遇到,
4.啟動Docker
sudo systemctl start docker
5.創建docker組并添加用戶
創建docker組
sudo groupadd docker
將您的用戶添加到docker組中
sudo usermod -aG docker $USER
激活對組的更改
newgrp docker
6.設定開機啟動
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
7.卸載
卸載 Docker Engine、CLI 和 Containerd 包
sudo yum remove docker-ce docker-ce-cli containerd.io
洗掉所有鏡像、容器和卷
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
使用sudo -i 進入root模式可以省略不停的敲sudo,
二、使用Docker部署MySQL資料庫
此時要進入root模式,不然沒有權限
1.先查找mysql,可以看到很多,
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11171 [OK]
mariadb MariaDB Server is a high performing open sou… 4242 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 832 [OK]
percona Percona Server is a fork of the MySQL relati… 548 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 90
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 88
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 45
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
prom/mysqld-exporter 40 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 30
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
mysql/mysql-router MySQL Router provides transparent routing be… 21
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
arey/mysql-client Run a MySQL client from a docker container 18 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 15 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
genschsa/mysql-employees MySQL Employee Sample Database 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
jelastic/mysql An image of the MySQL database server mainta… 2
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
monasca/mysql-init A minimal decoupled init container for mysql 0
2.選擇第一個,將鏡像拉下來,就鍵入命令
docker pull mysql
查看自己的鏡像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest c60d96bd2b77 4 days ago 514MB
洗掉掉鏡像使用
docker rmi 鏡像ID
鏡像ID 就是IMAGE ID下面的c60d96bd2b77
3.接著創建自己的鏡像容器
docker run -dit -p 3306:3306 --name mysql1 -e MYSQL_ROOT_PASSWORD=123456 mysql
-itd是配置引數
-d, --detach=false, 指定容器運行于前臺還是后臺,默認為false-i, --interactive=false, 打開STDIN,用于控制臺互動-t, --tty=false, 分配tty設備,該可以支持終端登錄,默認為false
具體其他引數可以參照Docker run引數詳解,
--name是給自己的容器命名,-e配置MySQL的引數,后面的陳述句設定root用戶密碼為123456,最后的mysql是鏡像名稱,
3306:3306 前面的指的是外部訪問用的埠號,后面的指的是Docker內部的MySQL設定的埠號,
查看自己的容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b368477b8670 mysql "docker-entrypoint.s…" 11 hours ago Exited (0) 11 hours ago mysql1
4.在Windows下 用MySQL Workbench訪問

可以更改埠號以區別于服務器系統上原有的MySQL ,
5.后續再啟動 這個鏡像容器
docker start mysql1
停止容器
docker stop mysql1
洗掉容器
docker rm mysql1
6.進入容器,并測驗,用MySQL代碼建一個資料庫,
[root@localhost ~]# docker exec -it mysql1 bash
root@b368477b8670:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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> show databases;
+--------------------+
| Database |
+--------------------+
| amo |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database tt;
Query OK, 1 row affected (0.43 sec)
在Windows下遠程連接也是正常可用的,MySQL8的下載安裝地址及教程

三、使用Docker部署Redis資料庫
1.先搜索并下拉鏡像redis
docker pull redis
2.建立自己的容器
docker run -idt --name redis1 -p 6377:6379 redis
這里用6377映射內部的Redis默認6379埠
3.進入容器中
[root@localhost ~]# docker exec -it redis1 bash
root@2970b297b995:/data# redis-cli
127.0.0.1:6379> select 3
OK
127.0.0.1:6379[3]> hset amo age 18 style handsome
(integer) 2
4.用redis-desktop-manager查看遠程連接


對鏡像的其他操作參照MySQL部分
四、使用Docker部署MongoDB資料庫
1.搜索并下拉mongo
docker pull mongo
2.建立自己的容器,并使用mongo 或 mongosh 登錄mongodb
[root@localhost ~]# docker run -itd --name mongo1 -p 27017:27017 mongo
2e599c6d0a23058ef84a28d4b59a68064a19d63d566e9b3c1f3935909ba86a1a
[root@localhost ~]# docker exec -it mongo1 bash
這個是一個沒密碼的容器
root@2e599c6d0a23:/# mongo
MongoDB shell version v5.0.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("34fb2c20-8287-479a-9921-0670288d8d8f") }
MongoDB server version: 5.0.1
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2021-07-27T02:44:09.374+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-07-27T02:44:09.374+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.runoob.insert({"name":"mao"})
WriteResult({ "nInserted" : 1 })
遠程連接


3.再做一個有密碼的
[root@localhost ~]# docker run -itd --name mongo2 -p 27018:27017 mongo --auth
96cbe1fdb9047fac07d703771234f9caf65f48cdf854bf1e5a08603569b15926
admin 權限登陸,并在資料庫中設定用戶和密碼
[root@localhost ~]# docker exec -it mongo2 mongo admin
MongoDB shell version v5.0.1
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("17e4c9cb-b5ac-4739-9a82-e54d50974d2e") }
MongoDB server version: 5.0.1
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
> db.createUser({ user:'amo',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]})
Successfully added user: {
"user" : "amo",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
"readWriteAnyDatabase"
]
}
查看遠程 連接


mongodb和遠程連接工具在Windows下的安裝,請參考MongoDB安裝,
五、結束語
這里對Centos8下Docker內的MySQL、Redis和MongoDB進行了簡單介紹,實作我目前需要python開發環境配置的基本功能,連接起來很方便,遠程連接更不用這設定那設定的,希望對大家有幫助,
還有就是如果遇到centos開機進入了命令列模式,可以startx進入圖形界面,然后原有的python3不要亂卸載,會回不去圖形界面,并卡在那里,,,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290775.html
標籤:其他


