一、学习目的
本节课是本次实训的第四次训练课,本节课的学习学习目的仍然是熟悉openGauss体系结构,能利用多个用户访问同一个数据库。
重点是:
1、怎样删除\创建数据库
2、怎样删除\创建表空间
3、怎样创建用户
4、怎样为用户授权
5、怎样多个用户访问同一个数据库
二、测试环境准备
2.1 删除已有数据库及表空间
- 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;
- NOTICE: database "musicdb" does not exist, skipping
- DROP DATABASE
- omm=# drop DATABASE IF EXISTS musicdb1;
- NOTICE: database "musicdb1" does not exist, skipping
- DROP DATABASE
- omm=# drop DATABASE IF EXISTS musicdb2;
- NOTICE: database "musicdb2" does not exist, skipping
- DROP DATABASE
- omm=# drop DATABASE IF EXISTS musicdb3;
- NOTICE: database "musicdb3" does not exist, skipping
- DROP DATABASE
- omm=# drop tablespace IF EXISTS music_tbs;
- DROP TABLESPACE
复制代码
2.2 创建数据库及表空间
- omm=# CREATE TABLESPACE music_tbs RELATIVE LOCATION 'tablespace/test_ts1';
- CREATE TABLESPACE
- omm=# CREATE DATABASE musicdb WITH TABLESPACE = music_tbs;
- CREATE DATABASE
复制代码
2.3 创建用户user1、user2、user3
- 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
复制代码
2.4 授予user1、user2、user3数据库系统SYSADMIN权限
- omm=# ALTER USER user1 SYSADMIN;
- omm=# ALTER ROLE
- omm=# ALTER USER user2 SYSADMIN;
- ALTER ROLE
- omm=# ALTER USER user3 SYSADMIN;
- omm=# ALTER ROLE
- omm=# \du
- List of roles
- Role name | Attributes
- | Member of
- -----------+------------------------------------------------------------------------------------------------------------------
- +-----------
- gaussdb | Sysadmin
- | {}
- omm | Sysadmin, Create role, Create DB, Replication, Administer audit, Monitoradmin, Operatoradmin, Policyadmin, UseFT
- | {}
- user2 | Sysadmin
- | {}
- user3 | Sysadmin
- | {}
- user1 | Sysadmin
- | {}
复制代码
2.5 利用user1、user2、user3访问musicdb
- --以用户user1的身份在数据库musicdb中创建表t1,并插入一条数据:
- omm=# \c musicdb user1
- Password for user user1:
- Non-SSL connection (SSL connection is recommended when requiring high-security)
- You are now connected to database "musicdb" as user "user1".
- musicdb=> create table t1(col1 char(20));
- CREATE TABLE
- musicdb=> insert into t1 values('Hello kunpeng 1');
- INSERT 0 1
- musicdb=> select * from t1;
- col1
- ----------------------
- Hello kunpeng 1
- (1 row)
- --以用户user2的身份在数据库musicdb中创建表t2,并插入一条数据:
- musicdb=> \c musicdb user2
- Password for user user2:
- Non-SSL connection (SSL connection is recommended when requiring high-security)
- You are now connected to database "musicdb" as user "user2".
- musicdb=> create table t2(col1 char(20));
- CREATE TABLE
- musicdb=> insert into t2 values('Hello kunpeng 2');
- musicdb=> INSERT 0 1
- musicdb=> select * from t2;
- col1
- ----------------------
- Hello kunpeng 2
- (1 row)
- musicdb=> \c musicdb user3
- Password for user user3:
- Non-SSL connection (SSL connection is recommended when requiring high-security)
- You are now connected to database "musicdb" as user "user3".
- musicdb=> create table t3(col1 char(20));
- CREATE TABLE
- musicdb=> insert into t3 values('Hello kunpeng 3');
- INSERT 0 1
- musicdb=> select * from t2;
- col1
- ----------------------
- Hello kunpeng 2
- (1 row)
复制代码
2.6 利用user1用户查看当前数据库musicdb有哪些表
- musicdb=> \c musicdb user1
- Password for user user1:
- Non-SSL connection (SSL connection is recommended when requiring high-security)
- You are now connected to database "musicdb" as user "user1".
- musicdb=> \dt
- List of relations
- Schema | Name | Type | Owner | Storage
- --------+------+-------+-------+----------------------------------
- public | t1 | table | user1 | {orientation=row,compression=no}
- public | t2 | table | user2 | {orientation=row,compression=no}
- public | t3 | table | user3 | {orientation=row,compression=no}
- (3 rows)
复制代码
三、课后作业
3.1 创建数据库musicdb2
- omm=# CREATE DATABASE musicdb2 WITH TABLESPACE = music_tbs;
- CREATE DATABASE
复制代码
3.2 创建用户user11、user22、user33
- omm=# CREATE USER user11 IDENTIFIED BY 'kunpeng@1234';
- NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
- CREATE ROLE
- omm=# CREATE USER user22 IDENTIFIED BY 'kunpeng@1234';
- NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
- CREATE ROLE
- omm=# CREATE USER user33 IDENTIFIED BY 'kunpeng@1234';
- NOTICE: The encrypted password contains MD5 ciphertext, which is not secure.
- CREATE ROLE
复制代码 3.3 授予user11、user22、user33用户SYSADMIN权限
- omm=# ALTER USER user11 SYSADMIN;
- omm=# ALTER ROLE
- omm=# ALTER USER user22 SYSADMIN;
- ALTER ROLE
- omm=#
- omm=# ALTER USER user33 SYSADMIN;
- ALTER ROLE
复制代码
3.4 分别在user11、user22、user33用户下创建对应products表并插入数据
- omm=# \c musicdb user11
- Password for user user11:
- Non-SSL connection (SSL connection is recommended when requiring high-security)
- You are now connected to database "musicdb" as user "user11".
- musicdb=> create table products1(product_id INT,product_name char(20),category char(30));
- musicdb=> CREATE TABLE
- musicdb=> insert into products1 values (1502,'olympus camera','category');
- INSERT 0 1
- musicdb=> insert into products1 values (1601,'lamaze','toys');
- INSERT 0 1
- musicdb=> insert into products1 values (1700,'wait interface','Books');
- musicdb=> INSERT 0 1
- musicdb=> insert into products1 values (1666,'harry potter','toys');
- INSERT 0 1
- musicdb=> select * from products1;
- product_id | product_name | category
- ------------+----------------------+--------------------------------
- 1502 | olympus camera | category
- 1601 | lamaze | toys
- 1700 | wait interface | Books
- 1666 | harry potter | toys
- (4 rows)
复制代码
- musicdb=> \c musicdb user22
- Password for user user22:
- Non-SSL connection (SSL connection is recommended when requiring high-security)
- You are now connected to database "musicdb" as user "user22".
- musicdb=> create table products1(product_id INT,product_name char(20),category char(30));
- ERROR: relation "products1" already exists in schema "public"
- DETAIL: creating new table with existing name in the same schema
- musicdb=> create table products2(product_id INT,product_name char(20),category char(30));
- CREATE TABLE
- musicdb=> insert into products2 values (1502,'olympus camera','category');
- INSERT 0 1
- musicdb=> insert into products2 values (1601,'lamaze','toys');
- INSERT 0 1
- musicdb=> insert into products2 values (1700,'wait interface','Books');
- INSERT 0 1
- musicdb=> insert into products2 values (1666,'harry potter','toys');
- INSERT 0 1
- musicdb=> select * from products2;
- product_id | product_name | category
- ------------+----------------------+--------------------------------
- 1502 | olympus camera | category
- 1601 | lamaze | toys
- 1700 | wait interface | Books
- 1666 | harry potter | toys
- (4 rows)
复制代码
- musicdb=> \c musicdb user33
- Password for user user33:
- Non-SSL connection (SSL connection is recommended when requiring high-security)
- You are now connected to database "musicdb" as user "user33".
- musicdb=> create table products3(product_id INT,product_name char(20),category char(30));
- CREATE TABLE
- musicdb=> insert into products3 values (1502,'olympus camera','category');
- INSERT 0 1
- musicdb=> insert into products3 values (1601,'lamaze','toys');
- INSERT 0 1
- musicdb=> insert into products3 values (1700,'wait interface','Books');
- INSERT 0 1
- musicdb=> insert into products3 values (1666,'harry potter','toys');
- musicdb=> INSERT 0 1
- musicdb=> select * from products3;
- product_id | product_name | category
- ------------+----------------------+--------------------------------
- 1502 | olympus camera | category
- 1601 | lamaze | toys
- 1700 | wait interface | Books
- 1666 | harry potter | toys
- (4 rows)
复制代码
3.4 利用user11查看musicdb有哪些表
- musicdb=> \c musicdb user11
- Password for user user11:
- Non-SSL connection (SSL connection is recommended when requiring high-security)
- You are now connected to database "musicdb" as user "user11".
- musicdb=> \dt
- List of relations
- Schema | Name | Type | Owner | Storage
- --------+-----------+-------+--------+----------------------------------
- public | products1 | table | user11 | {orientation=row,compression=no}
- public | products2 | table | user22 | {orientation=row,compression=no}
- public | products3 | table | user33 | {orientation=row,compression=no}
- public | t1 | table | user1 | {orientation=row,compression=no}
- public | t2 | table | user2 | {orientation=row,compression=no}
- public | t3 | table | user3 | {orientation=row,compression=no}
- (6 rows)
复制代码
四、心得体会
通过本节课的学习,加深了对openGauss体系架构的明白,在学习过程中要反复举行测试,通过大量实行来总结学到的知识。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |