ORACLE 19c新建用户登录

ORACLE 19c新建用户

数据据库、用户、CDB与PDB之间的关系

在建立用户和数据库前先要了解一下数据据库、用户、CDB与PDB之间的关系。
博文参考:https://www.cnblogs.com/peasant/p/6374014.html
在这里插入图片描述
通常在CDB上建立的用户是common user,新建用户名前要加C##。在PDB上创建的用户是local user。

在CDB上创建用户

  1. 确保再CDB环境下
SQL>  select name,cdb from v$database;

NAME      CDB
--------- ---
ORCL      YES

  1. 新建用户
SQL> create user C##test identified by testpass;

User created.

用户C##test
密码testpass

  1. 配置权限
SQL> grant dba,connect,resource,create view to C##test;
SQL> grant create session to C##test;
SQL> grant select any table to C##test;
SQL> grant update any table to C##test;
SQL> grant insert any table to C##test;
SQL>  grant delete any table to C##test;

  1. 删除用户
SQL> drop user C##test cascade;

User dropped.
  1. 登录
[oracle@localhost bin]$  sqlplus C##test/testpass

SQL*Plus: Release 19.0.0.0.0 - Production on Mon Oct 19 15:21:19 2020
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.

Last Successful login time: Mon Oct 19 2020 15:16:35 +08:00

Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

SQL>

在PDB上创建用户

  1. 查看PDB name
SQL> select pdb_id,pdb_name,dbid,status,creation_scn from dba_pdbs;

    PDB_ID
----------
PDB_NAME
--------------------------------------------------------------------------------
      DBID STATUS     CREATION_SCN
---------- ---------- ------------
         3
PDB
3301209450 NORMAL          2164771

         2
PDB$SEED
 442221313 NORMAL          2019898

    PDB_ID
----------
PDB_NAME
--------------------------------------------------------------------------------
      DBID STATUS     CREATION_SCN
---------- ---------- ------------

SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;

    CON_ID       DBID
---------- ----------
NAME
--------------------------------------------------------------------------------
OPEN_MODE
----------
         2  442221313
PDB$SEED
READ ONLY

         3 3301209450
PDB
MOUNTED

    CON_ID       DBID
---------- ----------
NAME
--------------------------------------------------------------------------------
OPEN_MODE
----------

本地PDB用户名为pdb
2. 进入PDB

SQL> alter session set container=pdb;

Session altered.
  1. 新建用户
SQL> create user test2 identified by test2pass;

User created.

用户名test2
密码test2pass

  1. 配置权限
SQL> grant dba,connect,resource,create view to test2;

Grant succeeded.

SQL> grant select any table to test2;

Grant succeeded.

SQL> grant update any table to test2;

Grant succeeded.

SQL> grant insert any table to test2;

Grant succeeded.

SQL>  grant delete any table to test2;

Grant succeeded.

SQL> grant create session to test2;

Grant succeeded.
  1. 配置TNS
    如果不配置登录失败,会报用户名密码错误,默认的登录是在CDB上的用户。需要修改tnsnames.ora文件
    linux 路径是在oracle安装路径network/admin下
    编辑vi tnsnames.ora
默认是ORCL
ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )
新建一个PDB(名字可以自己取),SERVICE_NAME为PDB的name
PDB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = pdb)
    )
  )
  1. 登录
    使用登录方式 sqlplus 用户名/密码@PDB(tnsnames.ora中新建PDB的名字,不是SERVICE_NAME)
[oracle@localhost bin]$  sqlplus test2/test2pass@PDB

SQL*Plus: Release 19.0.0.0.0 - Production on Mon Oct 19 15:42:34 2020
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

SQL>

之前一直报
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
是因为@名字是SERVICE_NAME

[oracle@localhost bin]$  sqlplus test2/test2pass@pdb

SQL*Plus: Release 19.0.0.0.0 - Production on Mon Oct 19 15:21:39 2020
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.

ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified

问题解决

报错1:
ERROR at line 1:
ORA-01507: database not mounted

  1. 关闭数据库
    sql>shutdown;

  2. 进入oracle home路径下dbs文件
    执行命令fuser -u lk+自己数据库命字
    [oracle@localhost dbs]$ fuser -u lkORCL
    /var/ftp/work/oracle/dbs/lkORCL: 27715(oracle) 27717(oracle) 27719(oracle) 27726(oracle) 27728(oracle) 27732(oracle) 27740(oracle) 27746(oracle) 27750(oracle) 27752(oracle) 27754(oracle) 27758(oracle) 27766(oracle) 27768(oracle) 27776(oracle) 27780(oracle) 27783(oracle) 27789(oracle)
    再执行fuser -k lk+自己数据库命字
    [oracle@localhost dbs]$ fuser -k lkORCL
    /var/ftp/work/oracle/dbs/lkORCL: 27715 27717 27719 27726 27728 27732 27740 27746 27750 27752 27754 27758 27766 27768 27776 27780 27783 27789

  3. 登录数据库,重新启动
    sqlplus / as sysdba
    SQL> startup
    ORACLE instance started.

Total System Global Area 1593832664 bytes
Fixed Size 9135320 bytes
Variable Size 1006632960 bytes
Database Buffers 570425344 bytes
Redo Buffers 7639040 bytes
Database mounted.
Database opened.

可以看到Database mounted.Database opened.

报错2:
ERROR at line 1:
ORA-01109: database not open

进入pbd数据库以后新建数据库报Pluggable数据库没有打开
直接startup
SQL> startup
Pluggable Database opened.

猜你喜欢

转载自blog.csdn.net/ly_6118/article/details/109162165