mysql 用户管理

1、创建用户:
create user fei@’localhost’ identified by ‘fei’;
note:该用户没有授予任何访问权限,如果不加@’localhost’默认为“@%”

2、用户授权:
grant delete on test.* to fei@’localhost’ with grant option;
note:1)*表示test的任何对象,如果是*.*表示数据库中的所有对象
2)with grant option 表明fei用户可以把相关权限授给其他用户

3、权限回收:
revoke delete on test.* from fei@’localhost’;

4、创建用户授权一起实现
grant select,insert,update,delete on *.* to ‘fei2’@’%’
identified by ‘fei2’ with grant option;
note:在mysql中,如果@后面的登录范围不同,帐号可以一样

5、直接使用insert建立用户
insert into user(host,user,password,ssl_cipher,x509_issuer,x509_subject)
values(‘localhost’,’xff’,password(‘xff’),”,”,”);
FLUSH PRIVILEGES;
note:1)必须要加上ssl_cipher,x509_issuer,x509_subject三列,以为其默认值不为空(数据库版本为:5.0.51b)
2)FLUSH PRIVILEGES重载授权表,使权限更改生效
3)mysql是通过User表,Db表,Host表,Tables_priv 表,Columns_priv 表这5张表实现用户权限控制,均可以通过直接对这些表的操作以达到对用户的管理

6、删除用户:
drop user xff@localhost;(@不加默认为“%”)

7、授权精确到列:
grant select (cur_url,pre_url) on test.abc to fei@localhost;

8、修改root密码:
update mysql.user set password=password(‘passw0rd’) where user=’root’;
FLUSH PRIVILEGES;

oracle之表管理

1、创建session级别的临时表(commit后数据还会保留)
1)create global temporary table tem_session on commit preserve rows
as select rowid rid,id from a;
2) create global temporary table tem_fei(id number,a varchar2(10))
on commit preserve rows;

2、创建commit级别的临时表(commit后数据清空)
1)create global temporary table tem_xff on commit delete rows
as select * from a;
2)create global temporary table tem_a (id int,abc number)
on commit delete rows;
note:当session退出或者数据库重启后临时表会被清空,但是临时表的结构还是保存在数据库里面的,还是可以直接插入数据等操作

3、修改表的所属表空间
1)查看表所属表空间
select table_name,tablespace_name from user_tables;
2)查看哪些表空间
select name from v$tablespace;
3)修改表所属表空间
alter table a move tablespace users;

4、删除表中若干列
设置为unused
alter table tt set unused column z cascade constraints;
每次删除1000条提交commit
alter table tt drop unused columns checkpoint 1000;
如果中断继续执行
alter table tt drop columns continue checkpoint 1000;
直接删除一列
alter table tt drop column y;

5、查看表的结构

--1)desc
desc tablename
--2)dbms_metadata.get_ddl
set long 100000
 set pages 0
select dbms_metadata.get_ddl('TABLE','tablename') from dual;

oracle之undo

1、创建undo表空间
create undo tablespace xff_undo datafile
‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\xff_undo1.dbf’
size 20m reuse autoextend on;
2、修改默认undo表空间
alter system set undo_tablespace=xff_undo;
3、查看undo中的transaction占用的block数目
select addr,used_ublk from v$transaction;
4、查看undo中的历史信息汇总
select begin_time,end_time,undoblks from v$undostat;
5、设置undo的过期时间(单位是s)
alter system set undo_retention=100;
6、决定undo大小
1)每秒钟undo的大小
select max(undoblks/(end_time-begin_time)*24*3600) from v$undostat;
2)undo的过期时间
show parameter undo_retention
3)oracle block的大小
show parameter db_block_size
4)上面三项相乘即为undo所需要的大小

mysql 使用二进制日志文件恢复数据库

在心中一直有个东西梗着,那就是mysql利用二进制日志文件恢复数据库,今天下决心解决这个问题,在网上查了些资料,然后自己的物理机上测试总是失败,开始一直怀疑是网上说的不正确,最后想起来自己的数据库是从5.0.1升级到5.1.49的,也许是因为升级的原因导致日志文件无法恢复,出现下面的错误

最后没有办法,在虚拟机上面新装个mysql 5.1.49的数据库进行测试,先启用日志功能:log-bin=bin_log,然后配置假定添加删除相关数据,模拟测试环境(新建表,添加数据,删除数据,添加数据,删除表)

use test;
create table test(id int auto_increment not null primary key,val int,data varchar(20));
insert into test(val,data) values(10,'liang');
insert into test(val,data) values(20,'jia');
insert into test(val,data) values(30,'hui');
flush logs;
insert into test(val,data) values(40,'aaa');
insert into test(val,data) values(50,'bbb');
insert into test(val,data) values(60,'ccc');
delete from test where id between 4 and 5;
insert into test(val,data) values(70,'ddd');
flush logs;
insert into test(val,data) values(80,'dddd');
insert into test(val,data) values(90,'eeee');
drop table test;

得到日志文件如下:

执行如下命令(dos要进入日志文件所在目录中)把二进制文件转化为记事本文件,方便查看:

mysqlbinlog bin_log.000001 >c:\1.txt
mysqlbinlog bin_log.000002 >c:\2.txt
mysqlbinlog bin_log.000003 >c:\3.txt


执行如下命令恢复数据库的删除数据和删除表操作

mysqlbinlog bin_log.000001 | mysql -uroot -p4020894
mysqlbinlog bin_log.000002 --stop-pos=861 | mysql -uroot -p4020894
mysqlbinlog bin_log.000002 --start-pos=965 | mysql -uroot -p4020894
mysqlbinlog bin_log.000003 --stop-pos=556 | mysql -uroot -p4020894

执行后的结果如:
上面语句相关说明:
1)stop-pos=861表示执行到861行停止,具体阅读2.txt文件(这里体现了二进制文件转化为文本文件的价值)
2)start-pos=965表示执行从965行开始

其他操作

mysqlbinlog bin_log.000001--读取日志文件在dos上显示
mysqlbinlog bin_log.000002 --stop-datetime="2010-08-19 14:49:39" |mysql -uroot -p4020894--基于停止时间点的恢复
mysqlbinlog bin_log.000001 --start-datetime="2010-08-19 14:48:25" |mysql -uroot  -p4020894--基于开始时间点的恢复
mysqlbinlog bin_log.000001 --start-datetime="2010-08-19 14:47:28" --stop-datetime="2010-08-19 14:48:41"|mysql -uroot -p4020894--基于开始与停止时间点的恢复
mysqlbinlog bin_log.000001 bin_log.000002 --start-datetime="2010-08-19 14:47:28" --stop-datetime="2010-08-19 14:50:13" |mysql -uroot -p4020894--多个日志文件的恢复

oracle之datafile,tablespace

1、创建一般tablespace

create tablespace xifenfei datafile ‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\
xifenfei.DBF’ size 10m reuse autoextend on next 10m maxsize UNLIMITED ;

2、创建temp tablespace

create temporary tablespace xff_temp tempfile

‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\xff_temp.dbf’ size 10m;

3、创建undo tablespace

create undo tablespace xff_undo datafile

‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\xff_undo.dbf’ size 10m;

4、更改数据库的默认临时表空间

alter database default temporary tablespace xff_temp;

5、查询表空间类型

select tablespace_name,contents from dba_tablespaces;

6、查询默认临时表空间

select * from database_properties where property_name like ‘%TEMP_TABLE%’;

7、表空间变为只读状态

alter tablespace xifenfei read only;

note:该表空间中的objects可以被drop,因为表的记录是放在数据字典(system)中

8、表空间变为读写状态

alter tablespace xifenfei read write;

9、表空间offline

alter tablespace xifenfei offline;

note:如果有数据没有commit,会自动被commit掉

10、表空间online

alter tablespace xifenfei online;

11、查看表空间剩余大小

select f.tablespace_name,a.total,u.used,f.free,round((u.used/a.total)*100) "% used", round((f.free/a.total)*100) "% Free"
 from
 (select tablespace_name, sum(bytes/(1024*1024)) total
 from dba_data_files group by tablespace_name) a,
 (select tablespace_name, round(sum(bytes/(1024*1024))) used
 from dba_extents group by tablespace_name) u,
 (select tablespace_name, round(sum(bytes/(1024*1024))) free
 from dba_free_space group by tablespace_name) f
 WHERE a.tablespace_name = f.tablespace_name and a.tablespace_name = u.tablespace_name order by "% Free"; 

12、表空间自增长
alter database datafile ‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\XIFENFEI.DBF
‘ autoextend on next 2m maxsize 100m;
13、表空间中添加数据文件
alter tablespace xifenfei add datafile ‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\OR
CL\XIFENFEI1.DBF’ size 20m;
14、查看表空间是否是自增长
select file_name,autoextensible from dba_data_files;

15、修改表空间大小

alter database datafile ‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\

XIFENFEI1.DBF’ resize 15m;

16、查询临时表空间

select tablespace_name,file_name from dba_temp_files;

17、datafile  rename

1)alter tablespace xifenfei rename datafile

‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\ XIFENFEI1.DBF’ to

‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\abc\ XIFENFEI1.DBF’

note:target文件必须存在,表空间必须离线

2) alter database rename file ‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\XIFENFEI1.DBF’
to ‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\orcl\XIFENFEI1.DBF’

note:target文件必须存在,数据库必须mount状态

18、删除表空间中的某个datafile

alter tablespace xifenfei drop datafile

‘C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\XIFENFEI1.DBF’;

19、删除表空间

drop tablespace xifenfei including contents and datafiles;