分类 Linux 下的文章

ping可以用于测试网络连通性。
语法:

ping ip/域名

例:

ping baidu.com

一直ping
语法:

ping ip/域名 -t

例:

ping qq.com -t

选择指定列:

select name,age from user; //选择name列和age列

返回不重复的值:

select distinct imgname from bk_img;

where 条件,特殊条件:

1.is null (空值判断)
select * from database where name is null;

2.between and (在 之间的值)
select * from database where sal between 1500 and 3000; //sal列中大于1500小于3000的值,包括1500和3000

3.In
select * from database where sal in (5000,1500,3000) //sal列中等于5000,1500,3000的值

4.like 模糊查询
select * from database where emname like 'M%';
% 表示多个字值
M% 为通配符,正则表达式,表示的意思是模糊查询信息为 M 开头的
%M% 表示查询包含 M 的所有内容
%M_ 表示查询以M在倒数第二位的所有内容
%a' //以a结尾的数据
'a%' //以a开头的数据
'%a%' //含有a的数据
'_a_' //三位且中间字母是a的
'_a' //两位且结尾字母是a的
'a_' //两位且开头字母是a的

5.不带比较运算符的 WHERE 子句:

WHERE 子句并不一定带比较运算符,当不带运算符时,会执行一个隐式转换。当 0 时转化为 false,1 转化为 true。例如:

SELECT studentNO FROM student WHERE 0
则会返回一个空集,因为每一行记录 WHERE 都返回 false。

SELECT studentNO FROM student WHERE 1
返回 student 表所有行中 studentNO 列的值。因为每一行记录 WHERE 都返回 true。

order by 关键字排序:

select * from database order by time desc; //按照 time 进行降序排序,desc 降序,asc升序;

order by 多列:

select * from database order by create_time,updata_time desc; // 多列排序

指定多列排序的排序顺序:

order by A,B 这个时候都是默认按升序排列
order by A desc,B 这个时候 A 降序,B 升序排列
order by A ,B desc 这个时候 A 升序,B 降序排列

inert into 插入数据:

insert into select 和select into from 的区别:

insert into scorebak select * from socre where neza='neza' --插入一行,要求表scorebak 必须存在

select * into scorebak from score where neza='neza' --也是插入一行,要求表scorebak 不存在

update 语句:
update bk_admin set username = 'test123' where username = 'test'; //修改username 等于 test 为 test123(Where语句为必须语句,否则更新整个表!!!)

执行没有 WHERE 子句的 UPDATE 要慎重,再慎重。

在 MySQL 中可以通过设置 sql_safe_updates 这个自带的参数来解决,当该参数开启的情况下,你必须在update 语句后携带 where 条件,否则就会报错。

set sql_safe_updates=1; 表示开启该参数

delete 语句:
delete from bk_admin where username = 'test123'; //删除 username = test123的行

SQL关于删除的三个语句:DROP、TRUNCATE、 DELETE 的区别。

DROP:

DROP test;
删除表test,并释放空间,将test删除的一干二净。

TRUNCATE:

TRUNCATE test;
删除表test里的内容,并释放空间,但不删除表的定义,表的结构还在。

DELETE:

1、删除指定数据

删除表test中年龄等于30的且国家为US的数据

DELETE FROM test WHERE age=30 AND country='US';
2、删除整个表

仅删除表test内的所有内容,保留表的定义,不释放空间。

DELETE FROM test 或者 DELETE FROM test;
DELETE FROM test 或者 DELETE FROM test;

LIMIT 选择指定数量行数:

select * from bk_img limit 10; //选择10行

select * from bk_img limit 5,10; //第5行开始,到第10行结束

通配符:

% 通配符:
SELECT * FROM Websites WHERE url LIKE
'https%'; //以某个字符串开头的记录
'%https%'; //包含https的记录

_ 通配符:

下面的 SQL 语句选取 name 以一个任意字符开始,然后是 "oogle" 的所有客户:
SELECT * FROM Websites
WHERE name LIKE '_oogle';
_ 号表示一个任意字符,可以这样使用:'_o_g_le'

[charlist]通配符(正则表达式):

Mysql使用 REGEXP 或 NOT REGEXP 运算符来操作正则表达式

select * from Websites where name REGEXP '^[GFS]'; //查询以G、F、s 开头的记录

SQL别名:
创建别名是为了让列名称可读性更强。

列的 SQL 别名语法:
SELECT column_name AS alias_name FROM table_name;

表的 SQL 别名语法:
SELECT column_name(s) FROM table_name AS alias_name;

JOIN 连接:

select Websites.id, Websites.name, access_log.count, access_log.date
from Websites
INNER JOIN access_log
ON Websites.id = access_log.site_id;

INNER JOIN:如果表中有至少一个匹配,则返回行
LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
FULL OUTER JOIN:只要其中一个表中存在匹配,则返回行

UNION 操作符:
用于合并两个或者多个select语句的结果集,select 语句必须要有相同列和相似的数据类型,同时,每个select语句的列的顺序必须相同。

select country from Websites
UNION
select country from apps
order by country; //以上语句返回不同的值,会过滤重复的值。

UNION ALL 操作符:

同上UNION操作符,不同的是会返回所有的值,包括重复的值。

select country from Websites
UNION ALL
select country from apps
order by country; //以上语句返回所有的值,不会过滤重复的值。

INSERT INTO SELECT 语句:
从一个表复制数据,然后把数据复制到一个已经存在的表中,目标表的数据不收影响。

insert into Websites (name, country)
select app_name, country from apps; //复制 apps 中的 app_name 列到 Websies表中

insert into Websites (name, country)
select app_name, country from apps
where id = 1; //只复制id为一的列

CREATE语句:

CREATE DATABASE dbname; //创建一个名字为 dbname 的数据库;

CREATE TABLE userinfo
(
Id int,
username varchar(255),
password varchar(255),
address varchar(255)
); //新建一个数据表

CREATE TABLE 约束:

NO NULL:不能为空 //id int NO NULL

UNIQUE:唯一性。
Mysql写法:
CREATE TABLE tb2(
id int no null,
name varchar(255) no null,
UNIQUE(id)
);

PRIMARY KEY 主键:
每一个表都应该有主键,并且每个表只能有一个主键。

新建表时创建主键:
CREATE TABLE tb2
(
id int NOT NULL,
username varchar(255),
password varchar(255),
PRIMARY KEY (id) //设置id为主键
);

当表已经创建时创建主键:
ALTER TABLE tb2
ADD PRIMARY KEY (id);

撤销主键:
Mysql:
ALTER TABLE tb2
DROP PRIMARY KEY;

FOREIGN KEY 约束:
外键约束。防止非法插入数据。

新建表时设置外键:
CREATE TABLE order
(
O_id int NOT NULL,
orderno int NOT NULL,
P_id int,
PRIMARY KEY (O_id), // O_id 为主键键
FOREIGN KEY (P_id) REFERENCES persons(P_id) // P_id 为 persons 表的P_id
);

当表已经存在时设置外键:

ALER TABLE order
ADD FOREIGN KEY (P_id)
REFERENCES persons (P_id);

撤销 FOREIGN KEY 约束:
Mysql:
ALTER TABLE Order
DROP FOREIGN KEY persons;

CHECK 约束:
用于限制列中的值得范围。
如果对单个列定义CHECK约束,那么该列只允许特定的值。
表定义 ,那么此约束会基于行中其他列的值在特定的列中对值进行限制。

新建表时 CHECK 约束:
CREATE TABLE user
(
id int NOT NULL,
username varchar(255),
password varchar(255),
CHECK (username > 0) //限制 username字段 大于 0
)

多个列的 CHECK 约束:

CHECK (username > 1 AND password > 0); 限制 username 字段大于1 和 password 字段大于0

当表已经存在时 CHECK 约束:
Mysql:
ALTER TABLE user
ADD CHECK (username > 0);

撤销 CHECK 约束:
ALTER TABLE user
DROP CONSTRAINT username;

DEFAULT 约束:
用于向列中插入默认值,设置默认值。

新建表时设置默认值:
CREATE TABLE user
(
id int NOT NULL,
username varchar(255) NOT NULL,
password varchar(255) DEFAULT '123456' //设置 passwdord 字段的默认值为 123456
);

当表已经创建的时候设置默认值:
ALTER TABLE testdb
ALTER username SET DEFALT '123456'; //设置username字段默认值

安装所需环境

Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 centos 7 作为安装环境。

一. gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install -y gcc-c++

二. PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

三. zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

四. OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

下载

1.直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html

9f0c9a14a5e0bdc644fb5316ab49bfba.png

2.使用wget命令下载(推荐)。确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装。

wget -c https://nginx.org/download/nginx-1.15.8.tar.gz

解压
依然是直接命令:

tar -zxvf nginx-1.15.8 .tar.gz  

cd nginx-1.15.8

配置
其实在 nginx-1.12.0 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。

1.使用默认配置

./configure

2.自定义配置(不推荐)

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

编译安装

make
make install

查找安装路径:

whereis nginx
启动、停止nginx
cd /usr/local/nginx/sbin/    #切换目录
./nginx   #启动 Nginx
./nginx -s stop  #停止 Nginx
./nginx -s quit   #停止 Nginx
./nginx -s reload  #重启 Nginx
./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。  

查询nginx进程:

ps aux|grep nginx

重启 nginx

1.先停止再启动(推荐):
对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:

./nginx -s quit
./nginx

2.重新加载配置文件:
当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:

./nginx -s reload

启动成功后,在浏览器可以看到这样的页面:

36a59597ad0a9695073e6204ae8fab23.png

-----------------------------------------------分割线--------------------------------------------------

如果不能访问,应该是防火墙没开放端口,解决方法如下:

1.直接关闭防火墙(不推荐)

查询防火墙状态:

service firewalld status

active 看到防火墙是开着的,我们把它给停止,命令如下:

service firewaddl stop

设置开机不自启动:

systemctl disable firewalld.service

2.防火墙放行80端口(推荐):

firewall-cmd --zone=public --add-port=80/tcp --permanent

-----------------------------------------------分割线--------------------------------------------------

设置Nginx自启动

即在rc.local增加启动代码就可以了。

vi /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx 

设置执行权限:

chmod 755 rc.local

参考文章:

https://www.cnblogs.com/liujuncm5/p/6713784.html

总是出现Exiting on user cancel,导致不能正常安装。

这是yum的一个bug导致的问题。修改/usr/lib/python2.7/site-packages/urlgrabber/grabber.py

vi /usr/lib/python2.7/site-packages/urlgrabber/grabber.py
1510             elif errcode == 42:
1511                 # this is probably wrong but ultimately this is what happens
1512                 # we have a legit http code and a pycurl 'writer failed' code
1513                 # which almost always means something aborted it from outside
1514                 # since we cannot know what it is -I'm banking on it being
1515                 # a ctrl-c. XXXX - if there's a way of going back two raises to
1516                 # figure out what aborted the pycurl process FIXME
1517                 raise KeyboardInterrupt

修改后:

1510             #elif errcode == 42:
1511                 # this is probably wrong but ultimately this is what happens
1512                 # we have a legit http code and a pycurl 'writer failed' code
1513                 # which almost always means something aborted it from outside
1514                 # since we cannot know what it is -I'm banking on it being
1515                 # a ctrl-c. XXXX - if there's a way of going back two raises to
1516                 # figure out what aborted the pycurl process FIXME
1517             #    raise KeyboardInterrupt

然后以root用户运行如下命令升级:

yum clean metadata
yum clean all
yum upgrade

升级完成后运行yum命令安装即可。