【docker】mysql数据库镜像制作与应用

1.准备MySQL软件 

mkdir -p /data/mysql 
cd /data/mysql 
--准备如下文件
[root@es1 mysql]# ll
total 262096
-rw-r--r-- 1 root root      2523 Jul  5 19:02 Centos-7.repo
drwxr-xr-x 2 root root         6 Jul  5 18:58 data
-rw-r--r-- 1 root root        63 Jul  5 18:59 docker-build-mysql.sh
-rw-r--r-- 1 root root      1448 Jul  5 18:59 dockerfile-mysql.yml
-rw-r--r-- 1 root root      2409 Jul  5 18:57 my.cnf
-rw-r--r-- 1 root root       981 Jul  5 19:25 mysql-config.sh
-rw-r--r-- 1 root root 188841984 Jul  5 18:59 mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
-rw-r--r-- 1 root root       137 Jul  5 18:58 mysqlstart.sh

cat > docker-build-mysql.sh << EOF
EOF


2.创建镜像,初始化数据库。

./docker-build-mysql.sh
.........................
Successfully built 677788b528f9
Successfully tagged mysql:5.7.37

3.创建卷

docker volume create mysqldata
ls /data/docker/data/volumes/mysqldata/_data/

4.运行容器:将数据初始化到:mysqldata 卷下。

docker run -it -d --name mysql3306 -h mysql3306 \
--privileged --mount-p 3306:3306 mysql:5.7.37

[root@es1 volumes]# docker ps 
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS         PORTS                                       NAMES
9a78ba8e5527   mysql:5.7.37   "sh /usr/local/mysql…"   10 seconds ago   Up 4 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   mysql3306


docker exec -it mysql3306 bash
ps -ef|grep mysql
--设置socket的连接。
[root@mysql3306 data]# ln -sv /usr/local/mysql/data/mysql.sock /tmp/mysql.sock
‘/tmp/mysql.sock’ -> ‘/usr/local/mysql/data/mysql.sock’

--查看MySQL初始化密码。
[root@mysql3306 data]# cat error.log |grep password
2024-07-05T20:00:07.575915+08:00 1 [Note] A temporary password is generated for root@localhost: m,#2Ro:)b&gT
2024-07-05T20:09:57.220435+08:00 2 [Note] Access denied for user 'root'@'localhost' (using password: YES)

[root@mysql3306 data]# mysql -uroot -p'm,#2Ro:)b&gT'
mysql> alter user user() identified by 'rootroot';
Query OK, 0 rows affected (0.00 sec)
mysql> show databases; 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
--创建远程连接用户。
mysql> create user root@'%' identified by 'rootroot';
Query OK, 0 rows affected (0.00 sec)

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

--远程连接访问。
[root@es1 volumes]# mysql -uroot -prootroot -h192.168.1.7 -e "show databases"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sspudb             |
| sys                |
+--------------------+

5.删除容器,重新创建容器

docker rm -f mysql3306
docker run -it -d --name mysql3306 -h mysql3306 \
--mount src=mysqldata,-p 3306:3306 mysql:5.7.37

--删除容器,再创建容器会使用原来的数据。
[root@es1 data]# docker run -it -d --name mysql3306 -h mysql3306 \
> --mount src=mysqldata,dst=/usr/local/mysql/data -p 3306:3306 mysql:5.7.37
cc330f7a0a30c692b28cef29ec9a16abb75d75f6b1e29a4ac5613174bcb009a2
[root@es1 data]# 
[root@es1 data]# 
[root@es1 data]# 
[root@es1 data]# mysql -uroot -prootroot -h192.168.1.7 -e "show databases;"
Logging to file '/data/mysql/logs/query.log'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sspudb             |
| sys                |
+--------------------+

6.将镜像上传到远程Harbor 

docker tag mysql:5.7.37 192.168.1.11:443/myharbor/mysql:5.7.37
docker push 192.168.1.11:443/myharbor/mysql:5.7.37
[root@es1 data]# docker push 192.168.1.11:443/myharbor/mysql:5.7.37
The push refers to repository [192.168.1.11:443/myharbor/mysql]
802570c1ae29: Pushed 
f57032be20f3: Pushed 
3275e129f0c0: Pushed 
636bcf5d940d: Pushed 
4716215b96df: Pushed 
5194520e8637: Pushing  880.9MB/2.688GB
ed509e59793f: Pushing  817.4MB/2.666GB
8d5761c88604: Pushing  787.9MB/2.666GB
abd71516e14d: Pushed 
3501e53fe1c2: Pushed 
53c61a520c85: Mounted from myharbor/nginx 
88765da43086: Mounted from myharbor/nginx 
2ca40431ab7d: Mounted from myharbor/nginx 
caceb42f09c2: Mounted from myharbor/nginx 
5b1d42135d46: Mounted from myharbor/nginx 
174f56854903: Mounted from myharbor/nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值