数据库
首页 > 数据库> > 数据库管理员 DBA 数据库管理: 数据库基础

数据库管理员 DBA 数据库管理: 数据库基础

作者:互联网

数据库管理员 DBA 数据库管理: 数据库基础

基本使用 建表 数据类型 约束条件 修改表 键值

+++++RDBMS1_DAY03 mysql-5.7
1 数据导入与导出
1.1 默认检索路径
1.1.1 查看
mysql> show variables like “secure_file_priv”;
±-----------------±----------------------+
| Variable_name | Value |
±-----------------±----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
±-----------------±----------------------+
1 row in set (0.00 sec)

	1.1.2 修改

]# vim /etc/my.cnf
[mysqld]
secure_file_priv="/myload"
:wq
]# mkdir /myload
]# chown mysql /myload/
]# systemctl restart mysqld

]# mysql -uroot -p123qqq…A
mysql> show variables like “secure_file_priv”;
±-----------------±---------+
| Variable_name | Value |
±-----------------±---------+
| secure_file_priv | /myload/ |
±-----------------±---------+
1 row in set (0.00 sec)

1.2 数据导入:把系统文件的内容存储到数据库的表里
	1 命令格式
	  mysql> load  data  infile   "目录名/文件名" 

into table 库名.表名 fields terminated by “分隔符” lines terminated by “\n”;
2 注意事项?
3 数据导入步骤
建库
建表
拷贝文件到检索目录下
导入数据
查看表记录
4 例子
mysql> create database db3;
mysql> create table db3.user(
name char(50) , password char(1) ,uid int , gid int , comment varchar(150) , homedir char(60) , shell char(50)
);

mysql> system cp /etc/passwd /myload/

mysql> load data infile “/myload/passwd” into table db3.user fields terminated by “:” lines terminated by “\n” ;

mysql> alter table db3.user add id int primary key auto_increment first;

mysql> select * from db3.user ;

mysql> select * from db3.user where id = 1;

1.3 数据导出 :把表记录存储到系统文件里
	命令格式:
	sql查询命令  into   outfile   “目录名/文件名";

	sql查询命令   into   outfile   “目录名/文件名" 
            fields  terminated   by   “分隔符”; 
	
        sql查询命令   into   outfile   “目录名/文件名”
            fields  terminated   by   “分隔符”
            lines   terminated   by   “\n” ;  

	注意事项? 
	例子:

mysql> select * from db3.user where id <= 2 into outfile “/myload/user1.txt” ;

mysql> select * from db3.user where id <= 2 into outfile “/myload/user2.txt”
-> fields terminated by “#”;

mysql> select * from db3.user where id <= 2 into outfile “/myload/user3.txt” fields terminated by “#” lines terminated by “!!!”;

mysql> select name,uid,shell from db3.user where id <= 3 into outfile “/myload/user4.txt”;

mysql> system ls /myload

mysql> system cat /myload/user1.txt

2 管理表记录
2.1 插入表记录 insert into
插入1条表记录给所以字段赋值
insert into 库.表 values(值列表);

    插入多条表记录给所以字段赋值
	insert into 库.表   values(值列表),(值列表)....;

    插入1条表记录给指定字段赋值
	insert into 库.表(字段名列表)  values(值列表);	

    插入多条表记录给指定字段赋值
	insert into 库.表(字段名列表)  values(值列表),(值列表)....;

mysql> insert into db3.user(name)values(“bob”);
mysql> insert into db3.user(name,uid,homedir) values(“bob”,19,"/bin/bash");
mysql> insert into db3.user values (23,“jerry”,“x”,2000,2000,“student user”,"/home/jerry","/bin/bash");

2.2 查询表记录 select 
	select 字段名列表 from 库.表 where 条件 ;

mysql> select * from db3.user;
mysql> select name,uid ,password from db3.user;
mysql> select name,uid ,password from db3.user where shell="/sbin/nologin";

2.3 修改记录字段值 
	update  库.表  set 字段名=值,字段名=值【 where 条件 】;

mysql> update db3.user set password=“a” ;
mysql> update db3.user set password=“x” where name=“root”;

2.4 删除表记录 (删除行)
	delete    from   库名.表名  【where  条件表达式】;
    mysql> delete from  db3.user where name="bob";

3 匹配条件 (适用于select update delete)
3.1 基本匹配条件
1 数值比较 = != > >= < <=
mysql> select name from db3.user where uid = 0 ;
mysql> select name,uid,gid from db3.user where uid = gid;
mysql> select name,uid,gid from db3.user where uid != gid;
mysql> select * from db3.user where id <= 10;
2 字符比较 = !=
mysql> select name ,shell from db3.user where shell != “/bin/bash”;
mysql> select name ,shell from db3.user where shell = “/bin/bash”;
mysql> select name from db3.user where name=“mysql”;
3 空 is null
非空 is not null
mysql> select name , shell from db3.user where shell is not null;
mysql> select name , shell from db3.user where shell is null;

mysql> insert into db3.user(name)values (null),(“null”),("");
mysql> select id ,name from db3.user;
mysql> select id , name from db3.user where name=“null”;
mysql> select id , name from db3.user where name="";
mysql> select id , name from db3.user where name is null;
4 逻辑匹配 (多个条件)
逻辑与 and 多个条件必须都成立
逻辑或 or 多个条件成立即可
逻辑非 !/ not 取反 != is not null
mysql> select * from db3.user where uid=0 or name=“bin” ;
mysql> select * from db3.user where uid=0 and name=“bin” ;
5 范围内匹配/去重显示
in (值列表) 在…里…
not in (值列表) 不在…里…
between 数字 and 数字 在…之间…
distinct 字段名 去重显示
mysql> select name ,uid from db3.user where uid in (1 , 9 ,3 ,7);
mysql> select name from db3.user where name in (“demo”,“sync”,“bin”,“root”);

mysql> select name , shell from db3.user
where shell not in ("/bin/bash","/sbin/nologin");

mysql> select name ,uid from db3.user where uid between 10 and 40 ;

mysql> select distinct gid from db3.user where id <= 10;
3.2 高级匹配条件
1、 like 模糊查询
where 字段名 like ‘通配符’
_ 表示1个字符
% 表示0~n个字符
mysql> select name from db3.user where name like ‘’ ;
mysql> select name from db3.user where name like 'a
’ ;
mysql> select name from db3.user where name like ‘a%’ ;
mysql> select name from db3.user where name like ‘%a%’ ;
mysql> select name from db3.user where name like '
%_’ ;
mysql> select name from db3.user where name like ‘%a%’ ;
2、正则表达式
where 字段名 regexp '正则表达式’
元字符 ^ $ . [ ] * |
mysql> insert into db3.user(name)values(“9yaya”),(“ya7ya”),(“yaya6”);
mysql> select name from db3.user where name regexp ‘^a|t;mysql&gt;selectnamefromdb3.userwherenameregexp[09];mysql&gt;selectnamefromdb3.userwherenameregexp[09]mysql&gt;selectname,uidfromdb3.userwhereuidregexp..;mysql&gt;selectname,uidfromdb3.userwhereuidregexp..&#x27; ; mysql&gt; select name from db3.user where name regexp &#x27;^[0-9]&#x27;; mysql&gt; select name from db3.user where name regexp &#x27;[0-9]&#x27; mysql&gt; select name ,uid from db3.user where uid regexp &#x27;..&#x27;; mysql&gt; select name ,uid from db3.user where uid regexp &#x27;^..′;mysql>selectnamefromdb3.userwherenameregexp′[0−9]′;mysql>selectnamefromdb3.userwherenameregexp′[0−9]′mysql>selectname,uidfromdb3.userwhereuidregexp′..′;mysql>selectname,uidfromdb3.userwhereuidregexp′..’;
mysql> select name ,uid from db3.user where uid regexp ‘^…$’;

	3 四则运算  +  -  *  /  %

mysql> alter table db3.user add age tinyint unsigned default 10 after name;

mysql> select name ,uid , gid , uid+gid zh from db3.user;

mysql> select name ,age , 2019 - age csnf from db3.user where name=“root”;

mysql> select name ,uid from db3.user where uid % 2 = 0 ;

mysql> update db3.user set uid=uid+1 where uid <= 10;

mysql> update db3.user set uid=uid-1 where uid <= 10;

mysql> select name , uid , gid , (uid+gid)/2 pjz from db3.user where name = “bin”;
3.3 操作查询结果
1 聚集函数(对数据做统计的命令)
avg(字段名) //统计字段平均值
sum(字段名) //统计字段之和
min(字段名) //统计字段最小值
max(字段名) //统计字段最大值
count(字段名) //统计字段值个数

mysql> select avg(gid) from db3.user;
mysql> select max(uid) from db3.user;
mysql> select min(uid) from db3.user;
mysql> select sum(uid) from db3.user;
mysql> select count(name) from db3.user where shell != “/bin/bash”;
mysql> select count(*) from db3.user;

	2 查询结果排序 order by  字段  asc/desc ;

mysql> select name ,uid from db3.user where uid <= 1000 order by uid ;
mysql> select name ,uid from db3.user where uid <= 1000 order by uid desc;

	3 查询结果分组 group by  字段名;

mysql> select shell from db3.user where uid <= 100 group by shell ;
mysql> select shell from db3.user group by shell ;

	4 查询结果过滤 having 条件;

mysql> select name from db3.user where uid <= 3000;
mysql> select name from db3.user where uid <= 3000 having name=“mysql”;

	5 限制查询结果显示行数 limit
		只显示查询结果的头几行   limit 数字 ;

mysql> select * from db3.user where id <= 10 ;
mysql> select * from db3.user where id <= 10 limit 1;
mysql> select * from db3.user where id <= 10 limit 2;

		只显示查询结果指定行     limit 起始行,行个数

mysql> select * from db3.user where id <= 10 limit 0,2;
mysql> select * from db3.user where id <= 10 limit 1,2;
mysql> select * from db3.user where id <= 10 limit 3,3;

4 MySQL管理工具
命令行 mysql
安装软件,由软件提供管理数据库的页面(web页面或软件自己提供的图形窗口)

]# scp phpMyAdmin-2.11.11-all-languages.tar.gz root@192.168.4.50:/root/

安装图形工具phpmyadmin
# yum -y install httpd php php-mysql

systemctl start httpd

systemctl enabled httpd

tar -zxvf phpMyAdmin-2.11.11-all-languages.tar.gz

mv phpMyAdmin-2.11.11-all-languages /var/www/html/phpmyadmin

cd /var/www/html/phpmyadmin/

cp config.sample.inc.php config.inc.php //创建主配置文件

vim config.inc.php

17 $cfg[‘blowfish_secret’] = ‘plj123’; //cookies参数
31 cfg[Servers][cfg[‘Servers’][cfg[‘Servers’][i][‘host’] = ‘localhost’; //数据库服务器IP地址

访问软件 http://192.168.4.50/phpmyadmin
用户root
密码
+++++++++++RDBMS_DAY04
数据库服务器192.168.4.50 、192.168.4.51
一、用户授权
1.1 什么是用户授权:在数据库服务器上添加客户端连接时使用的用户,并设置访问权限。

1.2 为什么要学习用户授权:默认只允许数据库管理员root用户本机登陆

1.3 怎样配置用户授权:授权命令格式
mysql> grant  权限列表 on 数据库名 to 用户名@"客户端地址" identified  by  "密码" [ with grant option ] ; 

权限列表: 
个别权限 select , insert  ,update ,delete
	 update(name,uid)

所有权限 all
无权限   usage	

数据库名:
	库名.表名	db3.user
	某个库		db3.*
	所有库/表       *.*

用户名:
	客户端连接时使用的名字,授权时自定义即可

客户端地址:
	本机 localhost
	某1台主机  192.168.4.51
	某个网段   192.168.4.%
	所有主机   %
密码 连接使用密码 ,要与数据库服务器的密码策略匹配

with grant option  可选项,授权用户有执行grant命令的权限
1.4 例子:

mysql> grant all on db4.* to yaya@"%" identified by “123qqq…A”;

mysql> show grants for yaya@"%";

mysql> set password for yaya@"%"=password(“123qqq…A”);

mysql> drop user yaya@"%" ;

客户端连接

]# mysql -h192.168.4.50 -uyaya -p123qqq…A

相关命令
select user();
show grants;
set password=password("123aaa...A");

验证权限 : 建库 建表 对记录做 增删改查

授权库mysql:记录授权信息
查看表记录可以获取用户权限;也可以通过更新记录,修改用户权限

user表 记录已有的授权用户及权限
db表 记录已有授权用户对数据库的访问权限
tables_priv表 记录已有授权用户对表的访问权限
columns_priv表 记录已有授权用户对字段的访问权限

mysql> grant all on . to root@"%" identified by “123qqq…A” with grant option;

mysql> select host ,user from mysql.user;
mysql> select * from mysql.user where user=“root” and host=“localhost”\G;
mysql> show grants from root@"%";

]# mysql -h192.168.4.50 -uroot -p123qqq…A
mysql> show grants;

create database bbsdb;
create table bbsdb.a(name char(10));
insert into db3.user(name)values(“jerry”);

grant all on bbsdb.* to yaya88@“localhost” identified by “123qqq…A”;

grant select,update(name,uid) on db3.user to yaya99@“192.168.4.51” identified by “123qqq…A”;

mysql> show grants for yaya99@“192.168.4.51”;

mysql> select * from mysql.tables_priv
where user=“yaya99” and host=“192.168.4.51” \G;

mysql> update mysql.tables_priv set Table_priv=“select,insert,delete”
where user=“yaya99” and host=“192.168.4.51”;

mysql> flush privileges;

mysql> show grants for yaya99@“192.168.4.51”;

]# mysql -h192.168.4.50 -uyaya99 -p123qqq…A

show grants;

用户授权
验证用户权限
查看用户权限
相关命令
授权库
删除授权用户
撤销权限:删除已有授权用户的权限
命令格式: mysql> revoke 权限列表 on 库名.表 from
用户名@“客户端地址”;

select user ,host from mysql.user;
show grants for root@"%";
revoke grant option on . from root@"%";
show grants for root@"%";
revoke update , delete on . from root@"%";
show grants for root@"%";

revoke all on . from root@"%" ;
mysql> show grants for root@"%";

drop user root@"%";

1.2 root密码
1.2.1 恢复 ( 时间10分钟到 14:57)
]# vim /etc/my.cnf
[mysqld]
skip-grant-tables
:wq

	]# systemctl  restart mysqld
	]# mysql

mysql> update mysql.user set authentication_string=password(“123qqq…A”)
where user=“root” and host=“localhost” ;
mysql> flush privileges;
mysql> exit

	]# vim /etc/my.cnf
		[mysqld]
		#skip-grant-tables
		:wq
	]# systemctl  restart mysqld
	]# mysql -uroot -p123qqq...A

1.2.2 修改

]# mysqladmin -hlocalhost -uroot -p password “abc123…Q”
Enter password:输入旧密码

二、完全备份
数据备份相关概念?
备份方式?
备份策略?完全备份 、 备份新产生的数据(增量备份 或差异备份)

[root@host50 ~]#
201 mkdir /dbbak
202 ls /dbbak/
203 cp -r /var/lib/mysql /dbbak/
204 ls /dbbak/
205 ls /dbbak/mysql/
206 tar -zcvf /dbbak/mysql.tar.gz /var/lib/mysql/*
207 ls /dbbak/
208 scp -r /dbbak/mysql root@192.168.4.51:/root/

[root@host51 ~]#
737 ls /root/mysql
738 systemctl stop mysqld
739 ls /var/lib/mysql
740 rm -rf /var/lib/mysql
741 cp -r /root/mysql /var/lib/
742 ls /var/lib/mysql
743 chown -R mysql:mysql /var/lib/mysql
744 ls -ld /var/lib/mysql
745 ls -l /var/lib/mysql
748 systemctl start mysqld
749 ps -C mysqld
750 netstat -utnlp | grep mysqld
751 mysql -uroot -pabc123…Q
> show databases;

完全备份 命令格式
]#mysqldump -uroot -p密码 库名 > 目录/xxx.sql

备份时库名表示方式:
–all-databases 或 -A //所有库
数据库名 //单个库
数据库名 表名 //单张表
-B 数据库1 数据库2 //多个库

完全恢复命令格式
]#mysql -uroot -p密码 [ 库名 ] < 目录/xxx.sql

]# mysqldump -uroot -pabc123…Q -A > /dbbak/all.sql
]# mysqldump -uroot -pabc123…Q db3 > /dbbak/db3.sql
]# mysqldump -uroot -pabc123…Q db3 user > /dbbak/db3_user.sql
]# mysqldump -uroot -pabc123…Q -B mysql db3 > /dbbak/twodb.sql
[root@host50 ~]# ls /dbbak/*.sql
[root@host50 ~]# scp /dbbak/db3_user.sql root@192.168.4.51:/opt/
[root@host50 ~]# scp -r /dbbak/twodb.sql root@192.168.4.51:/opt/

[root@host51 ~]# mysql -uroot -pabc123…Q db3 < /opt/db3_user.sql
[root@host51 ~]# mysql -uroot -pabc123…Q db3

show tables;
select * from user;

[root@host51 ~]# mysql -uroot -pabc123…Q < /opt/twodb.sql
[root@host51 ~]# mysql -uroot -pabc123…Q

select user ,host from mysql.user;
use db3;
show tables;

标签:name,数据库管理员,DBA,数据库,user,mysql,db3,where,select
来源: https://blog.csdn.net/weixin_44793145/article/details/100364717