[openGauss 学废系列]- openGauss体系结构-多个用户访问同一个数据库 ...

打印 上一主题 下一主题

主题 835|帖子 835|积分 2505

一、学习目的

   本节课是本次实训的第四次训练课,本节课的学习学习目的仍然是熟悉openGauss体系结构,能利用多个用户访问同一个数据库。
重点是:
1、怎样删除\创建数据库
2、怎样删除\创建表空间
3、怎样创建用户
4、怎样为用户授权
5、怎样多个用户访问同一个数据库
  二、测试环境准备

2.1 删除已有数据库及表空间

  1. root@modb:~# su - omm
  2. omm@modb:~$ gsql -r
  3. gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
  4. Non-SSL connection (SSL connection is recommended when requiring high-security)
  5. Type "help" for help.
  6. omm=# drop DATABASE  IF EXISTS  musicdb;
  7. NOTICE:  database "musicdb" does not exist, skipping
  8. DROP DATABASE
  9. omm=# drop DATABASE  IF EXISTS  musicdb1;
  10. NOTICE:  database "musicdb1" does not exist, skipping
  11. DROP DATABASE
  12. omm=# drop DATABASE  IF EXISTS  musicdb2;
  13. NOTICE:  database "musicdb2" does not exist, skipping
  14. DROP DATABASE
  15. omm=# drop DATABASE  IF EXISTS  musicdb3;
  16. NOTICE:  database "musicdb3" does not exist, skipping
  17. DROP DATABASE
  18. omm=# drop tablespace IF EXISTS music_tbs;
  19. DROP TABLESPACE
复制代码


2.2 创建数据库及表空间

  1. omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
  2. CREATE TABLESPACE
  3. omm=# CREATE DATABASE musicdb  WITH TABLESPACE = music_tbs;
  4. CREATE DATABASE
复制代码


2.3 创建用户user1、user2、user3

  1. omm=# CREATE USER user1 IDENTIFIED BY 'kunpeng@1234';
  2. NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
  3. CREATE ROLE
  4. omm=# CREATE USER user2 IDENTIFIED BY 'kunpeng@1234';
  5. NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
  6. CREATE ROLE
  7. omm=# CREATE USER user3 IDENTIFIED BY 'kunpeng@1234';
  8. NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
  9. CREATE ROLE
复制代码


2.4 授予user1、user2、user3数据库系统SYSADMIN权限

  1. omm=# ALTER USER user1 SYSADMIN;
  2. omm=# ALTER ROLE
  3. omm=# ALTER USER user2 SYSADMIN;
  4. ALTER ROLE
  5. omm=# ALTER USER user3 SYSADMIN;
  6. omm=# ALTER ROLE
  7. omm=# \du
  8.                                                               List of roles
  9. Role name |                                                    Attributes                                                   
  10. | Member of
  11. -----------+------------------------------------------------------------------------------------------------------------------
  12. +-----------
  13. gaussdb   | Sysadmin                                                                                                         
  14. | {}
  15. omm       | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT
  16. | {}
  17. user2     | Sysadmin                                                                                                         
  18. | {}
  19. user3     | Sysadmin                                                                                                         
  20. | {}
  21. user1     | Sysadmin                                                                                                         
  22. | {}
复制代码




2.5 利用user1、user2、user3访问musicdb

  1. --以用户user1的身份在数据库musicdb中创建表t1,并插入一条数据:
  2. omm=# \c musicdb user1
  3. Password for user user1:
  4. Non-SSL connection (SSL connection is recommended when requiring high-security)
  5. You are now connected to database "musicdb" as user "user1".
  6. musicdb=> create table t1(col1 char(20));
  7. CREATE TABLE
  8. musicdb=> insert into t1 values('Hello kunpeng 1');
  9. INSERT 0 1
  10. musicdb=> select * from t1;
  11.          col1         
  12. ----------------------
  13. Hello kunpeng 1     
  14. (1 row)
  15. --以用户user2的身份在数据库musicdb中创建表t2,并插入一条数据:
  16. musicdb=> \c musicdb user2
  17. Password for user user2:
  18. Non-SSL connection (SSL connection is recommended when requiring high-security)
  19. You are now connected to database "musicdb" as user "user2".
  20. musicdb=> create table t2(col1 char(20));
  21. CREATE TABLE
  22. musicdb=> insert into t2 values('Hello kunpeng 2');
  23. musicdb=> INSERT 0 1
  24. musicdb=> select * from t2;
  25.          col1         
  26. ----------------------
  27. Hello kunpeng 2     
  28. (1 row)
  29. musicdb=> \c musicdb user3
  30. Password for user user3:
  31. Non-SSL connection (SSL connection is recommended when requiring high-security)
  32. You are now connected to database "musicdb" as user "user3".
  33. musicdb=> create table t3(col1 char(20));
  34. CREATE TABLE
  35. musicdb=> insert into t3 values('Hello kunpeng 3');
  36. INSERT 0 1
  37. musicdb=> select * from t2;
  38.          col1         
  39. ----------------------
  40. Hello kunpeng 2     
  41. (1 row)
复制代码




2.6 利用user1用户查看当前数据库musicdb有哪些表

  1. musicdb=> \c musicdb user1
  2. Password for user user1:
  3. Non-SSL connection (SSL connection is recommended when requiring high-security)
  4. You are now connected to database "musicdb" as user "user1".
  5. musicdb=> \dt
  6.                         List of relations
  7. Schema | Name | Type  | Owner |             Storage              
  8. --------+------+-------+-------+----------------------------------
  9. public | t1   | table | user1 | {orientation=row,compression=no}
  10. public | t2   | table | user2 | {orientation=row,compression=no}
  11. public | t3   | table | user3 | {orientation=row,compression=no}
  12. (3 rows)
复制代码


三、课后作业

3.1 创建数据库musicdb2

  1. omm=# CREATE DATABASE musicdb2  WITH TABLESPACE = music_tbs;
  2. CREATE DATABASE
复制代码


3.2 创建用户user11、user22、user33

  1. omm=# CREATE USER user11 IDENTIFIED BY 'kunpeng@1234';
  2. NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
  3. CREATE ROLE
  4. omm=# CREATE USER user22 IDENTIFIED BY 'kunpeng@1234';
  5. NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
  6. CREATE ROLE
  7. omm=# CREATE USER user33 IDENTIFIED BY 'kunpeng@1234';
  8. NOTICE:  The encrypted password contains MD5 ciphertext, which is not secure.
  9. CREATE ROLE
复制代码
3.3 授予user11、user22、user33用户SYSADMIN权限

  1. omm=# ALTER USER user11 SYSADMIN;
  2. omm=# ALTER ROLE
  3. omm=# ALTER USER user22 SYSADMIN;
  4. ALTER ROLE
  5. omm=#
  6. omm=# ALTER USER user33 SYSADMIN;
  7. ALTER ROLE
复制代码


3.4 分别在user11、user22、user33用户下创建对应products表并插入数据

  1. omm=# \c musicdb user11
  2. Password for user user11:
  3. Non-SSL connection (SSL connection is recommended when requiring high-security)
  4. You are now connected to database "musicdb" as user "user11".
  5. musicdb=> create table products1(product_id INT,product_name char(20),category char(30));
  6. musicdb=> CREATE TABLE
  7. musicdb=> insert into products1 values (1502,'olympus camera','category');
  8. INSERT 0 1
  9. musicdb=> insert into products1 values (1601,'lamaze','toys');
  10. INSERT 0 1
  11. musicdb=> insert into products1 values (1700,'wait interface','Books');
  12. musicdb=> INSERT 0 1
  13. musicdb=> insert into products1 values (1666,'harry potter','toys');
  14. INSERT 0 1
  15. musicdb=> select * from products1;
  16. product_id |     product_name     |            category            
  17. ------------+----------------------+--------------------------------
  18.        1502 | olympus camera       | category                     
  19.        1601 | lamaze               | toys                          
  20.        1700 | wait interface       | Books                        
  21.        1666 | harry potter         | toys                          
  22. (4 rows)
复制代码


  1. musicdb=> \c musicdb user22
  2. Password for user user22:
  3. Non-SSL connection (SSL connection is recommended when requiring high-security)
  4. You are now connected to database "musicdb" as user "user22".
  5. musicdb=> create table products1(product_id INT,product_name char(20),category char(30));
  6. ERROR:  relation "products1" already exists in schema "public"
  7. DETAIL:  creating new table with existing name in the same schema
  8. musicdb=> create table products2(product_id INT,product_name char(20),category char(30));
  9. CREATE TABLE
  10. musicdb=> insert into products2 values (1502,'olympus camera','category');
  11. INSERT 0 1
  12. musicdb=> insert into products2 values (1601,'lamaze','toys');
  13. INSERT 0 1
  14. musicdb=> insert into products2 values (1700,'wait interface','Books');
  15. INSERT 0 1
  16. musicdb=> insert into products2 values (1666,'harry potter','toys');
  17. INSERT 0 1
  18. musicdb=> select * from products2;
  19. product_id |     product_name     |            category            
  20. ------------+----------------------+--------------------------------
  21.        1502 | olympus camera       | category                     
  22.        1601 | lamaze               | toys                          
  23.        1700 | wait interface       | Books                        
  24.        1666 | harry potter         | toys                          
  25. (4 rows)
复制代码


  1. musicdb=> \c musicdb user33
  2. Password for user user33:
  3. Non-SSL connection (SSL connection is recommended when requiring high-security)
  4. You are now connected to database "musicdb" as user "user33".
  5. musicdb=> create table products3(product_id INT,product_name char(20),category char(30));
  6. CREATE TABLE
  7. musicdb=> insert into products3 values (1502,'olympus camera','category');
  8. INSERT 0 1
  9. musicdb=> insert into products3 values (1601,'lamaze','toys');
  10. INSERT 0 1
  11. musicdb=> insert into products3 values (1700,'wait interface','Books');
  12. INSERT 0 1
  13. musicdb=> insert into products3 values (1666,'harry potter','toys');
  14. musicdb=> INSERT 0 1
  15. musicdb=> select * from products3;
  16. product_id |     product_name     |            category            
  17. ------------+----------------------+--------------------------------
  18.        1502 | olympus camera       | category                     
  19.        1601 | lamaze               | toys                          
  20.        1700 | wait interface       | Books                        
  21.        1666 | harry potter         | toys                          
  22. (4 rows)
复制代码


3.4 利用user11查看musicdb有哪些表

  1. musicdb=> \c musicdb user11
  2. Password for user user11:
  3. Non-SSL connection (SSL connection is recommended when requiring high-security)
  4. You are now connected to database "musicdb" as user "user11".
  5. musicdb=> \dt
  6.                            List of relations
  7. Schema |   Name    | Type  | Owner  |             Storage              
  8. --------+-----------+-------+--------+----------------------------------
  9. public | products1 | table | user11 | {orientation=row,compression=no}
  10. public | products2 | table | user22 | {orientation=row,compression=no}
  11. public | products3 | table | user33 | {orientation=row,compression=no}
  12. public | t1        | table | user1  | {orientation=row,compression=no}
  13. public | t2        | table | user2  | {orientation=row,compression=no}
  14. public | t3        | table | user3  | {orientation=row,compression=no}
  15. (6 rows)
复制代码


四、心得体会

通过本节课的学习,加深了对openGauss体系架构的明白,在学习过程中要反复举行测试,通过大量实行来总结学到的知识。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表