oracle、postgres、mysql数据库的建库、创建用户、导人导出备份总结

 

本文包含的内容:使用命令操作oracle、postgres、mysql的导入导出,登录到数据,创建用户

注:我在公司使用的是Center OS操作系统,所以oracle和postgres均是在Linux使用,在其他工具中未测试;mysql是在自己的笔记本电脑上使用的,均在Linux和Dos命令下使用过。

1.oracle

  • 切换到oracle用户: su - oracle 
  • 1.登陆数据库:sqlplus 用户名/密码@172.10.103.78:1521/orcl as sysdba;(用户名可使用系统的: sys)
  • 2.创建表空间:create tablespace 表空间名 datafile '/xx/xxx/xx.dbf' size 1000M autoextend on next 100M;
  • 3.创建用户:create user 用户名 identified by 密码 default tablespace 表空间;
  • 4.授权:grant connect,resource,dba to 用户名; (自己视情况而定)

     

  注:导出表之前可能需要进行空表的处理:

    select count(1) from user_tables where num_rows = 0;  --查询有多少个空表

    select 'alter table  ' || table_name || ' allocate extent ; ' from user_tables where num_rows  = 0;     --对空表的处理,执行这句后,将产生的结果集全部执行

 导出:exp 用户名/密码@ip:sid/orcl  file=/xxx/xxx/xx.dmp  
 如:exp platform/platform@172.10.103.105:1521/orcl file=/data/oracle/platform_20180824.dmp;

导出具体的某些表:如:exp platform/platform@172.10.103.105:1521/orcl tables=student,user,services,department file=/data/oracle/platform_20180824.dmp;

 导入:imp 用户名/密码@ip:sid/orcl file=/xxx/xxx/xxx.dmp ignore=y full=y ;    
如:imp platform/platform@172.10.103.105:1521/orcl file=/data/oracle/platform_20180824.dmp ignore=y full=y;

  其他操作

  •   查用户:select * from dba_users;
  •        查询使用表空间:select tablespace_name from user_tables group by tablespace_name;
  •        查询当前用户默认表空间:select default_tablespace from dba_users where username='用户名';
  •        查看表空间名,路径:select  t1.name, t2.name from v$tablespace t1, v$datafile t2 where t1.ts #=t2.tx#;

 2.postgres

  •   在Linux中,切换到postgres用户下:su - postgres
  •   登录postgres数据库中:psql  输入密码:postgres 
 导出:pg_dump  数据库名 > 路径/文件名.dump; 之后再输入密码  如:pg_dump  platform_jjjc > /data/postgres/platform_jjjc20180824.dump;
 导入:psql 数据库名 -U 数据库密码  <  路径/导入的文件名;  
如:psql platform_jjjc -U platform_jjjc < /data/postgres/platform_jjjc20180824.dump;

创建一套postgres:创建用户、创建schema、创建数据库、授权

  1.  首先登录postgres数据库中:psql  输入密码:postgres
  2.  创建用户:create user 用户名 with password '密码';
如:create user mytest with password 'mytest'; 

:1.以分号结尾;2.密码要用单引号括起来。

  3.  创建数据库:create database 数据库名 owner 指定拥有者名称;  如:create database mytest owner mytest;

  4.  创建schema: create schema schema名称;
 如:create schema mytest;

或者创建schema时指定owner:create schema mytest authorization xxx;

:schema的作用:1.方便管理众多对象;2.多个用户可以共享一个数据库,各个schema独立。

  删除schema的命名:drop schema schema名称;

 5.  赋权:grant all privileges on database 数据库名 to 用户名; 
如:grant all privileges on database mytest to mytest;

退出:\q

 

  

  3.mysql

  mysql登录、添加新用户、为用户创建数据库、为用户分配权限 

1.登陆:mysql -u root -p 或者 mysql -u账号 -p密码

 注:远程登陆命令:mysql:mysql -u账号 -p面 -h IP地址

2.创建用户:create user '用户名@IP' identified by '密码';  
如:create user 'platform_hxc@172.0.0.1' identified by 'platform_hxc';

允许外网访问:create user 'test'@'%' identified by 'test';

刷新权限:flush privileges;

3.为用户创建数据库:create database 数据库名 default charset utf8 collate utf8_general_ci;

如:create database platform_hxc default charset utf8 collate utf8_general_ci;

4.授权:grant all privileges on *.* to '用户名'@'IP'  identified by '密码' with grant option;

如:grant all privileges on *.* to 'platform_hxc'@'172.0.0.1' identified by 'platform_hxc' with grant option;

5.刷新权限:flush privileges;
6.退出:exit;

 

mysql其他相关操作:

  • 修改密码:set password for '用户名'@'IP' = password('新密码'); 若修改登录用户 直接用:set password = password('新密码');
  • 撤销用户权限:revoke privilege on 数据库名(.表名) from '用户名'@'IP';
  • 删除用户:drop user '用户名'@'IP';

 

posted @ 2018-08-24 23:37  黄小葱  阅读(537)  评论(0编辑  收藏  举报