代码改变世界

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

2021-11-13 00:17  AlfredZhao  阅读(52)  评论(0编辑  收藏  举报

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

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

使用docker ps查看具体信息:

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操作界面:

docker exec -it mysql mysql -uroot -p
docker exec -it mysql57 mysql -uroot -p
docker exec -it testdb sqlplus / as sysdba

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

#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的密码:

ALTER USER 'root'@'localhost' IDENTIFIED BY '';

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

[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 ~]$

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

AlfredZhao©版权所有「从Oracle起航,领略精彩的IT技术。」