哈特中尉's Blog

不会写代码的司机不是好厨师!

tomcat准备

本地成功启动两个tomcat
tomcatA 8080 webapps/cms/index.html文件的内容tomcatA
tomcatB 8081 webapps/cms/index.html文件的内容tomcatA

tomcatA http://localhost:8080/cms/index.html
tomcatB http://localhost:8081/cms/index.html

配置nginx

nginx.conf 配置内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
http{
upstream cmsCluster{
server localhost:8080 weight=1;
server localhost:8081 weight=1;
}
server{
listen 8090;
server_name localhost;
location /{
proxy_pass http://cmsCluster;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location = /50x.html {
root html;
}
}
}

nginx http://localhost:8090/cms/index.html

不停刷新页面,看内容变化。负载均衡完成。

catalina.out’: Permission denied解决方法

1
sudo chmod a+rwx -R logs

所有人可操作logs文件
chmod说明(u:与文件属主拥有一样的权限[a:所有人];+:增加权限;rwx:可读可写可执行)-R:递归所有目录和文件

tomcat启动时SessionIdGeneratorBase.createSecureRandom耗时5分钟的问题

vi $JAVA_PATH/jre/lib/security/java.security
securerandom.source替换成
securerandom.source=file:/dev/./urandom

原理:http://blog.csdn.net/chszs/article/details/49494701

server.xml配置外部项目

conf/server.xml文件hosts节点下增加如下内容

1
2
<Context path="/demo" docBase="D:\develop\git4projects\demo\target\hxch-1.0" 
debug="9" reloadable="false" verbosity="4"/>

查看java进程并结束

  • 查看Java进程获取pid号
    1
    ps -ef|grep java|grep -v grep
  • 杀死进程
    1
    kill -9 4834 

查看端口占用程序并结束

1
2
3
4
5
6
7
netstat -lnp|grep 8000
ps 1111
kill -9 1111

pgrep nginx
kill -quit 1233

查询nginx主进程号

ps -ef | grep nginx

  • 停止进程
    kill -QUIT 11111 #主进程号
  • 快速停止
    kill -TERM 11111 #主进程号
  • 强制停止
    pkill -9 nginx

常用zip相关命令

  • 安装zip/unzip
    1
    yum -y install zip unzip
  • 压缩文件/目录
    例如:mydata目录压缩为mydata.zip
    1
    zip -r mydata.zip mydata
  • 解压
    -d指定目录,缺省为当前目录
    1
    2
    unzip mydata.zip
    unzip mydata.zip -d mydatabak

显示当前所在目录

pwd

查看所有java进程

1
ps -ef|grep java|grep -v grep

杀死所有Java进程

ps -ef|grep 'java' |grep -v grep|cut -c 9-15|xargs kill -9 >/dev/null 2>&1
或者
pkill -9 java

百度百科SELinux介绍
Linux 下为何要关闭 SELinux? - 知乎
参考文献:查看 SELinux状态及关闭SELinux

查看SELinux状态:

  • /usr/sbin/sestatus -v
    如果SELinux status参数为enabled即为开启状态

  • 或者 getenforce
    Permissive

关闭SELinux:

  • 临时关闭(不用重启机器):
    setenforce 0 ##设置SELinux 成为permissive模式
    setenforce 1 ##设置SELinux 成为enforcing模式

  • 修改配置文件(需要重启机器):
    修改 文件
    vi /etc/selinux/config
    SELINUX=enforcing改为SELINUX=disabled

  • 重启机器生效

安装(需要 Ruby)

1
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

使用

搜索:brew search mysql
查询:brew info mysql 主要看具体的信息,比如目前的版本,依赖,安装后注意事项等
更新:brew update 这会更新 Homebrew 自己,并且使得接下来的两个操作有意义——
检查过时(是否有新版本):brew outdated 这回列出所有安装的软件里可以升级的那些
升级:brew upgrade 升级所有可以升级的软件们
清理:brew cleanup 清理不需要的版本极其安装包缓存
常用的就这些。一般来说如果你追求新版本(不升级不舒服斯基),那么你最常用的操作序列就是这样:

更多使用参考:https://blog.csdn.net/u010275932/article/details/76080833

编写要执行的脚本back.sh

1
2
3
#!/bin/sh
date=$(date +%Y%m%d%H%M)
mysqldump -h127.0.0.1 data11 > $date.sql

使脚本可以执行

1
chmod +x back.sh

添加 crontab任务

1
crontab -e

内容如下:
每分钟执行一次~/back.sh文件

1
* * * * * ~/back.sh

禁止产生邮件

1
crontab -e

第一行加入:

1
MAILTO=""

安装crond服务(可选)

1
2
yum install vixie-cron
yum install crontabs

说明:
vixie-cron软件包是cron的主程序;
crontabs软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。

启动cron服务

cron 是linux的内置服务,但它不自动起来。

1
2
3
4
5
6
7
8
9
10
11
12
#启动服务
/sbin/service crond start
#关闭服务
/sbin/service crond stop
#重启服务
/sbin/service crond restart
#重新载入配置
/sbin/service crond reload
#查看crontab服务状态
service crond status
#手动启动crontab服务
service crond start

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
编辑root下的定时任务
crontab -u root -e

每天早上6点追加一条字符串到一个文本。
6 * * * echo "Good morning." >> /tmp/test.txt

每两个小时追加一条字符串一个文本。
*/2 * * * echo "Have a break now." >> /tmp/test.txt

晚上11点到早上8点之间每两个小时,早上八点
23-7/2,8 * * * echo "Have a good dream:)" >> /tmp/test.txt

每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
11 4 * 1-3 command line

1月1日早上4点
4 1 1 * command line

每月每天每小时的第 0 分钟执行一次 /bin/ls
* * * * /bin/ls

在 12 月内, 每天的早上 6 点到 12 点中,每隔 20 分钟执行一次 /usr/bin/backup
*/20 6-12 * 12 * /usr/bin/backup

周一到周五每天下午 5:00 寄一封信给 alex_mail_name :
17 * * 1-5 mail -s "hi" alex_mail_name < /tmp/maildata

每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha"
0-23/2 * * * echo "haha"

晚上11点到早上8点之间每两个小时,早上8点,显示时间
23-7/2,8 * * * date

Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。

从2011年12月开始,Tengine成为一个开源项目,Tengine团队在积极地开发和维护着它。Tengine团队的核心成员来自于淘宝、搜狗等互联网企业。

更多介绍 http://tengine.taobao.org/

安装依赖

1
2
3
4
yum -y install openssl
yum -y install openssl-devel
yum -y install pcre
yum -y install pcre-devel

下载安装包

1
wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz

释放文件

1
tar -zxvf tengine-2.2.0.tar.gz

执行配置

1
2
cd tengine-2.2.0
./configure --prefix=/usr/local/nginx

编译安装

1
make && make install

启动nginx

1
2
cd /usr/local/nginx/sbin
./nginx

查看结果

1
2
netstat -tln | grep 80
curl http://localhost

如果局域网其他机器无法访问,请检查防火墙配置。

yum安装

1
2
yum install nginx
service nginx start

停止firewall

1
systemctl stop firewalld.service

禁止firewall开机启动·1

1
systemctl disable firewalld.service

查看默认防火墙状态

1
firewall-cmd --state

(关闭后显示notrunning,开启后显示running)

查看防火墙状态

systemctl status firewalld.service

启动防火墙

systemctl start firewalld.service

关闭防火墙

systemctl stop firewalld.service
##Add
firewall-cmd –permanent –zone=public –add-port=80/tcp
firewall-cmd –permanent –zone=public –add-port=443/tcp
firewall-cmd –permanent –zone=public –add-port=22/tcp
##Remove
firewall-cmd –permanent –zone=public –remove-port=80/tcp
##Reload
firewall-cmd –reload

查看所有打开的端口:
firewall-cmd –list-port
firewall-cmd –zone=public –list-ports

http://www.centoscn.com/CentOS/Intermediate/2016/0602/7348.html

CentOS7 的防火墙配置跟以前版本有很大区别,经过大量尝试,终于找到解决问题的关键
CentOS7这个版本的防火墙默认使用的是firewall,与之前的版本使用iptables不一样。按如下方便配置防火墙:
1、关闭防火墙:sudo systemctl stop firewalld.service
2、关闭开机启动:sudo systemctl disable firewalld.service

3、安装iptables防火墙
执行以下命令安装iptables防火墙:sudo yum install iptables-services
4、配置iptables防火墙,打开指定端口(具体跟以前版本一样,网上介绍很多,这里不多介绍了)
5. 设置iptables防火墙开机启动:sudo systemctl enable iptables

更多参考
http://www.cnblogs.com/liuzk552/p/4607336.html

安装依赖包(centos-min安装)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yum -y install libxml2
yum -y install libxml2-devel
yum -y install openssl
yum -y install openssl-devel
yum -y install curl
yum -y install curl-devel
yum -y install libjpeg
yum -y install libjpeg-devel
yum -y install libpng
yum -y install libpng-devel
yum -y install freetype
yum -y install freetype-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install libxslt
yum -y install libxslt-devel
yum -y install bzip2
yum -y install bzip2-devel
安装openssl(可选)
1
2
3
4
5
6
wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz
tar -xzvf openssl-1.1.0f.tar.gz
cd openssl-1.1.0f
./config --prefix=/usr/local --openssldir=/usr/local/ssl
make && make install
openssl version -a

下载php安装包

1
wget http://php.net/distributions/php-7.1.7.tar.gz

释放安装包

1
2
tar -xvzf php-7.1.7.tar.gz
cd php-7.1.7

执行配置

1
./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip

实际上这里的配置项比上述还多,可以使用 ./configure --help 命令查看所有选项,
这里注意在php7中--with-mysql原生支持已经不存在了,操作都变成mysqli或者pdo了;
以上这些选项在正常的php开发中完全够用了,后期如果需要,可以选择手动开启相应的模块.

执行编译

1
make

执行安装:

1
make install

配置相应的文件

php的默认安装位置上面已经指定为/usr/local/php

1
2
3
cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin

设置php.ini

1
vi /usr/local/php/lib/php.ini

775行左右,设置 cgi.fix_pathinfo=0

默认被注释并且值为1,根据官方文档的说明,这里为了当文件不存在时,阻止Nginx将请求发送到后端的PHP-FPM模块,从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为0

关于php.ini文件位置

php.ini配置文件的位置可以在编译前配置参数中设置,编译参数可以写成:--with-config-file-path=/usr/local/php 这样的话php就回去指定的目录下读取php.ini配置文件,如果不加这个参数默认位置就是php安装目录下的lib目录,具体也可以在phpinfo()输出界面查看。

创建php-fpm用户

groupadd web
useradd -g web web

配置php-fpm用户用户

1
2
3
cd /usr/local/php/etc/php-fpm.d
cp www.conf.default www.conf
vi www.conf
1
2
user = web
group = web

默认user和group的设置为nobody,将其改为web

启动php-fpm服务

1
/usr/local/bin/php-fpm

查看启动结果

php-fpm服务默认端口9000

1
netstat -tln | grep 9000

nginx支持php

1
vim /usr/local/nginx/conf/nginx.conf

配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:

1
2
3
4
5
6
7
8
location ~* \.php$ {
root html/chanzhieps/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $request_uri;
include fastcgi_params;
}

nginx的用户

回到nginx.conf第一行,默认是#user nobody;
这里要去掉注释改为user web;或者user web web;表示nginx服务器的权限为web.

重启nginx

1
2
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx

测试php文件解析

在nginx下的html目录下创建info.php文件

1
2
3
<?php
phpinfo();
?>

http://192.168.0.100/info.php

查看已安装的mysql

1
yum list installed | grep mysql

卸载已经安装

1
yum -y remove mysql-libs.x86_64

配置yum源

  • 下载mysql源安装包

    1
    wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
  • 安装mysql源

    1
    yum localinstall mysql57-community-release-el7-11.noarch.rpm
  • 检查mysql源是否安装成功

    1
    yum repolist enabled | grep "mysql.*-community.*"
  • 查看yum库上的mysql版本信息
    yum list | grep mysql yum -y list mysql*

安装MySQL

1
yum -y install mysql-community-server

或者

1
yum -y install mysql-server mysql mysql-devel

启动MySQL服务

1
systemctl start mysqld

查看MySQL的启动状态

1
systemctl status mysqld

开机启动

1
2
systemctl enable mysqld
systemctl daemon-reload

修改root默认密码

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。
通过下面的方式找到root默认密码,然后登录mysql进行修改:

1
2
3
grep 'temporary password' /var/log/mysqld.log
mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root@2017!';

或者

1
set password for 'root'@'localhost'=password('root@2017!');

注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误.

忘记root密码解决办法

vim /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
例如:

1
2
3
4
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables

重新启动mysqld
service mysqld restart
登录Mysql
直接输入mysql

依次执行下面的语句

1
2
3
4
5
6
7
8
9
use mysql ;

set global read_only=0; --关掉新主库的只读属性
flush privileges ;

set password for 'root'@'localhost'=password('root@2017!');

set global read_only=1; --(读写属相)
flush privileges;

1.还原配置文件
2.重启mysql服务

查看密码策略

1
show variables like '%password%';
  • validate_password_policy:密码策略,默认为MEDIUM策略
  • validate_password_dictionary_file:密码策略文件,策略为STRONG才需要
  • validate_password_length:密码最少长度
  • validate_password_mixed_case_count:大小写字符长度,至少1个
  • validate_password_number_count :数字至少1个
  • validate_password_special_char_count:特殊字符至少1个
    上述参数是默认策略MEDIUM的密码检查规则。

共有以下几种密码策略:
策略 检查规则

修改密码策略

/etc/my.cnf文件添加validate_password_policy配置,指定密码策略

选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件

1
validate_password_policy=0

如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:

1
validate_password = off

重新启动mysql服务使配置生效:

1
systemctl restart mysqld

添加远程登录用户

默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:

1
GRANT ALL PRIVILEGES ON *.* TO 'yangxin'@'%' IDENTIFIED BY 'Yangxin0917!' WITH GRANT OPTION;

配置默认编码为utf8

修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:

1
2
3
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'

默认配置文件路径

  • 配置文件:/etc/my.cnf
  • 日志文件:/var/log//var/log/mysqld.log
  • 服务启动脚本:/usr/lib/systemd/system/mysqld.service
  • socket文件:/var/run/mysqld/mysqld.pid

查看my.cnf位置

查看mysqld命令位置

1
which mysqld

查看默认的配置文件加载顺序

1
/usr/sbin/mysqld --verbose --help | grep -A 1 'Default options'

显示结果:

1
2
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

查看mysql默认读取my.cnf目录

1
mysql --help | grep 'my.cnf'

结果:

1
2
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

查看mysql全局变量

未登录mysql时候,shell命令:

1
mysqladmin variables -p

登录mysql的时候,mysql命令:

1
show global variables;

mysql备份

备份命令
参照:http://www.cnblogs.com/xcxc/archive/2013/01/30/2882840.html

1
2
mysqldump -h127.0.0.1 -uroot -proot data11 > backupfile.sql
// ip地址 账号root 密码root 数据库data111 生成的sql文件:backupfile.sql

备份成功,但是出现如下警告,原因是命令行写明文密码不安全。

1
mysqldump: [Warning] Using a password on the command line interface can be insecure.

解决办法:

1
vi /etc/mysql/my.cnf

增加mysqldump的用户和密码,则无需在命令行指定

1
2
3
[mysqldump]
user=dumper
password=dumper@2017!

创建专门的备份用户(可选)

1
2
3
4
5
create user dumper@'127.0.0.1' identified by 'dumper@2017!';
grant select on tempdb.* to dumper@'127.0.0.1';
grant show view on tempdb.* to dumper@'127.0.0.1';
grant lock tables on tempdb.* to dumper@'127.0.0.1';
grant trigger on tempdb.* to dumper@'127.0.0.1';

mysql备份文件还原

1
2
mysql -uroot -proot data11 < backupfile.sql
// 账号root 密码root 数据库data111 还原的sql文件:backupfile.sql

修改默认3306端口

  • vi /etc/my.cnf
  • 配置如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [client]
    port=3417
    [mysqld]
    port=3417
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    symbolic-links=0
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    validate_password=off
    bind-address=127.0.0.1
  • 重启mysql
    systemctl restart mysqld
  • 重启失败
    关闭SELinux:setenforce 0 ,再次重启。

实用sql语句

1
2
/***生成2-20之间随机整数**/
select FLOOR(2 + (RAND() * 20));

http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar

https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar