ORACLE、MySQL、PostgreSQL 客户端 sqlplus,mysql,psql 常用命令对比(一)
写在前面的话:当前根据 摩天轮中国数据库排行 https://www.modb.pro/dbRank 来看,已经有 292 个数据库了,国产数据库遍地开花,如果只掌握一种数据库貌似有点落伍了。
大体梳理了下,目前我有的数据库专业级别(Professional)的证书有 ORACLE 11g OCP(2010)、MySQL 5.7(2019)/8.0(2023) OCP、Tidb PCTP(2022)、HCIP-GaussDB-OLTP(2022);初级级别的有:巨杉数据库、openGauss、MogDB、OceanBase、GoldenDb 总共 9 种数据库了,有云数据库、分布式、集中式、开源数据库等,实际上这些数据库大多都是 paper 能力,目前实际能够支持的也就是 ORACLE 和 MySQL,目前准备实际学习下 PostgreSQL,发现数据库多了,一些命令容易混,还发现 PostgreSQL 的一些操作跟 ORACLE 和 MySQL 有一些不太一样的地方、感觉不太习惯的地方,于是就萌生了总结下这三种数据库的一些对比,先从客户端的常用命令开始吧。
手里正好有一台 ORACLE Cloud 2c12g 的 arm 服务器,看了下目前 MySQL 和 PostgreSQL 的客户端都已经支持了 arm 架构了,但是 ORACLE 目前没用找到 arm 架构的客户端,但是 ORACLE 数据库目前是支持 arm 架构了,但是需要 oel 8.4 及以后的版本,目前我的arm 服务器上安装的是 oel7 的 os,所以本次测试 ORACLE 是在 x86 服务器上测试,MySQL 和 PostgreSQL 是在 arm 架构的服务器上测试。
安装客户端工具
目前三种数据库的客户端工具都支持 rpm
安装
ORACLE 数据库客户端 sqlplus 安装
下载软件包:https://yum.oracle.com/repo/OracleLinux/OL7/oracle/instantclient/x86_64/index.html
wget https://download.oracle.com/otn_software/linux/instantclient/2112000/oracle-instantclient-basic-21.12.0.0.0-1.x86_64.rpm
wget https://download.oracle.com/otn_software/linux/instantclient/2112000/oracle-instantclient-sqlplus-21.12.0.0.0-1.x86_64.rpm
oracle-instantclient-sqlplus 依赖 oracle-instantclient-basic,下载两个软件包,然后通过 yum install *.rpm 安装即可。
[root@tcloud sqlplus]# yum install *.rpm
Loaded plugins: fastestmirror, langpacks
Examining oracle-instantclient-basic-21.12.0.0.0-1.x86_64.rpm: oracle-instantclient-basic-21.12.0.0.0-1.x86_64
Marking oracle-instantclient-basic-21.12.0.0.0-1.x86_64.rpm to be installed
Examining oracle-instantclient-sqlplus-21.12.0.0.0-1.x86_64.rpm: oracle-instantclient-sqlplus-21.12.0.0.0-1.x86_64
Marking oracle-instantclient-sqlplus-21.12.0.0.0-1.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package oracle-instantclient-basic.x86_64 0:21.12.0.0.0-1 will be installed
---> Package oracle-instantclient-sqlplus.x86_64 0:21.12.0.0.0-1 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================
Package Arch Version Repository Size
=============================================================================================================
Installing:
oracle-instantclient-basic x86_64 21.12.0.0.0-1 /oracle-instantclient-basic-21.12.0.0.0-1.x86_64 238 M
oracle-instantclient-sqlplus x86_64 21.12.0.0.0-1 /oracle-instantclient-sqlplus-21.12.0.0.0-1.x86_64 3.2 M
Transaction Summary
=============================================================================================================
Install 2 Packages
Total size: 241 M
Installed size: 241 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : oracle-instantclient-basic-21.12.0.0.0-1.x86_64 1/2
Installing : oracle-instantclient-sqlplus-21.12.0.0.0-1.x86_64 2/2
Verifying : oracle-instantclient-basic-21.12.0.0.0-1.x86_64 1/2
Verifying : oracle-instantclient-sqlplus-21.12.0.0.0-1.x86_64 2/2
Installed:
oracle-instantclient-basic.x86_64 0:21.12.0.0.0-1 oracle-instantclient-sqlplus.x86_64 0:21.12.0.0.0-1
Complete!
[root@tcloud sqlplus]#
[root@tcloud sqlplus]# sqlplus -V
SQL*Plus: Release 21.0.0.0.0 - Production
Version 21.12.0.0.0
MySQL 数据库客户端 mysql 安装
rpm 包下载地址:https://downloads.mysql.com/archives/community/
本次 MySQL 是以 arm 的主机进行演示的,当只下载 mysql-community-client-8.0.33-1.el7.aarch64.rpm
的时候会提示需要依赖,本次测试安装需要 mysql-community-client-plugins
、mysql-community-libs
、mysql-community-common
这三个依赖。下载之后通过 yum install *.rpm 安装即可。
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-community-client-plugins-8.0.33-1.el7.aarch64.rpm
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-community-client-8.0.33-1.el7.aarch64.rpm
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-community-libs-8.0.33-1.el7.aarch64.rpm
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-community-common-8.0.33-1.el7.aarch64.rpm
[root@instance-20211219-1950 mysqlcli]# yum install *.rpm
Loaded plugins: langpacks, ulninfo
Examining mysql-community-client-8.0.33-1.el7.aarch64.rpm: mysql-community-client-8.0.33-1.el7.aarch64
Marking mysql-community-client-8.0.33-1.el7.aarch64.rpm to be installed
Examining mysql-community-client-plugins-8.0.33-1.el7.aarch64.rpm: mysql-community-client-plugins-8.0.33-1.el7.aarch64
Marking mysql-community-client-plugins-8.0.33-1.el7.aarch64.rpm to be installed
Examining mysql-community-common-8.0.33-1.el7.aarch64.rpm: mysql-community-common-8.0.33-1.el7.aarch64
Marking mysql-community-common-8.0.33-1.el7.aarch64.rpm to be installed
Examining mysql-community-libs-8.0.33-1.el7.aarch64.rpm: mysql-community-libs-8.0.33-1.el7.aarch64
Marking mysql-community-libs-8.0.33-1.el7.aarch64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.aarch64 0:8.0.33-1.el7 will be installed
---> Package mysql-community-client-plugins.aarch64 0:8.0.33-1.el7 will be installed
---> Package mysql-community-common.aarch64 0:8.0.33-1.el7 will be installed
---> Package mysql-community-libs.aarch64 0:8.0.33-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================
Package Arch Version Repository Size
=============================================================================================================
Installing:
mysql-community-client aarch64 8.0.33-1.el7 /mysql-community-client-8.0.33-1.el7.aarch64 79 M
mysql-community-client-plugins aarch64 8.0.33-1.el7 /mysql-community-client-plugins-8.0.33-1.el7.aarch64 20 M
mysql-community-common aarch64 8.0.33-1.el7 /mysql-community-common-8.0.33-1.el7.aarch64 10 M
mysql-community-libs aarch64 8.0.33-1.el7 /mysql-community-libs-8.0.33-1.el7.aarch64 7.5 M
Transaction Summary
=============================================================================================================
Install 4 Packages
Total size: 117 M
Installed size: 117 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-client-plugins-8.0.33-1.el7.aarch64 1/4
Installing : mysql-community-common-8.0.33-1.el7.aarch64 2/4
Installing : mysql-community-libs-8.0.33-1.el7.aarch64 3/4
Installing : mysql-community-client-8.0.33-1.el7.aarch64 4/4
Verifying : mysql-community-client-plugins-8.0.33-1.el7.aarch64 1/4
Verifying : mysql-community-client-8.0.33-1.el7.aarch64 2/4
Verifying : mysql-community-common-8.0.33-1.el7.aarch64 3/4
Verifying : mysql-community-libs-8.0.33-1.el7.aarch64 4/4
Installed:
mysql-community-client.aarch64 0:8.0.33-1.el7 mysql-community-client-plugins.aarch64 0:8.0.33-1.el7 mysql-community-common.aarch64 0:8.0.33-1.el7
mysql-community-libs.aarch64 0:8.0.33-1.el7
Complete!
[root@instance-20211219-1950 mysqlcli]# ll
total 21900
-rw-r--r-- 1 root root 16516556 Mar 17 2023 mysql-community-client-8.0.33-1.el7.aarch64.rpm
-rw-r--r-- 1 root root 3670704 Mar 17 2023 mysql-community-client-plugins-8.0.33-1.el7.aarch64.rpm
-rw-r--r-- 1 root root 680276 Mar 17 2023 mysql-community-common-8.0.33-1.el7.aarch64.rpm
-rw-r--r-- 1 root root 1544900 Mar 17 2023 mysql-community-libs-8.0.33-1.el7.aarch64.rpm
[root@instance-20211219-1950 mysqlcli]#
[root@instance-20211219-1950 mysqlcli]# mysql -V
mysql Ver 8.0.33 for Linux on aarch64 (MySQL Community Server - GPL)
PostgreSQL 数据库客户端 psql 安装
官方手册:https://www.postgresql.org/download/linux/redhat/
在我写本文章的时候2023/12/19 ,通过官方的 PostgreSQL Yum Repository 安装命令是无法正常安装的。
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-aarch64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql16-server # 安装数据库
sudo yum install -y postgresql-client
sudo /usr/pgsql-16/bin/postgresql-16-setup initdb
sudo systemctl enable postgresql-16
sudo systemctl start postgresql-16
报错如下:
yum install postgresql16-server
Loaded plugins: langpacks, ulninfo
https://download.postgresql.org/pub/repos/yum/common/redhat/rhel-7Server-aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
也就是 https://download.postgresql.org/pub/repos/yum/common/redhat/rhel-7Server-aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found 这个网页404了。
还是采用rpm 软件包的方式安装:Direct RPM download direct download
我的操作系统是RHEL / CentOS 7 - aarch64:https://download.postgresql.org/pub/repos/yum/common/redhat/rhel-7-aarch64/
wget https://download.postgresql.org/pub/repos/yum/15/redhat/rhel-7-aarch64/postgresql15-15.5-1PGDG.rhel7.aarch64.rpm
wget https://download.postgresql.org/pub/repos/yum/15/redhat/rhel-7-aarch64/postgresql15-libs-15.5-1PGDG.rhel7.aarch64.rpm
yum 安装的时候需要依赖 postgresql15-libs ,两个rpm都下载,然后通过 yum install 安装
[root@instance-20211219-1950 psql]# yum install *.rpm
Loaded plugins: langpacks, ulninfo
Examining postgresql15-15.5-1PGDG.rhel7.aarch64.rpm: postgresql15-15.5-1PGDG.rhel7.aarch64
Marking postgresql15-15.5-1PGDG.rhel7.aarch64.rpm to be installed
Examining postgresql15-libs-15.5-1PGDG.rhel7.aarch64.rpm: postgresql15-libs-15.5-1PGDG.rhel7.aarch64
Marking postgresql15-libs-15.5-1PGDG.rhel7.aarch64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package postgresql15.aarch64 0:15.5-1PGDG.rhel7 will be installed
---> Package postgresql15-libs.aarch64 0:15.5-1PGDG.rhel7 will be installed
--> Finished Dependency Resolution
epel/aarch64/metalink | 7.6 kB 00:00:00
ol7_UEKR6/aarch64 | 3.0 kB 00:00:00
ol7_addons/aarch64 | 3.0 kB 00:00:00
ol7_developer_EPEL/aarch64 | 3.6 kB 00:00:00
ol7_ksplice/aarch64 | 3.0 kB 00:00:00
ol7_latest/aarch64 | 3.6 kB 00:00:00
ol7_oci_included/aarch64 | 2.9 kB 00:00:00
ol7_optional_latest/aarch64 | 3.0 kB 00:00:00
ol7_software_collections/aarch64 | 3.0 kB 00:00:00
https://download.postgresql.org/pub/repos/yum/common/redhat/rhel-7Server-aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7Server-aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-7Server-aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
https://download.postgresql.org/pub/repos/yum/13/redhat/rhel-7Server-aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
https://download.postgresql.org/pub/repos/yum/14/redhat/rhel-7Server-aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
https://download.postgresql.org/pub/repos/yum/15/redhat/rhel-7Server-aarch64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
Dependencies Resolved
=============================================================================================================
Package Arch Version Repository Size
=============================================================================================================
Installing:
postgresql15 aarch64 15.5-1PGDG.rhel7 /postgresql15-15.5-1PGDG.rhel7.aarch64 9.2 M
postgresql15-libs aarch64 15.5-1PGDG.rhel7 /postgresql15-libs-15.5-1PGDG.rhel7.aarch64 1.2 M
Transaction Summary
=============================================================================================================
Install 2 Packages
Total size: 10 M
Installed size: 10 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : postgresql15-libs-15.5-1PGDG.rhel7.aarch64 1/2
Installing : postgresql15-15.5-1PGDG.rhel7.aarch64 2/2
Verifying : postgresql15-15.5-1PGDG.rhel7.aarch64 1/2
Verifying : postgresql15-libs-15.5-1PGDG.rhel7.aarch64 2/2
Installed:
postgresql15.aarch64 0:15.5-1PGDG.rhel7 postgresql15-libs.aarch64 0:15.5-1PGDG.rhel7
Complete!
这里仍然报 repomd.xml: [Errno 14] HTTPS Error 404 - Not Found 但是不影响安装,由于这里需要的rpm包都已经本地下载完成。
[root@instance-20211219-1950 psql]# psql -V
psql (PostgreSQL) 15.5
当然上面只是为了比较而安装,通常情况下我们都已经安装了数据库,这些客户端工具都随着数据库都自动安装了。
总结:三种数据库安装客户端工具都支持通过 rpm 软件包进行安装,需要注意的是rpm包需要依赖。
下面对三种数据库的客户端工具常用操作进行对比。
连接到数据库
连接远程数据库
Oracle sqlplus 客户端连接
sqlplus [username][/password]/@[hostname]:[port]/[DB service name] [AS SYSDBA]
- 普通用户连接数据库(不使用 as sysdba)
sqlplus liups/liups@liups.com:1521/ora19cl
上面是用户 liups
使用密码 liups
连接到 IP 为 liups.com
端口为 1521
,服务名为ora19cl
的 oracle 数据库,如果不写端口号默认是1521,写端口号的方式是 hostname:port
- 特权用户连接数据库(使用 as sysdba)
sqlplus sys/password123@liups.com:1521/ora19cl as sysdba
注意⚠️:
特权用户必须使用 as sysdba
或者as sysoper
选项,否则报 ERROR:ORA-28009: connection as SYS should be as SYSDBA or SYSOPER
以上是使用 [hostname]:[port]/[DB service name]
的方式进行连接,ORACLE 还支持通过tnsname
的方式进行连接。
比如tnsnames.ora
里新增如下 tnsname:dbo19c_high
cat tnsnames.ora
dbo19c_high = (description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=adb.ap-tokyo-1.oraclecloud.com))(connect_data=(service_name=aad4ubqywguykly_dbo19c_high.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))
sqlplus liups/liups@dbo19c_high
sqlplus sys/password123@dbo19c_high as sysdba
ORACLE 可以直接输入sqlplus 后面不带任何参数,然后通过交互的形式输入用户、密码
[oracle@liups:/home/oracle]$ sqlplus
SQL*Plus: Release 19.0.0.0.0 - Production on Wed Dec 20 20:49:17 2023
Version 19.18.0.0.0
Copyright (c) 1982, 2022, Oracle. All rights reserved.
Enter user-name: a