Docker笔记2--mysql服务

公司准备 上云,所以 先从一个mysql的镜像服务开始吧!

[root@muze1 ~]# docker search mysql

[root@muze1 ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
69692152171a: Already exists
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
0f86c95aa242: Pull complete
37ba2d8bd4fe: Pull complete
6d278bb05e94: Pull complete
497efbd93a3e: Pull complete
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[root@muze1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest d1a364dc548d 8 days ago 133MB
tomcat latest c43a65faae57 2 weeks ago 667MB
mysql latest c0cdc95609f1 3 weeks ago 556MB
lhrbest/lhrcentos76 8.2 14ab81d0ca62 7 weeks ago 3.41GB
iwanttobefreak/weblogic1036 latest a54169dcf667 4 years ago 783MB
jaspeen/oracle-11g latest 0c8711fe4f0f 5 years ago 281MB
ismaleiva90/weblogic12 latest 84795663769d 5 years ago 3.65GB
[root@muze1 ~]#

[root@muze1 ~]# docker run -d --name zxk-mysql -e MYSQL_ROOT_PASSWORD=my123456 -p 13306:3306 mysql
34ad55909777c201800d445cdce0e3ea7521e3d9768bd70699da9d8bc3ad9cf3

此处的-e  环境变量指定MySQL的root 密码。

把容器的mysql端口3306映射到宿主机的13306端口,这样想访问mysql就可以直接访问宿主机的13306端口。

查看mysql 容器启动日志:

[root@muze1 ~]# docker logs 34ad
2021-06-03 09:20:12+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.25-1debian10 started.
2021-06-03 09:20:12+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-06-03 09:20:12+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.25-1debian10 started.
2021-06-03 09:20:12+00:00 [Note] [Entrypoint]: Initializing database files
2021-06-03T09:20:12.598950Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.25) initializing of server in progress as process 39
2021-06-03T09:20:12.604339Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-06-03T09:20:13.902652Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-06-03T09:20:15.683511Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2021-06-03 09:20:17+00:00 [Note] [Entrypoint]: Database files initialized
2021-06-03 09:20:17+00:00 [Note] [Entrypoint]: Starting temporary server
2021-06-03T09:20:18.607117Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.25) starting as process 84
2021-06-03T09:20:18.686988Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-06-03T09:20:19.519816Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-06-03T09:20:19.724672Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2021-06-03T09:20:20.133537Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-06-03T09:20:20.133721Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2021-06-03T09:20:20.134855Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2021-06-03T09:20:20.153056Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.25' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL.
2021-06-03 09:20:20+00:00 [Note] [Entrypoint]: Temporary server started.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

2021-06-03 09:20:22+00:00 [Note] [Entrypoint]: Stopping temporary server
2021-06-03T09:20:22.495921Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.25).
2021-06-03T09:20:23.294208Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.25) MySQL Community Server - GPL.
2021-06-03 09:20:23+00:00 [Note] [Entrypoint]: Temporary server stopped

2021-06-03 09:20:23+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.

2021-06-03T09:20:23.806554Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.25) starting as process 1
2021-06-03T09:20:23.815366Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-06-03T09:20:24.245210Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-06-03T09:20:24.391344Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2021-06-03T09:20:24.529547Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-06-03T09:20:24.529703Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2021-06-03T09:20:24.531524Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2021-06-03T09:20:24.550273Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.25' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.

日志看应该没啥问题,Ready for  Connections 

进容器登陆mysql 瞅瞅

 查看MySQL的数据文件目录如下:

root@34ad55909777:/var/lib# cd mysql/
root@34ad55909777:/var/lib/mysql# ls
'#ib_16384_0.dblwr' auto.cnf binlog.index client-cert.pem ib_logfile0 ibtmp1 performance_schema server-cert.pem undo_001
'#ib_16384_1.dblwr' binlog.000001 ca-key.pem client-key.pem ib_logfile1 mysql private_key.pem server-key.pem undo_002
'#innodb_temp' binlog.000002 ca.pem ib_buffer_pool ibdata1 mysql.ibd public_key.pem sys
root@34ad55909777:/var/lib/mysql# pwd
/var/lib/mysql

 容器删除后,保存在容器里面的数据就丢失了。因此需要将容器的数据目录映射到宿主机的目录上,以保障数据的安全性。

[root@muze1 data]# mkdir -pv /data/mytest
mkdir: created directory ‘/data/mytest’

 验证进到容器能正常登陆mysql:

[root@muze1 data]# docker exec -it fd453 /bin/bash
root@fd453e0604ba:/# mysql -uroot -pmy123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.25 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> exit

mysql 容器里的数据文件映射到本地宿主机上对应的目录。

 通过客户端能连接到mysql服务上:

原文地址:https://www.cnblogs.com/vzhangxk/p/14846287.html