[关闭]
@SovietPower 2022-04-20T16:24:21.000000Z 字数 13182 阅读 804

数据库系统 实验1 安装 命令

DB



某些问题解答:https://support.huaweicloud.com/intl/zh-cn/trouble-dws/dws-trouble.pdf

Docker安装

下载Docker Desktop for Windows:https://hub.docker.com/editions/community/docker-ce-desktop-windows
https://docs.docker.com/desktop/windows/install/
安装wsl:https://docs.microsoft.com/zh-cn/windows/wsl/install

网上教程基本都需要使用Hyper-V,但是Hyper-V会导致虚拟机无法启动(最新版本没事?)和其它问题?
最新的Docker使用Windows 10 Home 64-bit with WSL 2即可启动,不需要Hyper-V,且WSL 2 provides better performance than the legacy Hyper-V backend
安装wsl后,安装Docker Desktop即可。

注意安装前可修改安装位置(默认为C盘)
删除"C:\Program Files\Docker"目录,创建"E:\Programs\Docker"目录,用cmd运行mklink /j "C:\Program Files\Docker" "E:\Programs\Docker"
然后安装docker。
注意powershell不支持mklink,要先在powershell中运行cmd

修改Docker镜像目录
https://www.jianshu.com/p/e79f4c938000

  1. PS E:\> wsl --list -v
  2. NAME STATE VERSION
  3. * Ubuntu Stopped 2
  4. docker-desktop-data Stopped 2
  5. docker-desktop Stopped 2
  6. PS E:\> wsl --export docker-desktop-data "E:\Docker\docker-desktop-data.tar"
  7. PS E:\> dir "E:\Docker"
  8. 目录: E:\Docker
  9. Mode LastWriteTime Length Name
  10. ---- ------------- ------ ----
  11. d----- 2022/3/1 10:39 Image
  12. -a---- 2022/3/1 12:51 665978880 docker-desktop-data.tar
  13. PS E:\> wsl --unregister docker-desktop-data
  14. 正在注销...
  15. PS E:\> wsl --list -v
  16. NAME STATE VERSION
  17. * Ubuntu Stopped 2
  18. docker-desktop Stopped 2
  19. PS E:\> wsl --import docker-desktop-data "E:\Docker" "E:\Docker\docker-desktop-data.tar" --version 2
  20. PS E:\> wsl --list -v
  21. NAME STATE VERSION
  22. * Ubuntu Stopped 2
  23. docker-desktop-data Stopped 2
  24. docker-desktop Stopped 2

备份文件"E:\Docker\docker-desktop-data.tar"可以删除。
导入完成后在新目录下会存在一个ext4.vhdx的虚拟磁盘路径。

安装vim
在root下(而不是omm下):

  1. apt-get update
  2. apt-get install vim

修改容器中文件的方法
https://blog.csdn.net/m0_67391377/article/details/123871347

docker命令

docker run 创建容器
docker ps 列出正在运行的容器。-a列出所有(含未运行的)。
docker start [OPTIONS] [CONTAINER] [CONTAINER...] 运行已停止运行的容器
docker exec -it CONTAINER bash 进入正在运行的容器
docker stop [CONTAINER] 停止正在运行的容器
docker rm [CONTAINER] 删除容器

使用问题

Error response from daemon: open \.\pipe\docker_engine_linux: The system cannot find the file specified.
重新运行了一下命令就好了。

openGuass安装

使用前需开启 Docker Desktop。
安装:
dockerhub可拉取最新的openGauss镜像:

  1. PS E:\> docker pull enmotech/opengauss
  2. Using default tag: latest
  3. latest: Pulling from enmotech/opengauss
  4. 284055322776: Pull complete
  5. a7ca82b898d7: Pull complete
  6. 3842013b7685: Pull complete
  7. 6bc7e92855e3: Pull complete
  8. 39c9c4e5b487: Pull complete
  9. 1f9d76df94b5: Pull complete
  10. 44db1c59ef84: Pull complete
  11. 63ab02376fd3: Pull complete
  12. cf751b0b3be9: Pull complete
  13. 9dc428e2c8b4: Pull complete
  14. Digest: sha256:d5a3e38fa2553a44e7fa1cd5cad0b4f0845a679858764067d7b0052a228578a0
  15. Status: Downloaded newer image for enmotech/opengauss:latest
  16. docker.io/enmotech/opengauss:latest

创建并运行openGuass镜像的一个容器:

  1. PS E:\> docker run --name opengauss --privileged=true -d -e GS_PASSWORD=Password@123 -p 15432:5432 enmotech/opengauss:latest
  2. 0c4bfd4e2ab4769a86a68bf6b9219279b935d010e2e19d076b509c5458e44031
  3. PS E:\> docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 0c4bfd4e2ab4 enmotech/opengauss:latest "entrypoint.sh gauss…" 13 seconds ago Up 5 seconds 0.0.0.0:15432->5432/tcp opengauss
  6. PS E:\> docker exec -it opengauss bash
  7. root@0c4bfd4e2ab4:/# su omm
  8. omm@0c4bfd4e2ab4:/$ ls
  9. bin dev entrypoint.sh home lib64 mnt proc run srv tmp var
  10. boot docker-entrypoint-initdb.d etc lib media opt root sbin sys usr
  11. omm@0c4bfd4e2ab4:/$ exit
  12. exit
  13. root@0c4bfd4e2ab4:/# exit
  14. exit
  15. PS E:\>

第一条,使用enmotech/opengauss:latest镜像,创建并启动一个openGauss数据库的实例,为名为opengauss的容器。
第二条,docker ps查看当前系统中的正在运行的容器。
第三条,在容器内部启动一个Bash Shell 进程,并连接到当前PowerShell窗口。
su omm切换到openGauss的超级用户omm。

在 Docker 容器退出时,默认容器内部的文件系统仍然被保留,以方便调试并保留用户数据。此时容器状态为exited
若要短期运行而不保留数据,可在docker run后加--rm参数。

点击容器可查看详细信息(日志等)。

openGuass命令

gsql命令参数

切换到omm用户:
su omm

使用omm连接:
gsql

使用测试用户连接openGuass数据库(切换到omm后):
gsql -d postgres -U gaussdb -W 'Password@123'

可能的错误:
failed to connect Unknown:5432. 重启一下该容器,再运行。
gsql: FATAL: Invalid username/password,login denied. 未解决
重新建立容器后解决?

使用omm用户连接openGuass数据库(切换到omm后):
gsql -d postgres -r

内部命令
\?:查看openGauss内部命令的帮助信息
\help:查看SQL语句的帮助信息
\l:查看所有数据库对象
\q:关闭
\du:显示所有用户及权限
\c [db_name]:连接另一个数据库
\c - [username]:切换到另一个用户
\d\dt:显示当前数据库中的关系(表)
\di:查看当前数据库中的所有索引对象
\d [table_name]查看表的结构和属性
\dn[S+] [PATTERN] list schemas
\dt[S+] [PATTERN] list tables
\dv[S+] [PATTERN] list views
\sf[+] FUNCNAME show a function's definition

在查看对象的命令后加+,可查看更详细信息。

数据库命令

连接后=#表示输入命令,-#表示继续输入上一行未完成的命令(缺少;)。

创建新用户
create user [username] with password '[password]';
password需用引号包括。至少需有三种字符(如大小写字母+数字)。

修改用户密码
alter user gxb with password 'Gxb11111';
密码不能和之前使用的相同。

授权
usernamepublic时,表示所有用户。
grant all privileges to [username]; 为某用户授予所有权限(仅含增删改查)
grant all privileges on [db_name] [,db_name2, ...] to [username] [,username2, ...]; 为某用户授予某数据库上的所有权限(仅含增删改查)
grant select/insert/update/delete on [db] to [username]; 为某用户授予某数据库上的某些权限
alter role [username] [privilege] 授予某用户某权限(更高级)
[privilege]可为:createdb等。

创建数据库
create database [db_name];
create database [db_name] owner [username]; 指定owner

删除数据库
drop database [db_name];

函数

函数定义

  1. CREATE [OR REPLACE] FUNCTION function_name (arguments)
  2. RETURNS return_datatype AS $variable_name$
  3. DECLARE
  4. declaration;
  5. [...]
  6. BEGIN
  7. < function_body >
  8. [...]
  9. RETURN { variable_name | value }
  10. END; LANGUAGE plpgsql;

function_name:指定函数的名称。
DECLARE:定义参数(参数名写在前面 类型写在后面)。
BEGIN~END: 在中间写方法主体。
RETURN:指定要从函数返回的数据类型(它可以是基础,复合或域类型,或者也可以引用表列的类型)。
LANGUAGE:它指定实现该函数的语言的名称。
例(一般不用$return_value$?):

  1. create or replace function gradeToPoint(in g varchar(2))
  2. returns numeric(2,1) as $$
  3. declare
  4. res numeric(2,1);
  5. begin
  6. select point into res
  7. from grade_point
  8. where grade=g;
  9. return res;
  10. end;
  11. $$ LANGUAGE plpgsql;

视图

更新视图
不能直接更新,必须声明有instead of的触发器,指定insert等时要干什么。然后可以返回新的faculty值(不能返回原来的faculty),也可以不返回。
例:

  1. create view faculty as
  2. select ID, name, dept_name
  3. from instructor;
  4. create or replace rule rl_faculty_insert as
  5. on insert to faculty do instead
  6. insert into instructor values(new.ID, new.name, new.dept_name)
  7. returning instructor.ID, instructor.name, instructor.dept_name;
  8. create or replace rule r1_CSinstructors_insert as
  9. on insert to CSinstructors do instead
  10. insert into instructor values(new.ID, new.name, new.dept_name, new.salary);

update例:

  1. CREATE OR REPLACE RULE rl_test_view_update AS
  2. ON UPDATE TO test_view DO INSTEAD
  3. UPDATE test SET a = NEW.a, b = NEW.b
  4. WHERE test.a = old.a AND test.b = old.b;

创建并更新数据库

创建数据库

先新建一个用户。远程登录不允许使用omm用户?
然后创建数据库,并授予新用户权限。

  1. openGauss=# create user gxb with password "Gxb12345";
  2. CREATE ROLE
  3. openGauss=# create database db2022 owner gxb;
  4. CREATE DATABASE
  5. openGauss=# grant all privileges to GXB
  6. openGauss-# ;
  7. ALTER ROLE
  8. openGauss=# \c db2022
  9. Non-SSL connection (SSL connection is recommended when requiring high-security)
  10. You are now connected to database "db2022" as user "omm".
  11. db2022=# \du
  12. List of roles
  13. Role name | Attributes
  14. | Member of
  15. -----------+------------------------------------------------------------------------------------------------------------
  16. ------+-----------
  17. gxb | Sysadmin
  18. | {}
  19. omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin,
  20. UseFT | {}
  21. db2022=# \c - gxb
  22. Password for user gxb:
  23. Non-SSL connection (SSL connection is recommended when requiring high-security)
  24. You are now connected to database "db2022" as user "gxb".
  25. db2022=> \l
  26. List of databases
  27. Name | Owner | Encoding | Collate | Ctype | Access privileges
  28. -----------+-------+----------+-------------+-------------+-------------------
  29. db2022 | gxb | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/gxb +
  30. | | | | | gxb=CTc/gxb +
  31. | | | | | gxb=APm/gxb
  32. postgres | | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
  33. template0 | | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
  34. | | | | | omm=CTc/omm
  35. template1 | | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
  36. | | | | | omm=CTc/omm
  37. (6 rows)

更新数据库

直接输入SQL语句。
创建表

  1. db2022=> \d
  2. No relations found.
  3. db2022=> create table classroom
  4. db2022-> (building varchar(15),
  5. db2022(> room_number varchar(7),
  6. db2022(> capacity numeric(4,0),
  7. db2022(> primary key (building, room_number)
  8. db2022(> );
  9. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "classroom_pkey" for table "classroom"
  10. CREATE TABLE
  11. db2022=> create table department
  12. db2022-> (dept_name varchar(20),
  13. db2022(> building varchar(15),
  14. db2022(> budget numeric(12,2) check (budget > 0),
  15. db2022(> primary key (dept_name)
  16. db2022(> );
  17. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "department_pkey" for table "department"
  18. CREATE TABLE
  19. db2022=> \d
  20. List of relations
  21. Schema | Name | Type | Owner | Storage
  22. --------+------------+-------+-------+----------------------------------
  23. public | classroom | table | gxb | {orientation=row,compression=no}
  24. public | department | table | gxb | {orientation=row,compression=no}
  25. (2 rows)
  26. ...

插入数据

  1. db2022=> insert into classroom values ('Packard', '101', '500');
  2. INSERT 0 1
  3. db2022=> insert into classroom values ('Painter', '514', '10');
  4. INSERT 0 1
  5. db2022=> insert into classroom values ('Taylor', '3128', '70');
  6. INSERT 0 1
  7. ...

查询数据库

  1. db2022=> select * from advisor;
  2. s_id | i_id
  3. -------+-------
  4. 00128 | 45565
  5. 12345 | 10101
  6. 23121 | 76543
  7. 44553 | 22222
  8. 45678 | 22222
  9. 76543 | 45565
  10. 76653 | 98345
  11. 98765 | 98345
  12. 98988 | 76766
  13. (9 rows)

查看数据库信息
\d查看当前数据库所有数据表对象:

  1. db2022=> \d
  2. List of relations
  3. Schema | Name | Type | Owner | Storage
  4. --------+------------+-------+-------+----------------------------------
  5. public | advisor | table | gxb | {orientation=row,compression=no}
  6. public | classroom | table | gxb | {orientation=row,compression=no}
  7. public | course | table | gxb | {orientation=row,compression=no}
  8. public | department | table | gxb | {orientation=row,compression=no}
  9. public | instructor | table | gxb | {orientation=row,compression=no}
  10. public | prereq | table | gxb | {orientation=row,compression=no}
  11. public | section | table | gxb | {orientation=row,compression=no}
  12. public | student | table | gxb | {orientation=row,compression=no}
  13. public | takes | table | gxb | {orientation=row,compression=no}
  14. public | teaches | table | gxb | {orientation=row,compression=no}
  15. public | time_slot | table | gxb | {orientation=row,compression=no}
  16. (11 rows)

\d [table_name]查看表的结构和属性。
\d+ [table_name]查看更详细的表的结构和属性。

  1. db2022=> \d+ advisor
  2. Table "public.advisor"
  3. Column | Type | Modifiers | Storage | Stats target | Description
  4. --------+----------------------+-----------+----------+--------------+-------------
  5. s_id | character varying(5) | not null | extended | |
  6. i_id | character varying(5) | | extended | |
  7. Indexes:
  8. "advisor_pkey" PRIMARY KEY, btree (s_id) TABLESPACE pg_default
  9. Foreign-key constraints:
  10. "advisor_i_id_fkey" FOREIGN KEY (i_id) REFERENCES instructor(id) ON DELETE SET NULL
  11. "advisor_s_id_fkey" FOREIGN KEY (s_id) REFERENCES student(id) ON DELETE CASCADE
  12. Has OIDs: no
  13. Options: orientation=row, compression=no

\di:查看当前数据库中的所有索引对象。

  1. db2022=> \di
  2. List of relations
  3. Schema | Name | Type | Owner | Table | Storage
  4. --------+-----------------+-------+-------+------------+---------
  5. public | advisor_pkey | index | gxb | advisor |
  6. public | classroom_pkey | index | gxb | classroom |
  7. public | course_pkey | index | gxb | course |
  8. public | department_pkey | index | gxb | department |
  9. public | instructor_pkey | index | gxb | instructor |
  10. public | prereq_pkey | index | gxb | prereq |
  11. public | section_pkey | index | gxb | section |
  12. public | student_pkey | index | gxb | student |
  13. public | takes_pkey | index | gxb | takes |
  14. public | teaches_pkey | index | gxb | teaches |
  15. public | time_slot_pkey | index | gxb | time_slot |
  16. (11 rows)

-q退出数据库。

使用Data Studio

Data Studio为openGauss官方图形客户端工具。
按照如下输入新建连接:

无法连接外部数据库

server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.

https://stackoverflow.com/questions/9764126/how-to-configure-postgresql-postgresql-conf-listen-addresses-for-multiple-ip-add#

没有将外部的IP添加到配置listen_addresses中,所以会不监听外部的请求。
gsql连接任一数据库,用show listen_addresses;查看当前配置,默认只有localhost(PostgreSQL数据库为了安全,它不会监听除本地以外的所有连接请求)。

通过alter system set listen_addresses = '*';将其改为允许任意IP访问。
或是直接修改文件/var/lib/opengauss/data/postgresql.conf,改为listen_addresses = '*'

FATAL: no pg_hba.conf entry for host "172.17.0.1".
修改listen_addresses后又出现了该错误。
没有配置pg_hba。除了上述的修改监听地址外,还需在此添加允许建立连接的地址。
修改文件/var/lib/opengauss/data/pg_hba.conf
# IPv4 local connections:下面,添加host all all 172.17.0.1/32 md5md5表示用密码连接,也可以用sha256,改为trust表示不需要密码(但数据库禁止openGauss内部其他节点无认证接入,除本地外不能用trust,否则会出Forbid remote connection with trust method)。
或者直接:

  1. vim /var/lib/opengauss/data/pg_hba.conf
  2. #TYPE DATABASE USER ADDRESS METHOD
  3. host all all 0.0.0.0/0 md5

表示允许所有主机通过TCP/IP建立连接,认证方式为MD5,允许使用所有用户,连接所有的数据库。

fe_ sendauth: invalid authentication request from server: AUTH REQ SASL CONT
omm用户进入数据库,使用alter system set password_encryption_type=1;
然后使用其它用户连接数据库(不允许使用omm远程登录)。

然后需要重启容器!

none of the server's SASL authentication mechanisms are supported
alter system set password_encryption_type=1;password_encryption_type设为0或1。
然后更新一下登录用户的密码。

其他错误

切换用户时 No passwd entry for user
https://unix.stackexchange.com/questions/308668/using-su-to-change-user-gives-no-passwd-entry-for-user
不存在当前用户。可以通过cut -d: -f1 /etc/passwd查看用户,adduser [name]添加用户。


将Docker容器保存为镜像文件,并重新创建容器

docker commit opengauss mygauss:2022-03-14 将容器opengauss打包成名为mygauss的镜像。
docker images 查看本地仓库中的镜像。
docker save -o mygauss.tar mygauss 将镜像保存为文件。镜像文件可以用于在不同系统间实现迁移镜像。
docker image rm mygauss:2022-03-14 将本地仓库中的mygauss镜像删除。
docker load --input mygauss.tar 将镜像文件mygauss.tar加载到本地仓库。

  1. PS H:\> docker commit opengauss mygauss:2022-03-14
  2. sha256:b1d3144a639eb7a347ff1c2206baba702e2f43f83c02cda00ae41aa50247daa3
  3. PS H:\> docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. mygauss 2022-03-14 b1d3144a639e About a minute ago 1.21GB
  6. enmotech/opengauss latest b4dd24d09223 5 months ago 383MB
  7. PS H:\> docker save -o mygauss.tar mygauss

根据打包的镜像,创建新的一样的容器:

  1. PS H:\> docker run --name opengauss3 --privileged=true -d -e GS_PASSWORD=Password@123 -p 15432:5432 mygauss:2022-03-14
  2. 97375c3acbd1046ba2b6ccf5d24b7e758bab7bcf067c0869f745d6acfb125b8f
  3. PS H:\> docker exec -it opengauss3 bash
  4. root@97375c3acbd1:/# su - omm
  5. omm@97375c3acbd1:~$ gsql
  6. gsql: FATAL: database "omm" does not exist
  7. omm@97375c3acbd1:~$ gsql -d postgres -r
  8. gsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )
  9. Non-SSL connection (SSL connection is recommended when requiring high-security)
  10. Type "help" for help.
  11. openGauss=# \l
  12. List of databases
  13. Name | Owner | Encoding | Collate | Ctype | Access privileges
  14. -----------+-------+----------+-------------+-------------+-------------------
  15. db2022 | gxb | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/gxb +
  16. | | | | | gxb=CTc/gxb +
  17. | | | | | gxb=APm/gxb
  18. postgres | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
  19. template0 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
  20. | | | | | omm=CTc/omm
  21. template1 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
  22. | | | | | omm=CTc/omm
  23. (4 rows)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注