Linux Study
Linux平台安装python3.6.4教程
- wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
- tar xf Python-3.6.4 tgz
- cd Python-3.6.4
- yum install -y zlib-devel
- ./configure –prefix=/usr/local/python3
- 如果执行上面这一步时报一个关于C的错误,时由于缺少gcc的库要先安装gcc的库后再执行上面的语句,安装gcc的库:yum install -y gcc
- make && make install
- cd /etc/profile.d
- echo ‘export PATH=$PATH:/usr/local/python3/bin/’ > python3.sh
- 执行完毕后logout重新连接一下。
Linux
通用操作系统
- 任务:任务调度,内存分配,处理外围设备
- 组成:内核和系统程序(shell-人机对话的窗口, 设备驱动,底层库,服务程序等)
- 发展史:由linus Torvalds编写
- free: 自由而非免费
优点
- 通用的操作系统, 不跟特定的硬件绑定。
- 95%是C语言编写,有可移植性。
- 具有内核编程接口
- 支持多用户和多任务(后期团队一起登录,一起开发)
- 支持安全的分层文件系统
- 拥有大量的实用程序
- 进程间通信
- 强大的文档
命令按钮
登录和关机
- adduser student 添加用户
- passwd student 添加用户对应的密码
- shutdown 关机
文件管理
- ls -al ls -l/ 查看其他目录中的文件
- pwd 查看自己所在的目录
- mkdir / rmdir 创建和删除目录 touch / rm -rf 创建和删除文件
- cd ../. 返回上一级 当前目录
- copy / move 复制 和 剪切
- uniq / diff / file 去重/对比/查看文档类型
- find 找文件夹 grep 找内容
显示文件内容
根目录下文件
- root / home / usr / etc / 这是最重要的几个
帮助
下载文件
- wget url
- gzip / gunzip 压缩和解压缩
- tar -cvf / -xvf 压缩和解压缩
- xz/ xz -d 压缩和解压缩
链接
- ln oldfile newfile 硬链接
- ln -s oldfile newfile 软连接
包管理工具
- yum / npm
- install 安装
- list 罗列
- uptate 更新
系统命令
- systemctl start / stop / restart / status
- 防火墙80开洞 firewall-cmd –zone=public –add-port=80/tcp –permanent
- netstat -na | grep 3306 查看自己3306端口是否开启
- netstat -ntlp 查看当前哪些端口在工作
- systemctl enable / disable 设置开关机自动启动
- top 任务管理器 kill -9 进程号 杀进程
- chmod u+x/644 文件名 给文件加上执行的权限 u/g/o
网络命令
- ifconfig ip address 查看自己ip
- ping -s 500 -c 3 www.baidu.com s-单次字节 c-次数 请求3次,每次给我500字节
- netstat -nap 查看端口
- netstat -nap 2>result.txt 错误重定向
- netstat -nap > result.txt 2>error.txt
- wireshark / ethereal 数据窃听,安装此软件,并且把网卡设置为混杂模式
- ssh root@ip 从一个阿里云,登录到另一个阿里云
- sftp root@localhost
- Upload file 上传文件 Download file 下载文件
ping to death分布式拒绝服务攻击 –> 防D 买阿里云的服务
- DDos = Disributed Deny of Service ping包 ——> ping + 网站名
- ms 延迟 发送请求开始到重新接收到信息的时间。
- TCP flood tcp包
配置环境变量
- 用户登录就执行的东西,写在.bash_profile或者.bashrc中
- 开机就执行的程序,写在rc.local中。
- crond 计划任务,时间到了就执行的文件。
- crontab -e 命令来编辑
- 0-59 0-23 1-31 1-12 0-6 command 分钟 小时 天 月 星期 命令
vim 的使用
下载配置文件
- wget + 下载地址 ——下载一个vim的编辑工具,redis.
- .vimrc 新建一个,并在里面配置 set nu set ts-4
- 可以配置一个python3
命令模式 / 底行命令模式 / 编辑模式
命令模式
- G / gg 文末 文首
- ct + f / b 翻页
- 4yyp 复制
- u 撤销
- 4dd / dw 删除行 删除单词
- 宏 qa q 100@a
- 命令模式下,qa 开始录制
- 简单操作
- q 结束录制
- 100@a 将录制的动作执行100次。
底行命令模式
- w! / q! 强行输入和强行退出
- %s/x/y/g 将全文中的x 替换为y, g全局 i忽略大小写 % = (1, $)
- set nu / nonu 设置是否有行号
编辑模式
nginx
- Nginx – HTTP server 当服务器使用-静态页面。 wsgi 网关–动态页面
安装
- yum install nginx
- 将 /usr/share/local/html/index.html 换成自己的网页,就可以通过公网IP访问。
- 阿里云和自己的防火墙上都要开放相应的端口。
- 链接前需要开启nginx
mysql
- RDB / Persistence / SQL – 数据持久化 能存储,能取出。保证数据正确有效,Cluster 多个服务器当跑一个任务,集群 + Load Balance(Nginx / LVS) 负载均衡 + Keepalive 热备,双活。
安装
安装
- yum install mariadb mariadb-server 安装客户端和服务器
- systemctl start mariadb 启动客户端
特点
- 不区分大小写
- not null 非空约束,不能为空
- varchar(20) 变长字符串,varible charactor, 最少一个字符,最多20个字符。
- 命名规则,见名知义,前缀
- 句内注释 comment ‘注释’ 句外注释 – 注释
- 不能用双引号,只能用单引号。
- 数据以表格的形式出现;
- 每行为记录名称;
- 每列记录名称所对应的数据域
- 列和行组成一张表单
- 若干的表单组成database;
连接
- mysql -u root -p 连接自己
- mysql -h id -u 用户名 -p 连接别人
使用
- show databases; 查看有哪些数据库
- create databases
- 字符串类型
- char varchar tinyblob tinytext blob text很长的描述型的东西,备注
- create 创建表
- not null 不为空 unique
- into_increment 自增 一般用于主键。
- drop 删除表
- alter 更新表
- insert into … set … 插入数据
- delete from … where … 删除数据
- updata … set .. where .. 更新数据
- select … from … where .. 查询
drop database if exists company;
create database company default charset utf8;
use company;
drop table if exists tb_dept;
create table tb_dept
(
deptno integer not null comment '部门编号',
dname varchar(20) not null comment '名称',
dloc varchar(10) comment '所在地',
primary key (deptno)
);
alter table tb_dept add ddate date comment '成立日期';
insert into tb_dept values (10, '财务部', '成都', now());
insert into tb_dept (deptno, dname) values (20, '研发部');
insert into tb_dept (deptno, dname) values (30, '销售1部'), (40, '销售2部'), (50, '后勤保障部');
delete from tb_dept where deptno=50;
update tb_dept set dloc='深圳', ddate='2018-4-1' where deptno='30';
select * from tb_dept;
select deptno, dname from tb_dept;
select deptno as 部门编号, dname as 部门名称 from tb_dept;
select deptno,dname from tb_dept where dloc='深圳';
drop database if exists HR;
create database HR default charset utf8;
use HR;
drop table if exists TbDept;
create table TbDept
(
deptno int primary key not null comment '部门编号',
dname varchar(10) not null comment '部门名称',
dloc varchar(20) not null
);
insert into TbDept values (10, '会计部', '北京');
insert into TbDept values (20, '研发部', '成都');
insert into TbDept values (30, '销售部', '重庆');
insert into TbDept values (40, '运维部', '深圳');
drop table if exists TbEmp;
create table TbEmp
(
empno int primary key not null comment '员工编号',
ename varchar(20),
job varchar(20),
mgr int,
sal int not null,
dno int
);
alter table TbEmp add constraint fk_dno foreign key(dno) references TbDept(deptno);
insert into tbemp values
(7800, '张三丰', '总裁', null, 9000, 20),
(2056, '乔峰', '分析师', 7800, 5000, 20),
(3088, '李莫愁', '设计师', 2056, 3500, 20),
(3211, '张无忌', '程序员', 2056, 3200, 20),
(3233, '丘处机', '程序员', 2056, 3400, 20),
(3251, '张翠山', '程序员', 2056, 4000, 20),
(5566, '宋明月', '会计师', 7800, 4000, 10),
(5234, '郭靖', '出纳', 5566, 2000, 10),
(3344, '黄蓉', '销售主管', 7800, 3000, 30),
(1359, '胡一刀', '销售员', 3344, 1800, 30),
(4466, '苗人凤', '销售员', 3344, 2500, 30),
(3244, '欧阳锋', '程序员', 3088, 3200, 20),
(3577, '杨过', '会计师', 5566, 2200, 10),
(3588, '朱九真', '会计师', 5566, 2200, 10);
select sal, ename from tbemp
where sal = (
select max(sal) from tbemp);
select ename, sal * 12 as 年薪 from tbemp;
select count(dno)'人数',dno '部门编号',dname, from tbemp e join tbdept d on e.dno=d.deptno group by (e.dno);
select t1.deptno,t1.dname,ifnull(t2.c,0) from tbdept t1
left join
(select dno,count(*) as c from tbemp group by dno) t2
on t1.deptno = t2.dno
-- 4.查询所有部门的名称和人数
select count(dno) '人数', d.dname '部门名称' from tbemp e join tbdept d on e.dno=d.deptno group by (d.deptno);
select ename, sal from tbemp t1
where t1.sal = (
select max(t.sal) as maxsal from tbemp t
where t.mgr is not null) ;
select ename '姓名', sal '工资' from tbemp where sal > (select avg(sal) from tbemp);
select e.ename '姓名', d.deptno '部门编号', e.sal '工资' from tbemp e join tbdept d on d.deptno=e.dno where sal > (select avg(sal) from tbemp where dno=10) and e.dno=10 ;
select e.ename '姓名', d.deptno '部门编号', e.sal '工资' from tbemp e join tbdept d on d.deptno=e.dno where sal > (select avg(sal) from tbemp where dno=20) and e.dno=20 ;
select e.ename '姓名', d.deptno '部门编号', e.sal '工资' from tbemp e join tbdept d on d.deptno=e.dno where sal > (select avg(sal) from tbemp where dno=30) and e.dno=30 ;
select * from tbdept t3
join(
select t1.dno, ename, sal from tbemp t1
join(
select avg(sal) as avgsal, dno from tbemp group by dno) t2
on t1.dno = t2.dno and t1.sal > avgsal) t4
on t3.deptno = t4.dno
-- 8. 查询部门中薪水最高的人的姓名,工资和所在部门名称
select * from tbdept t3
join(
select ename,t1.dno,sal from tbemp t1
join (
select max(sal) as maxsal, dno from tbemp group by dno) t2
on t1.dno = t2.dno and maxsal=sal) as t4
on t3.deptno = t4.dno
-- 9. 查询主管的姓名和职位
select ename, job from tbemp
where empno in (
select distinct mgr from tbemp where mgr is not null)
-- 10 查询薪水排名的前三的员工姓名和工资
select sal,ename from tbemp order by (-sal) limit 3;
select sal,ename from tbemp order by (-sal) limit 5 offset 3;
drop database if exists SRS;
create database SRS default charset utf8;
use SRS;
drop table if exists TbStudent;
create table TbStudent(
stuid int(15) primary key not null,
stuname varchar(20) not null,
stusex int(1) default 1,
stubirth date,
stutel int(11),
stuaddr varchar(30),
stuphoto blob
);
alter table tbstudent drop column stutel;
drop table if exists tbcourse;
create table tbcourse
(
cosid int primary key ,
cosname varchar(20),
coscredit float,
cosintro varchar(30)
);
drop table if exists tbsc;
create table tbsc
(
scid integer primary key auto_increment,
sid integer not null,
cid integer,
scdate datetime not null,
score float
);
alter table tbsc add constraint fk_sid foreign key (sid) references tbstudent (stuid) on delete cascade on update cascade;
alter table tbsc add constraint fk_cid foreign key (cid) references tbcourse (cosid) on delete set null on update cascade;
insert into tbstudent values
(1001, '张三丰', default, '1978-1-1','成都市一环路西二段17号',null);
insert into tbstudent (stuid, stuname, stubirth) values
(1002, '郭靖', '1980-2-2');
insert into tbstudent (stuid, stuname, stusex, stubirth, stuaddr) values
(1003, '黄蓉', 0, '1982-3-3', '成都市二环路南四段123号');
insert into tbstudent values
(1004, '张无忌', 1, '1990-4-4', null, null),
(1005, '丘处机',1, '1983-5-5', '北京市海淀区宝盛北里西区28号',null),
(1006, '王处一',1, '1985-6-6', '深圳市宝安区宝安大道5010号',null),
(1007, '刘处玄',1, '1987-7-7', '郑州市井水去维五路21号',null),
(1008, '孙不二',0, '1989-8-8', '武汉市光谷大道61号',null),
(1009, '平一指',1, '1992-9-9', '西安市雁塔区高新六路52号',null),
(1010, '老不死',1, '1993-10-10', '广州市天河区元岗路310号',null),
(1011, '王大锤',0, '1994-11-11',null,null),
(1012, '隔壁老王',1, '1995-12-12',null,null),
(1013, '郭啸天',1, '1977-10-25', null,null);
delete from tbstudent where stuid=1004;
update tbstudent set stubirth='1980-12-12', stuaddr='上海市宝山区同济支路199号' where stuid=1002;
insert into tbcourse values
(1111, 'C语言设计', 3, '大神级讲师授课需要抢座'),
(2222,'Java程序设计',3, null),
(3333, '数据库概论', 2, null),
(4444, '操作系统原理', 4, null);
insert into tbsc values
(default, 1001, 1111, '2016-9-1',95),
(default, 1002, 1111, '2016-9-1',94),
(default, 1001, 2222, now(),null),
(default, 1001, 3333, '2017-3-1',85),
(default, 1001, 4444, now(), null),
(default, 1002, 4444, now(), null),
(default, 1003, 2222, now(), null),
(default, 1003, 3333, now(), null),
(default, 1005, 2222, now(), null),
(default, 1006, 1111, now(), null),
(default, 1006, 2222, '2017-3-1', 80),
(default, 1006, 3333, now(), null),
(default, 1006, 4444, now(), null),
(default, 1007, 1111, '2016-9-1',null),
(default, 1007, 3333, now(), null),
(default, 1007, 4444, now(), null),
(default, 1008, 2222, now(), null),
(default, 1010, 1111, now(), null);
select * from tbsc sc
join tbstudent st on st.stuid=sc.sid
join tbcourse co on co.cosid=sc.cid;
select cosname, coscredit from tbcourse;
select stuname, stubirth from tbstudent where stusex=0;
select stuname, stusex, stubirth from tbstudent where stubirth between '1980-1-1' and '1990-1-1';
select stuname, stusex from tbstudent where stuname like "王%";
select stuname from tbstudent where stuname like "郭_";
select stuname from tbstudent where stuname like "郭__";
select stuname from tbstudent where stuname like "%王%";
select stuname from tbstudent where stuaddr is null and stuphoto is null;
select distinct scdate from tbsc;
select stuname, stubirth from tbstudent order by stubirth;
select stuname, stubirth, stuaddr from tbstudent where stuaddr is not null and stusex=1 order by -stubirth;
select min(stubirth) from tbstudent ;
select max(stubirth) from tbstudent ;
select count(stusex), stusex from tbstudent group by (stusex);
select avg(score) from tbsc where cid=1111;
select sum(score) from tbsc where sid=1001;
select stuid, ifnull(sc.avg,0) from tbstudent st left join
(select avg(tbsc.score) as avg, sid from tbsc group by sid) as sc
on sc.sid=st.stuid;
select avgscore, sid from
(select avg(ifnull(score,0) as avg) as avgscore, sid from tbsc group by sid) where avgscore >= 90;
select stuid, ifnull(sc.avg,0) from tbstudent st left join
(select avg(tbsc.score) as avg, sid from tbsc group by sid) as sc
on sc.sid=st.stuid where sc.avg >= "90";
select stuname from tbstudent t1
where t1.stubirth=(
select min(t.stubirth) from tbstudent as t) ;
select * from tbstudent st
join (
select count(cid) as cot, sid from tbsc group by sid) sc
on sc.sid=st.stuid where sc.cot > 2;
select stuname, avg(ifnull(score,0)) from
tbstudent st join tbsc sc on sc.sid=st.stuid group by sid and sc.cid is not null;
select stuname, ifnull(sc.avg,0) from tbstudent st join (
select avg(score) as avg, sid from tbsc group by sid) as sc
on sc.sid=st.stuid;
select stuname, cosname,score from tbsc sc
join tbcourse co on sc.cid=co.cosid
join tbstudent st on st.stuid=sc.sid;
select count(cosid), stuname from tbsc sc
join tbcourse co on sc.cid=co.cosid
join tbstudent st on st.stuid=sc.sid
group by sid;
redis REmote Dlctionary Server
简介
- 是一个有Salvatore Sanfilippo写的key-value存储系统
- 它时一个开源的使用ANSI C语言编写 遵守BSD协议 支持网络 可基于内存持久化的日志型 key-value数据库,并提供多张语言的API
- 通常被称为数据结构服务器,因为值values可以是 String Map list sets sorted sets等类型
特点
- 支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用
- 支持key-value list set zset hash等数据结构的存储
- 支持数据的备份,即master-slave模式的数据备份
优势
- 读 110000次/秒 写81000次/秒
- 支持二进制的Strings, Lists , Hashes, Sets, Ordered Sets
- 原子 – 原子性。
- 支持 publish / subscribe , 通知 , key 过期等特性
使用redis redis-client
- 启动
- redis-server / redis-cli进入。
- 后台运行
- vim /etc/redis.conf 进入redis的配置文件
- redis-server –help
redis-server - 访问指定IP: 端口的redis服务器
- redis-cli -h ip -p 端口
- 例子 redis-cli -h 172.26.77.93 -p 6379
- redis 五大类型; string类型 hash list set dict
- set p 1 设置 p 的值为1
- get p 获取p 的值
- incr p 整数递增 ,默认值是零
- decr p 整数递减,用在抢票的时候。
string 字符串
- 最基本的数据类型,可以包含任何数据,一个键最大存储512MB
- 使用 set 和 get 命令。
hash 哈希
- 键值对集合 key => value
- 特别适合存储对象,每个hash可以存储2^32-1
- 使用 hmset 和 hget 命令
list 列表
- redis列表是简单的字符串列表,按照插入顺序排列,你可以添加一个原色到列表的头部或尾部
- 可以存储2^32-1
set 集合
- set 是 string 类型的无序集合
- 集合是通过哈希表实现的,所以添加, 删除,查找的复杂度都是0