• openGauss每日一练第4天 | openGauss中一个数据库可以被多个用户访问


    用户和角色有什么区别?
    user 和 role的唯一区别是user是具有login用户属性的,而role默认不带logins属性,可以通过alter role方式添加,在其他方面二者没有区别。

    一、学习目标

    学习openGauss体系结构,使用多个用户访问同一个数据库

    二、课程学习

    创建user1、user2、user3用户,验证数据库musicdb可以被用户user1、user2、user3访问(分别在数据库中创建了一张表、插入数据、进行查询)。即一个数据库可以被多个用户访问。

    三、课程作业

    --测试环境准备
    su - omm
    gsql -r
    --进入数据库omm,创建表空间、测试数据库
    drop DATABASE  IF EXISTS  musicdb;
    drop DATABASE  IF EXISTS  musicdb1;
    drop DATABASE  IF EXISTS  musicdb2;
    drop DATABASE  IF EXISTS  musicdb3;
    drop tablespace IF EXISTS music_tbs;
    
    CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
    CREATE DATABASE musicdb  WITH TABLESPACE = music_tbs;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    实训环境示例:

    root@modb:~# su - omm
    omm@modb:~$ gsql -r
    gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    Type "help" for help.
    
    omm=# drop DATABASE  IF EXISTS  musicdb;
    DROP DATABASE
    omm=# drop DATABASE  IF EXISTS  musicdb1;
    DROP DATABASE
    omm=# drop DATABASE  IF EXISTS  musicdb2;
    DROP DATABASE
    omm=# drop DATABASE  IF EXISTS  musicdb3;
    DROP DATABASE
    omm=# drop tablespace IF EXISTS music_tbs;
    DROP TABLESPACE
    omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
    CREATE DATABASE musicdb  WITH TABLESPACE = music_tbs;CREATE TABLESPACE
    omm=# 
    CREATE DATABASE
    omm=# 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    1. 创建用户user1、user2、user3,授予user1、user2、user3数据库系统的SYSADMIN权限
    --执行下面的SQL语句,创建用户user1、user2、user3:
    
     CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
     CREATE USER user2 IDENTIFIED BY 'kunpeng@1234';
     CREATE USER user3 IDENTIFIED BY 'kunpeng@1234';
     
    --授予user1、user2、user3数据库系统的SYSADMIN权限:
    
    ALTER USER user1 SYSADMIN;
    ALTER USER user2 SYSADMIN;
    ALTER USER user3 SYSADMIN;
    
    --执行下面的命令,查看有哪些用户:
     \du
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    omm=#  CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
    NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
    CREATE ROLE
    omm=#  CREATE USER user2 IDENTIFIED BY 'kunpeng@1234';
    NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
    CREATE ROLE
    omm=#  CREATE USER user3 IDENTIFIED BY 'kunpeng@1234';
    NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
    CREATE ROLE
    omm=# 
    omm=# ALTER USER user1 SYSADMIN;
    ALTER ROLE
    omm=# ALTER USER user2 SYSADMIN;
    ALTER ROLE
    omm=# ALTER USER user3 SYSADMIN;
    ALTER ROLE
    omm=# \du
                                                                  List of roles
     Role name |                                                    Attributes                     
                                   | Member of 
    -----------+-----------------------------------------------------------------------------------
    -------------------------------+-----------
     gaussdb   | Sysadmin                                                                          
                                   | {}
     omm       | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Ope
     user2     | Sysadmin                                                                          
                                   | {}
     user3     | Sysadmin                                                                          
                                   | {}
    
    ratoradmin, Policyadmin, UseFT | {}
     user1     | Sysadmin                                                                          
                                   | {}
    omm=# 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    2. 分别使用user1、user2、user3访问数据库musicdb2,创建各自的表,并插入数据。表名和数据如下:

    表名分别为: products1、 products2、 products3

    字段名数据类型含义
    product_idINTEGER产品编号
    product_nameChar(20)产品名
    categoryChar(30)种类
    向表中插入数据:
    product_idproduct_namecategory
    ---
    1502olympus cameraelectrncs
    1601lamazetoys
    1700wait interfaceBooks
    1666harry pottertoys
    --以用户user1的身份在数据库musicdb2中创建表t1,并插入一条数据:
     \c musicdb2 user1
    create table products1(product_id integer,product_name char(20),category char(30));
    insert into products1(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
    (1601,'lamaze','toys'),
    (1700,'wait interface','Books'),
    (1666,'harry potter','toys');
     
    --以用户user2的身份在数据库musicdb2中创建表t2,并插入一条数据:
      \c musicdb2 user2
    create table products2(product_id integer,product_name char(20),category char(30));
    insert into products2(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
    (1601,'lamaze','toys'),
    (1700,'wait interface','Books'),
    (1666,'harry potter','toys');
    
    
    --以用户user3的身份在数据库musicdb2中创建表t3,并插入一条数据:
      \c musicdb2 user3
    create table products3(product_id integer,product_name char(20),category char(30));
    insert into products3(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
    (1601,'lamaze','toys'),
    (1700,'wait interface','Books'),
    (1666,'harry potter','toys');
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    omm@modb:~$ gsql -r
    gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    Type "help" for help.
    
    omm=#  \c musicdb2 user1
    Password for user user1: 
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    You are now connected to database "musicdb2" as user "user1".
    musicdb2=> create table products1(product_id integer,product_name char(20),category char(30));
    CREATE TABLE
    musicdb2=> insert into products1(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
    musicdb2-> (1601,'lamaze','toys'),
    musicdb2-> (1700,'wait interface','Books'),
    musicdb2-> (1666,'harry potter','toys');
    INSERT 0 4
    musicdb2=> \c musicdb2 user2
    Password for user user2: 
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    You are now connected to database "musicdb2" as user "user2".
    musicdb2=> create table products2(product_id integer,product_name char(20),category char(30));
    CREATE TABLE
    musicdb2=> insert into products2(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
    musicdb2-> (1601,'lamaze','toys'),
    musicdb2-> (1700,'wait interface','Books'),
    musicdb2-> (1666,'harry potter','toys');
    INSERT 0 4
    musicdb2=>   \c musicdb2 user3
    Password for user user3: 
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    You are now connected to database "musicdb2" as user "user3".
    musicdb2=> create table products3(product_id integer,product_name char(20),category char(30));
    CREATE TABLE
    musicdb2=> insert into products3(product_id,product_name,category) values(1502,'olympus camera','electrncs'),
    musicdb2-> (1601,'lamaze','toys'),
    musicdb2-> (1700,'wait interface','Books'),
    musicdb2-> (1666,'harry potter','toys');
    INSERT 0 4
    musicdb2=> 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    3. 使用user1、user2、user3用户中的任何一个,查看当前数据库musicdb2有哪些表
    omm=# \c musicdb2 user3
    Password for user user3: 
    Non-SSL connection (SSL connection is recommended when requiring high-security)
    You are now connected to database "musicdb2" as user "user3".
    musicdb2=> \dt
                               List of relations
     Schema |   Name    | Type  | Owner |             Storage              
    --------+-----------+-------+-------+----------------------------------
     public | products1 | table | user1 | {orientation=row,compression=no}
     public | products2 | table | user2 | {orientation=row,compression=no}
     public | products3 | table | user3 | {orientation=row,compression=no}
    (3 rows)
    
    musicdb2=> 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    Java判断考试成绩程序代码:
    【Unity】Xml的加密读取保存
    基于 outline 实现头像剪裁以及预览
    培训机构招生电子传单制作教程:突出核心竞争力的方法
    Python自学笔记——高级篇(面向对象)
    pycharm无法加载第三方库问题解决
    我服了!SpringBoot升级后这服务我一个星期都没跑起来!(下)
    Rancher-Kubewarden-保姆级教学-含Demo测试
    装饰器模式
    LeakyReLU激活函数
  • 原文地址:https://blog.csdn.net/qq_40220309/article/details/128070670