Centos7安装mariadb

1、安装MariaDB

安装命令

yum -y install mariadb mariadb-server

安装完成MariaDB,首先启动MariaDB

systemctl start mariadb

设置开机启动

systemctl enable mariadb

接下来进行MariaDB的相关简单配置

mysql_secure_installation

首先是设置密码,会提示先输入密码

Enter current password for root (enter for none):<–初次运行直接回车

设置密码

Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码

其他配置

Remove anonymous users? [Y/n] <– 是否删除匿名用户,回车

Disallow root login remotely? [Y/n] <–是否禁止root远程登录,回车,

Remove test database and access to it? [Y/n] <– 是否删除test数据库,回车

Reload privilege tables now? [Y/n] <– 是否重新加载权限表,回车

初始化MariaDB完成,接下来测试登录

mysql -uroot -ppassword

完成。

2、配置MariaDB的字符集

文件/etc/my.cnf

vi /etc/my.cnf

在[mysqld]标签下添加

init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 
collation-server=utf8_unicode_ci 
skip-character-set-client-handshake

文件/etc/my.cnf.d/client.cnf

vi /etc/my.cnf.d/client.cnf

在[client]中添加

default-character-set=utf8

文件/etc/my.cnf.d/mysql-clients.cnf

vi /etc/my.cnf.d/mysql-clients.cnf

在[mysql]中添加

default-character-set=utf8

全部配置完成,重启mariadb

systemctl restart mariadb

之后进入MariaDB查看字符集

mysql> show variables like "%character%";show variables like "%collation%";

显示为

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client    | utf8                      |
| character_set_connection | utf8                      |
| character_set_database  | utf8                      |
| character_set_filesystem | binary                    |
| character_set_results    | utf8                      |
| character_set_server    | utf8                      |
| character_set_system    | utf8                      |
| character_sets_dir      | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-----------------+
| Variable_name        | Value          |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database  | utf8_unicode_ci |
| collation_server    | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

字符集配置完成。

3、添加用户,设置权限

创建用户命令

mysql>create user username@localhost identified by 'password'; 

直接创建用户并授权的命令

mysql>grant all on *.* to username@localhost indentified by 'password';

授予外网登陆权限

mysql>grant all privileges on *.* to username@'%' identified by 'password';

授予权限并且可以授权

mysql>grant all privileges on *.* to username@'hostname' identified by 'password' with grant option;

简单的用户和权限配置基本就这样了。

其中只授予部分权限把 其中 all privileges或者all改为select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file其中一部分。

CentOS下lvm在线扩容

最近一个case,客户由于业务增长迅速,原先在存储上划分给linux服务器的volume,可用空间不足,因此需要在存储上新增一个卷给主机,扩容空间,满足业务需求。

因为业务需要,不能重启服务器中断业务,因此通过LVM动态扩容linux服务器上的逻辑卷空间大小。

lvm扩容方法如下:

1、通过pvs,vgs,lvs 等确认要扩容的VG,LV的名称、大小等信息,以免弄错。

2、确认服务器新扫描到的volume信息,找到新设备/dev/sdj

#fdisk -l | more 

3、fdisk 给新设备分区,选择分区类型为8e,lvm格式,w保存分区。

#fdisk -l /dev/sdj1

4、创建PV

#pvcreate /dev/sdj1

#pvs

5、将新建的PV加入到VG,扩容VG

#vgextend  datavg /dev/sdj1

#vgs

6、扩容LV前,先确认LV信息,VG中可用来扩容LV的空间大小

lvdisplay

#vgs

#vgdisplay

7、扩容LV

#lvextend -L +950G /dev/datevg/lv010

#lvs

#lvdisplay

8、刷新文件系统的大小

#resize2fs  /dev/datevg/lv001

#df -h

这样,整个lvm扩容完成。

如何在Centos 7上用Logrotate管理日志文件

何为Logrotate?

Logrotate是一个实用的日志管理工具,旨在简化对系统上生成大量的日志文件进行管理。 Logrotate允许自动旋转压缩,删除和邮寄日志文件,从而节省宝贵的磁盘空间。 Logrotate可以设置为每天、每周、每月或当日志文件达到一定的大小时处理日志文件。还可以完全控制日志的自动化管理方式,而不需要人工干预。Logrotate支持Linux系统上的所有日志文件,包括但不限于Apache、Nginx、Tomcat、ELK、zabbix等应用。

1. 安装logrotate

系统默认已经安装,某些Linux发行版可能并未安装,那么请执行以下命令安装:

Centos:

$ yum install logrotate -y

Ubuntu:

$ apt-get install logrotate -y

通过yum默认安装的文件存储位置说明

/etc/cron.daily            # logrotate脚本文件存放位置
/etc/logrotate.conf        # 主配置文件
/usr/sbin/logrotate        # 二进制程序文件
/etc/logrotate.d/          # 特定服务日志存储目录(自己设定的配置文件)
/var/lib/logrotate.status  # 状态文件

2. 查看logrotate主文件默认配置情况

$ cat /etc/logrotate.conf

# see "man logrotate" for details
# rotate log files weekly
weekly                  # 每周转存一次

# keep 4 weeks worth of backlogs
rotate 4                    # 保留四个日志备份文件

# create new (empty) log files after rotating old ones
create                      # rotate后,创建一个新的空文件

# use date as a suffix of the rotated file
dateext                     # 轮转的文件名字带有日期信息

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d    # 此目录下的配置文件优先生效

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {             # 指定/var/log/wtmp日志文件;
    monthly                 # 每月轮转一次,优先于全局设定的每周轮转一次;
    minsize 1M              # 日志文件大于1M才会去轮转;
    create 0664 root utmp   # 新日志文件的权限,属主,属组;
    rotate 1                # 保留一个日志备份,优先于全局设置的四个;
}
/var/log/btmp {             # 指定/var/log/btmp日志文件;
    missingok               # 如果日志丢失,不报错;
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.
# 系统特定的日志也可以在主文件中配置。

3. logrotate日志切割

在定义日志文件时可以使用通配符,但不建议使用,因为它会包括已切换过的日志,如必须使用,请在号后加上已知的文件扩展名, 例如:.log。

Nginx日志:

在/etc/logrotate.d/目录下创建Nginx服务日志配置文件

$ vim /etc/logrotate.d/nginx

/usr/local/nginx/logs/*.log {
create 644 www root
daily
dateext
rotate 3
minsize 10K
copytruncate
nocompress
missingok
notifempty
noolddir
postrotate
/bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
endscript
}

php日志:

/usr/local/php/var/log/*.log {
missingok
notifempty
sharedscripts
delaycompress
create 0664 www www
postrotate
/bin/kill -SIGUSR1 `cat /usr/local/php/var/run/php-fpm.pid 2>/dev/null` 2>/dev/null || true
endscript
}

注:你也可以手动生成一个20M内容的日志文件进行测试,例如:

$ head -c 20M < /dev/urandom > /var/log/renwole-log

配置完成后,可以通过如下命令来手动执行查看效果:

$ logrotate -f /etc/logrotate.d/nginx

另外还可以使用如下命令查看logrotate运行状态:

$ cat /var/lib/logrotate/logrotate.status

由于Logrotate是基于cron定时运行的,所以logrotate脚本默认在 /etc/cron.daily/ 目录下,文件名是logrotate。你可以设置 /etc/anacrontab 来控制Logrotate何时运行。

4. 查看anacrontab默认配置

$ cat /etc/anacrontab

# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

5. 关于logrotate常用配置参数注解

daily,weekly,monthly  # 转储周期分别是每天/每周/每月;
minsize 15M           # 日志体积大于此值时轮换(例如:100K,4M);
dateext               # 轮询的文件名字带有日期信息;
missingok             # 如果日志文件丢失,不要显示错误;
rotate 5              # 轮转存储中包含多少备份日志文件,0为无备份,以数字为准;
compress              # 通过gzip压缩转储以后的日志,以*.gz结尾;
nocompress            # 不需要压缩时,用这个参数;
delaycompress         # 延迟压缩,和compress一起使用时压缩所有日志,除当前和下一个最近的;
nodelaycompress       # 覆盖delaycompress选项,转储同时压缩;
copytruncate          # 用于还在打开中的日志文件,把当前日志备份并截断;
nocopytruncate        # 备份日志文件但是不截断;
create 644 www root   # 转储文件,使用指定的文件模式创建新的日志文件;
nocreate              # 不建立新的日志文件;
errors [email protected] # 专储时的错误信息发送到指定的Email地址;
ifempty               # 即使是空文件也转储,这个是logrotate的缺省选项;
notifempty            # 如果日志文件为空,则不转储;
mail [email protected]   # 把转储的日志文件发送到指定的E-mail地;
nomail                # 转储时不发送日志文件;
olddir /tmp           # 转储后的日志文件放入指定目录,必须和当前日志文件在同一个文件系统;
noolddir              # 转储后的日志文件和当前日志文件放在同一个目录下;
prerotate/endscript   # 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行;
postrotate/endscript  # 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行;
tabooext              # 不转储指定扩展名的文件,缺省扩展名:cfsaved,.disabled,.dpkg-dist等;
sharedscripts         # 共享脚本,让postrotate/endscript包含脚本只执行一次即可;
dateformat            # 配合dateext使用可以为切割后的日志加上YYYYMMDD格式的日期;

以上参数都可以在全局主配置文件中定义,或指定为某个日志文件进行配置,注意:使用时参数之间不要冲突。

Centos7 Cacti自动安装脚本

Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具。界面友好,它提供了非常强大的数据和用户管理功能,可以指定每一个用户能查看树状结构、host以及任何一张图,还可以与LDAP结合进行用户验证,同时也能自己增加模板,功能非常强大完善。

—–BY:百度百科。

未分类

Cacti官方网站是:https://www.cacti.net/,官网提供多个版本的下载,不过最新版本都没有的最新插件的支持,而有些插件对我来说在运维过程中是必须的,所以我还是喜欢1.0之前的版本,所以制作此脚本使用的Cacti版本是0.8.8a。

关于脚本的一些说明:

全自动安装cacti0.8.8a

  1. 此脚本用于centos7系列全自动安装cacti0.8.8a,centos6系列系统不适用。

  2. 使用此脚本系统需全新系统,由于个人能力有限,脚本未检测您服务器是否已经安装apache及mysql、mariadb等,如果已经安装,此脚本安装完可能会有问题,cacti或许不能运行。

  3. 脚本会自动安装mariadb数据库并设置密码为Admin888。新建cacti用户名以及同名库,默认密码为Botonet123,如果您是正式场景使用,请修改数据库密码以及cacti的数据库配置文件。

  4. 初始登陆用户名和密码为admin

脚本使用/安装方法:

方法1.复制以下脚本在服务器上保存为install.sh,给于执行权限后执行脚本即可。

#!/bin/bash
[ ! -e "/usr/bin/git" ] && yum install -y git
git clone https://github.com/qiguang0920/cacti0.8.8a.git && cd cacti0.8.8a && chmod +x cacti-centos7.sh && ./cacti-centos7.sh

  
方法2.脚本以及所需文件托管在github.com,需要的网友可以直接从github.com打包下载,解压后运行目录下的cacti-centos7.sh。

如何在Centos上使用PageSpeed给Apache加速

前言

这个PageSpeed是Google官方给的一款适用于Web服务器的加速模块,

具体有没有效果,我不敢保证,反正我自己测试的时候,是看不出来的(逃

本文测试环境:Centos 7 , Oneinstack Apache+Tengine

安装过程

1、安装Google PageSpeed

安装之前:

Google PageSpeed会自动更新的,如果不想要自动更新,请先执行以下这句命令(不推荐自动更新):

touch /etc/default/mod-pagespeed

开始安装:

64位Centos/Fedora:

rpm -ivh https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm --force --nodeps

32位Centos/Fedora:

rpm -ivh https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_i386.rpm --force --nodeps

据测试,国内服务器也能直接下载(这个域名和服务器并没有被蔷)

2、复制安装的配置文件

由于Oneinstack安装的apache并不在默认目录,所以按照官方的操作是要没用的

首先得将默认的配置文件复制到apache配置文件目录下(以下为Oneinstack的示范):

cp /etc/httpd/conf.d/page* /usr/local/apache/conf/extra/

3、添加PageSpeed配置文件到Apache主配置文件中

编辑httpd.conf文件,并在最后添加:

Include conf/extra/pagespeed.conf
Include conf/extra/pagespeed_libraries.conf

或直接运行下面两行命令:

echo "Include conf/extra/pagespeed.conf" >> /usr/local/apache/conf/httpd.conf
echo "Include conf/extra/pagespeed_libraries.conf" >> /usr/local/apache/conf/httpd.conf

4、重启Apache服务器查看效果

service httpd restart
No related posts.

[CentOS] 解决 crontab 无法读取环境变量的问题

1. 问题描述

一段数据处理的 shell 程序,在 shell 中手动运行,可以正确执行。但是,把它放在 crontab 列表里,就会报错,提示 “matlab: command not found.”。

AutoRefreshData.sh 的部分内容如下:

[She@She ~]$ cat /home/She/data/AutoRefreshData.sh
#!/bin/bash
...
MatlabFile='/mnt/L/Data/main4mat.m'
chmod +x ${MatlabFile}
matlab  -nodesktop -nosplash -nojvm < ${MatlabFile} 1>running.log 2>running.err &

在终端下,AutoRefreshData.sh 可正确执行:

[She@She ~]$ /home/She/data/AutoRefreshData.sh
[She@She ~]$ cat ~/running.log

                            < M A T L A B (R) >
                  Copyright 1984-2015 The MathWorks, Inc.
                   R2015b (8.6.0.267246) 64-bit (glnxa64)
                              August 20, 2015


For online documentation, see http://www.mathworks.com/support
For product information, visit www.mathworks.com.

>> >> >> >> >> >> >> /mnt/L/Data/matFile/jpl16228.mat
>> 
[She@She ~]$ cat ~/running.err
[She@She ~]$  

将该 shell 脚本添加到 crontab 中:

[She@She ~]$ crontab -l
# part 2: refresh She data from FTP
08 12 *  *  * /home/She/data/AutoRefreshData.sh                             > /dev/null 2>&1

在 crontab 中,运行报错,结果如下:

[She@She ~]$ cat ~/running.log
[She@She ~]$ cat ~/running.err
/home/She/data/AutoRefreshData.sh: line 111: matlab: command not found 

2. Bug 原因分析与修复

原因分析:crontab 有一个坏毛病, 就是它总是不会缺省的从用户 profile 文件中读取环境变量参数,经常导致在手工执行某个脚本时是成功的,但是到 crontab 中试图让它定期执行时就是会出错。

修复:在脚本文件的开头,强制要求导入环境变量,可保万无一失。

这样的话,脚本的头部一律以下列格式开头:

#!/bin/sh
. /etc/profile
. ~/.bash_profile

以 AutoRefreshData.sh 为例,它的头部则由

[She@She ~]$ cat /home/She/data/AutoRefreshData.sh
#!/bin/bash
...
MatlabFile='/mnt/L/Data/main4mat.m'
chmod +x ${MatlabFile}
matlab  -nodesktop -nosplash -nojvm < ${MatlabFile} 1>running.log 2>running.err &

改为:

[She@She ~]$ vi /home/She/data/AutoRefreshData.sh
#!/bin/sh
. /etc/profile
. ~/.bash_profile
...
MatlabFile='/mnt/L/Data/main4mat.m'
chmod +x ${MatlabFile}
matlab  -nodesktop -nosplash -nojvm < ${MatlabFile} 1>running.log 2>running.err &

之后,更新 crontab 中的运行时间,立即测试,一切正常,不再报错。

[She@She ~]$ cat ~/running.log

                            < M A T L A B (R) >
                  Copyright 1984-2015 The MathWorks, Inc.
                   R2015b (8.6.0.267246) 64-bit (glnxa64)
                              August 20, 2015


For online documentation, see http://www.mathworks.com/support
For product information, visit www.mathworks.com.

>> >> >> >> >> >> >> /mnt/L/Data/matFile/jpl16228.mat
>> 
[She@She ~]$ cat ~/running.err
[She@She ~]$ 

Done。

CentOS 7上安装Docker

脚本安装

1、使用root权限登陆系统。

2、更新系统包到最新。

yum -y update

3、执行Docker安装脚本

curl -sSL https://get.docker.com/ | sh
yum install -y docker-selinux

4、启动 Docker

systemctl start docker.service

5、验证 docker 已经正常安装

docker run hello-world

yum 安装

1、添加 yum 仓库

cat >/etc/yum.repos.d/docker.repo <<-EOF
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

2、安装 Docker 包

yum install -y docker-engine

开机自启动

systemctl enable docker.service

docker里的centos 安装sshd服务

1、 yum install openssh-server

2、启动sshd 报错 需要绝对路径

[root@0463226081ca src]# sshd     
sshd re-exec requires execution with an absolute path

3、用绝对路径 再次报错

[root@0463226081ca src]# /usr/sbin/sshd     
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.

4、执行 少什么生成什么key

ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

5、再次执行 成功

[root@0463226081ca src]# /usr/sbin/sshd
[root@0463226081ca src]# 

6、外部连接还是失败 修改/etc/sshd/sshd_config配置文件 重启再连 成功

UsePAM no

Centos安装SNIProxy

一、序

最近朋友给我介绍了一个黑科技玩法,需要用到sniproxy,这东西我之前就知道,当初拿来代理谷歌配合自建dns用,好久没用都忘记怎么装了……所以做个记录

二、正文

其实吧,作者的GITHUB(传送门)说的很清楚……然而我还是要水一篇233

yum install autoconf automake curl gettext-devel libev-devel pcre-devel perl pkgconfig rpm-build udns-devel
cd /opt
wget -O sniproxy-0.4.0.tar.gz https://github.com/dlundquist/sniproxy/archive/0.4.0.tar.gz
tar xzf sniproxy-0.4.0.tar.gz 
cd sniproxy-0.4.0
./autogen.sh && ./configure && make dist
rpmbuild --define "_sourcedir `pwd`" -ba redhat/sniproxy.spec
rpm -ivh ~/rpmbuild/RPMS/x86_64/sniproxy-0.4.0-1.el6.x86_64.rpm

这是制作RPM包的方法,属于一劳永逸的(要更新除外),对了,在autogen的过程中会报 debchange: command not found 请无视,这是debian的玩意

另外打包必须要yum安装libev-devel和udns-devel,然而make install安装的话我们可以选择编译版本的libev和udns(版本更新,强迫症福利)

cd /opt
wget http://dist.schmorp.de/libev/Attic/libev-4.22.tar.gz
tar xzf libev-4.22.tar.gz
cd libev-4.22
./configure
make
make install
echo -e '/usr/local/libn/usr/local/lib64' > /etc/ld.so.conf.d/local.conf
ldconfig 
cd /opt
wget http://www.corpit.ru/mjt/udns/udns-0.4.tar.gz
tar xzf udns-0.4.tar.gz
cd udns-0.4
./configure
make
cd ..
mv udns-0.4 /usr/local/udns
echo -e '/usr/local/udns' > /etc/ld.so.conf.d/udns.conf
ldconfig

到这里安装基本没啥问题了,至于配置的话,这东西配置相当简单,我给出一份GITHUB上的Example,相信各位一看就懂了

user daemon

pidfile /tmp/sniproxy.pid

error_log {
    syslog daemon
    priority notice
}

listener 127.0.0.1:443 {
    protocol tls
    table TableName

    # Specify a server to use if the initial client request doesn't contain
    # a hostname
    fallback 192.0.2.5:443
}

table TableName {
    # Match exact request hostnames
    example.com 192.0.2.10:4343
    # If port is not specified the listener port will be used
    example.net [2001:DB8::1:10]
    # Or use regular expression to match
    .*\.com    [2001:DB8::1:11]:443
    # Combining regular expression and wildcard will resolve the hostname
    # client requested and proxy to it
    .*\.edu    *:443
}

Centos下改变整个目录及文件大小写

改变指定文件夹下所有目录为小写

function change_path(){
count=`find $1 -type d|wc -l`
echo "开始更改目录,共有${count}个目录"
for i in $(seq 1 $count)
do
    ii=$(find $1 -type d|sed -n "${i}p")
    if [[ $ii == "./" ]]
    then
        continue
    else
        lower_file=$(echo ${ii}|tr 'A-Z' 'a-z')
        if [[ $ii == $lower_file ]]
        then
            continue
        else
            mv $ii $lower_file
        fi
    fi
done
echo "目录修改完成"
}
change_path

代码测试可以运行,但是如果遇到特殊字符,如 { } 或者目录名中有空格或者中文字符会失败,目前没有想到好的解决方法~~ 如有您有什么好的方法, 可以在下方评论~~~