CentOS7使用firewalld打开关闭防火墙与端口

1、firewalld的基本使用
启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld
开机禁用 : systemctl disable firewalld
开机启用 : systemctl enable firewalld

2、systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。
启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl –failed

3、配置firewalld-cmd

查看版本: firewall-cmd –version
查看帮助: firewall-cmd –help
显示状态: firewall-cmd –state
查看所有打开的端口: firewall-cmd –zone=public –list-ports
更新防火墙规则: firewall-cmd –reload
查看区域信息: firewall-cmd –get-active-zones
查看指定接口所属区域: firewall-cmd –get-zone-of-interface=eth0
拒绝所有包:firewall-cmd –panic-on
取消拒绝状态: firewall-cmd –panic-off
查看是否拒绝: firewall-cmd –query-panic

那怎么开启一个端口呢
添加
firewall-cmd –zone=public –add-port=80/tcp –permanent (–permanent永久生效,没有此参数重启后失效)
重新载入
firewall-cmd –reload
查看
firewall-cmd –zone= public –query-port=80/tcp
删除
firewall-cmd –zone= public –remove-port=80/tcp –permanent

关闭防火墙但是端口依然无法访问,解决步骤

  • 先开启firewalld:systemctl start firewalld
  • 放通端口:firewall-cmd –zone=public –add-port=8080/tcp –permanent
  • 重新加载配置文件:firewall-cmd –reload

How to setup automatic MySQL backup on Ubuntu/Debian and CentOS

Overview

This tutorial is to guide you on setting up a simple, automatic MySQL database backups using mysqldump and a cron job.

Prerequisites

  1. Installed and running MySQL server
  2. SSH access
  3. root or sudo user

Step 1: Backing up a MySQL database

You can use the ‘mysqldump’ that comes in any standard MySQL installation to do a database dump from the shell. To backup the ‘exampledb’ database to a file do:

mysqldump -uroot -p exampledb > exampledb.sql

Note that there is no space between the ‘-u’ switch and the user (root). With the ‘-p’ switch and paramater after it the ‘mysqldump’ will prompt you to enter a password.

This command will output plain SQL queries in the exampledb.sql, but as they are text they can be compressed really good to save space. So to make a compressed backup, let’s do:

mysqldump -uroot -p exampledb | gzip > exampledb.sql.gz

You can use these commands to make manual backups of your databases. If you want to backup all databases on a server, use the ‘–all-databases’ switch instead of database name, like this:

mysqldump -uroot -p –all-databases| gzip > mysql_server.sql.gz

Step 2: Setting an automated scheduled backup of a MySQL database

Now that you know how to make manual database backups, let’s automate them with a cron job. You must set up a cron job that calls the ‘mysqldump’ tool on a schedule.

First let’s make a directory for storing the backups:

mkdir /var/backups/mysql

As backups are more usefull when they provide history, let’s give the backup file with more meaningful name by adding the date and time when the backup was taken:

mysqldump -uroot -p exampledb | gzip > /var/backups/mysql/exampledb.$(date +”%Y_%m_%d_%I_%M”).sql.gz

To automate the backup process ‘mysqldump’ must have the user password inline, so it’s highly recommended to set up an additional user specifically for backups.

Open a crontab editor:

crontab -e

and add the following line at the bottom:

0 1 * * *    mysqldump -uroot -p exampledb | gzip > /var/backups/mysql/exampledb.$(date +”\%Y_\%m_\%d_\%I_\%M”).sql.gz

The “0 1 * * *” stands for a schedule on every 01:00 or everyday at one after midnight. The cron line format is “m h dom mon dow command”, where:

  1. m – is for minutes
  2. h – is for hour
  3. dom – is the day of the month
  4. mon – is the month
  5. dow – day of the week

The ‘*’ symbol in any of these means “on every” minute, hour, day and so.

Saving the crontab will set your backup on schedule and you are good to go.

使用PHP-redis操作Redis

PHP-redis扩展提供了丰富的操作Redis的API接口,使用PHP-redis可以轻松操作Redis。本文整理常用的基础的Redis操作方法,作为笔记方便大家查阅。

PHP连接Redis

我们一般使用connect连接Redis,connect方法为短连接,提供了主机、端口、连接超时等参数设置,如:

$redis->connect('127.0.0.1', 6379);
$redis->connect('127.0.0.1'); // 默认端口6379,你可以设置为其他端口
$redis->connect('127.0.0.1', 6379, 2.5); // 2.5秒也连接不上则为超时断开

也可以使用pconnect或popen方法支持长连接,使用同上。
示例代码:

<?php
//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo "Connection to server sucessfully";
//查看服务是否运行
echo "Server is running: " . $redis->ping();

此外,phpredis还提供了验证密码,选择库和关闭连接释放资源、过期时间(用于缓存)、移动库等方法。

$redis->auth('password');  //登录验证密码,返回【true | false】
$redis->select(0);  //选择redis库, 0~15 共16个库
$redis->close();  //释放资源
$redis->expire('key', 10);  //设置失效时间[true | false]
$redis->move('key', 15);  //把当前库中的key移动到15库中[0|1]

String字符串

Redis 字符串数据类型的相关命令用于管理 redis 字符串值。可以设置、获取、删除键key的值,可以对键值自增和自减等。

$redis->set('key', 1);  //设置key=aa value=1 [true]
$redis->mset($arr);  //设置一个或多个键值[true]
$redis->setnx('key', 'value');  //key=value, key存在返回false[|true]
$redis->get('key');  //获取key [value]
$redis->mget($arr);  //(string|arr), 返回所查询键的值
$redis->del($key_arr);  //(string|arr)删除key,支持数组批量删除【返回删除个数】
$redis->delete($key_str, $key2, $key3);  //删除keys, [del_num]
$redis->getset('old_key', 'new_value');  //先获得key的值,然后重新赋值, [old_value | false]

$redis->strlen('key');  //获取当前key的长度
$redis->append('key', 'string');  //把string追加到key现有的value中[追加后的个数]
$redis->incr('key');  //自增1,如不存在key, 赋值为1(只对整数有效, 存储以10进制64位,redis中为str)[new_num | false]
$redis->incrby('key', $num);  //自增$num, 不存在为赋值, 值需为整数[new_num | false]
$redis->decr('key');  //自减1,[new_num | false]
$redis->decrby('key', $num);  //自减$num,[ new_num | false]
$redis->setex('key', 10, 'value');  //key=value,有效期为10秒[true]

List列表

Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232-1 个元素 (4294967295, 每个列表超过40亿个元素)。

$redis->llen('key');  //返回列表key的长度, 不存在key返回0, [ len | 0]
$redis->lpush('key', 'value');  //增,只能将一个值value插入到列表key的表头(左侧),不存在就创建 [列表的长度 |false]
$redis->rpush('key', 'value');  //增,只能将一个值value插入到列表key的表尾(右侧) [列表的长度 |false]
$redis->lInsert('key',  Redis::AFTER,  'value',  'new_value');  //增,将值value插入到列表key当中,位于值value之前或之后。[new_len | false]
$redis->lpushx('key', 'value');  //增,只能将一个值value插入到列表key的表头,不存在不创建 [列表的长度 |false]
$redis->rpushx('key', 'value');  //增,只能将一个值value插入到列表key的表尾,不存在不创建 [列表的长度 |false]
$redis->lpop('key');  //删,移除并返回列表key的头元素, [被删元素 | false]
$redis->rpop('key');  //删,移除并返回列表key的尾元素, [被删元素 | false]
$redis->lrem('key', 'value', 0);  //删,根据参数count的值,移除列表中与参数value相等的元素count=(0|-n表头向尾|+n表尾向头移除n个value)  [被移除的数量 | 0]
$redis->ltrim('key', start, end);  //删,列表修剪,保留(start, end)之间的值 [true|false]
$redis->lset('key', index, 'new_v');  //改,从表头数,将列表key下标为第index的元素的值为new_v,  [true | false]
$redis->lindex('key', index);  //查,返回列表key中,下标为index的元素[value|false]
$redis->lrange('key', 0, -1);  //查,(start, stop|0, -1)返回列表key中指定区间内的元素,区间以偏移量start和stop指定。[array|false]

Hash字典

Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。Redis 中每个 hash 可以存储40多亿键值对。

$redis->hset('key', 'field', 'value');  //增,改,将哈希表key中的域field的值设为value, 不存在创建, 存在就覆盖【1 | 0】
$redis->hget('key', 'field');  //查,取值【value|false】
$arr = array('one'=>1, 2, 3);$arr2 = array('one', 0, 1);
$redis->hmset('key', $arr);  //增,改,设置多值$arr为(索引|关联)数组, $arr[key]=field,  [ true ]
$redis->hmget('key', $arr2);  //查,获取指定下标的field,[$arr | false]
$redis->hgetall('key');  //查,返回哈希表key中的所有域和值。[当key不存在时,返回一个空表]
$redis->hkeys('key');  //查,返回哈希表key中的所有域。[当key不存在时,返回一个空表]
$redis->hvals('key');  //查,返回哈希表key中的所有值。[当key不存在时,返回一个空表]
$redis->hdel('key', $arr2);  //删,删除指定下标的field, 不存在的域将被忽略, [num | false]
$redis->hexists('key', 'field');  //查看hash中是否存在field, [1 | 0]
$redis->hincrby('key', 'field', $int_num);  //为哈希表key中的域field的值加上量(+|-)num, [new_num | false]
$redis->hlen('key');  //返回哈希表key中域的数量。[ num | 0]

Set集合

Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。 Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。

$redis->sadd('key', 'value1', 'value2', 'valuen');  //增,改,将一个或多个member元素加入到集合key当中,已经存在于集合的member元素将被忽略。[insert_num]
$redis->srem('key', 'value1', 'value2', 'valuen');  //删,移除集合key中的一个或多个member元素,不存在的member元素会被忽略 [del_num | false]
$redis->smembers('key');  //查,返回集合key中的所有成员 [array | '']
$redis->sismember('key', 'member');  //判断member元素是否是集合key的成员 [1 | 0]
$redis->spop('key');  //删,移除并返回集合中的一个随机元素 [member | false]
$redis->srandmember('key');  //查,返回集合中的一个随机元素 [member | false]
$redis->sinter('key1', 'key2', 'keyn');  //查,返回所有给定集合的交集 [array | false]
$redis->sunion('key1', 'key2', 'keyn');  //查,返回所有给定集合的并集 [array | false]
$redis->sdiff('key1', 'key2', 'keyn');  //查,返回所有给定集合的差集 [array | false]

Sorted Set有序集合

Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。 不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。 有序集合的成员是唯一的,但分数(score)却可以重复。

$redis->zAdd('key', $score1, $member1, $scoreN, $memberN);  //增,改,将一个或多个member元素及其score值加入到有序集key当中。[num | 0]
$redis->zrem('key', 'member1', 'membern');  //删,移除有序集key中的一个或多个成员,不存在的成员将被忽略。[del_num | 0]
$redis->zscore('key', 'member');  //查, 通过值反拿权 [num | null]
$redis->zrange('key', $start, $stop);  //查,通过(score从小到大)【排序名次范围】拿member值,返回有序集key中,【指定区间内】的成员 [array | null]
$redis->zrevrange('key', $start, $stop);  //查,通过(score从大到小)【排序名次范围】拿member值,返回有序集key中,【指定区间内】的成员 [array | null]
$redis->zrangebyscore('key', $min, $max[, $config]);  //查,通过scroe权范围拿member值,返回有序集key中,指定区间内的(从小到大排)成员[array | null]
$redis->zrevrangebyscore('key', $max, $min[, $config]);  //查,通过scroe权范围拿member值,返回有序集key中,指定区间内的(从大到小排)成员[array | null]
$redis->zrank('key', 'member');  //查,通过member值查(score从小到大)排名结果中的【member排序名次】[order | null]
$redis->zrevrank('key', 'member');  //查,通过member值查(score从大到小)排名结果中的【member排序名次】[order | null]
$redis->ZINTERSTORE();  //交集
$redis->ZUNIONSTORE();  //差集

Server服务

Phpredis提供服务级别的操作方法,轻松管理redis。

$redis->dbSize();  //返回当前库中的key的个数
$redis->flushAll();  //清空整个redis[总true]
$redis->flushDB();  //清空当前redis库[总true]
$redis->save();  //同步??把数据存储到磁盘-dump.rdb[true]
$redis->bgsave();  //异步??把数据存储到磁盘-dump.rdb[true]
$redis->info();  //查询当前redis的状态 [verson:2.4.5....]
$redis->lastSave();  //上次存储时间key的时间[timestamp]

$redis->watch('key', 'keyn');  //监视一个(或多个) key ,如果在事务执行之前这个(或这些) key 被其他命令所改动,那么事务将被打断 [true]
$redis->unwatch('key', 'keyn');  //取消监视一个(或多个) key [true]
$redis->multi(Redis::MULTI);  //开启事务,事务块内的多条命令会按照先后顺序被放进一个队列当中,最后由 EXEC 命令在一个原子时间内执行。
$redis->multi(Redis::PIPELINE);  //开启管道,事务块内的多条命令会按照先后顺序被放进一个队列当中,最后由 EXEC 命令在一个原子时间内执行。
$redis->exec();  //执行所有事务块内的命令,;【事务块内所有命令的返回值,按命令执行的先后顺序排列,当操作被打断时,返回空值 false】

Linux Mint + Xampp Error + ‘grep: /opt/lampp/include/php/main/php.h: No Such File Or Directory’

So you wanted to install an extension on Xampp.. you execute phpize by running something like: /opt/lampp/bin/phpize.. and boooom! You start getting some xampp error messages as shown below:

grep: /opt/lampp/include/php/main/php.h: No such file or directory
grep: /opt/lampp/include/php/Zend/zend_modules.h: No such file or directory
grep: /opt/lampp/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

Solution To This Grep Error
You are getting this phpize grep error simply because you have installed the default common LAMP package for XAMPP, that is the normal final user version. This version is not equipped, with the source files and libraries, to build, compile and install extensions.
So, how will you do that?
ANSWER => Install the Xampp Devel package, aka the Xampp Development package.

Where To Get The Xampp Development package
You can download the Xampp Development package (Xampp Devel) on the same download page as the normal one, here! Just look for ‘Development package‘ and click to download! Bingo you have it!
How To Install Xampp Devel / Xampp Development Package?
Simple!
I assume you have by now already downloaded your package, mine at the time of this writing, is named as xampp-linux-devel-1.7.4.tar.gz.

To install, simply execute the following shell command:

sudo tar xvfz xampp-linux-devel-1.7.4.tar.gz -C /opt

– NOTE: This extraction will extract and place those needed development libraries and source file inside your existing Lampp folder in /opt/lampp/

Voila!
(happy Mint-Xampp-ing)

Mac下使用国内镜像安装Homebrew

国内的镜像

新增brew.git与homebrew-core.git镜像
由于官方弃用了旧的homebrew仓库,将homebrew程序与软件包拆分成了两个仓库。为保证用户正常升级,旧镜像将暂时保留一段时间,择期删除。
仓库对应关系:
github.com/Homebrew/brew -> mirrors.ustc.edu.cn/brew.git
github.com/Homebrew/homebrew-core -> mirrors.ustc.edu.cn/homebrew-core.git
github.com/Homebrew/homebrew(弃用) -> mirrors.ustc.edu.cn/homebrew.git

安装

获取install文件并编辑

cd ~
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install >> brew_install

编辑brew_install文件

#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
# This script installs to /usr/local only. To install elsewhere you can just
# untar https://github.com/Homebrew/brew/tarball/master anywhere you like or
# change the value of HOMEBREW_PREFIX.
HOMEBREW_PREFIX = “/usr/local”.freeze
HOMEBREW_REPOSITORY = “/usr/local/Homebrew”.freeze
HOMEBREW_CACHE = “#{ENV[“HOME”]}/Library/Caches/Homebrew”.freeze
HOMEBREW_OLD_CACHE = “/Library/Caches/Homebrew”.freeze
#BREW_REPO = “https://github.com/Homebrew/brew”.freeze
BREW_REPO = “git://mirrors.ustc.edu.cn/brew.git”.freeze
#CORE_TAP_REPO = “https://github.com/Homebrew/homebrew-core”.freeze
CORE_TAP_REPO = “git://mirrors.ustc.edu.cn/homebrew-core.git”.freeze

注释掉BREW_REPO = “https://github.com/Homebrew/brew”.freeze和CORE_TAP_REPO = “https://github.com/Homebrew/homebrew-core”.freeze
修改为BREW_REPO = “git://mirrors.ustc.edu.cn/brew.git”.freeze和CORE_TAP_REPO = “git://mirrors.ustc.edu.cn/homebrew-core.git”.freeze

安装

/usr/bin/ruby ~/brew_install 

运行修改了的brew_install文件。

替换homebrew源

替换homebrew默认源

cd "$(brew --repo)"
git remote set-url origin git://mirrors.ustc.edu.cn/brew.git

替换homebrew-core源

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin git://mirrors.ustc.edu.cn/homebrew-core.git

brew更新

brew update

设置 bintray镜像

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile