前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用别名简化进入docker容器数据库的操作

利用别名简化进入docker容器数据库的操作

作者头像
Alfred Zhao
发布2023-08-24 17:25:25
1330
发布2023-08-24 17:25:25
举报

之前研究docker和数据库的交互,越发对docker这个东西喜爱了。因为平常偶尔会用到各类数据库测试环境验证一些想法,需要进一步简化进入到这些环境的步骤。

比如我现在有三套docker容器数据库测试环境: MySQL 5.7 MySQL 8.0 Oracle 19.3

使用docker ps查看具体信息:

代码语言:javascript
复制
CONTAINER ID        IMAGE                                                         COMMAND                  CREATED             STATUS                  PORTS                                                                         NAMES
219b2d4fc7c4        container-registry.oracle.com/mysql/community-server:5.7.33   "/entrypoint.sh mysq…"   24 hours ago        Up 24 hours (healthy)   0.0.0.0:32773->3306/tcp, 0.0.0.0:32772->33060/tcp                             mysql57
c4b0fe102401        container-registry.oracle.com/mysql/community-server:8.0.27   "/entrypoint.sh mysq…"   24 hours ago        Up 24 hours (healthy)   0.0.0.0:32771->3306/tcp, 0.0.0.0:32770->33060/tcp, 0.0.0.0:32769->33061/tcp   mysql
576076210b7b        container-registry.oracle.com/database/enterprise:19.3.0.0    "/bin/sh -c 'exec $O…"   25 hours ago        Up 25 hours (healthy)                                                                                 testdb

虽然正常我就可以通过docker exec一条命令,进入对应容器环境的数据库SQL操作界面:

代码语言:javascript
复制
docker exec -it mysql mysql -uroot -p
docker exec -it mysql57 mysql -uroot -p
docker exec -it testdb sqlplus / as sysdba

但总是输入这个也怪麻烦的,懒惰的我就在OCI的opc用户下加几个alias的环境变量。

代码语言:javascript
复制
#alias name='value'
alias mysql='docker exec -it mysql mysql -uroot -p'
alias mysql57='docker exec -it mysql57 mysql -uroot -p'
alias sql='docker exec -it testdb sqlplus / as sysdba'

另外mysql默认有密码:第一次容器运行的mysql,首次登录的密码可以通过docker logs mysql-container-name 查看到,登入后也会强制你修改密码,个人测试环境没必要考虑安全,我就索性选择清除了mysql的密码:

代码语言:javascript
复制
ALTER USER 'root'@'localhost' IDENTIFIED BY '';

一切设置就绪,下次当再登录这个环境,就可以使用我们设置好的别名直接进入对应数据库的SQL操作命令行界面:

代码语言:javascript
复制
[opc@oci-001 ~]$ mysql
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2920
Server version: 8.0.27 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> \q
Bye
[opc@oci-001 ~]$ mysql57
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2883
Server version: 5.7.33 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> \q
Bye
[opc@oci-001 ~]$ sql

SQL*Plus: Release 19.0.0.0.0 - Production on Fri Nov 12 15:55:52 2021
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
[opc@oci-001 ~]$

以后就算维护容器,或是搞坏了某个容器环境重新建立一个,只要名字还维持之前的,就不需要改啥配置。 这简直太方便了,忍不住也记录下。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档