龚哥哥 爱生活、做自己!
CentOS yum remove卸载软件出错
发表于 2016-8-26 | 浏览(2369) | 服务器

卸载过程中的错误

error: %preun(XXX-1.5.4-3.el6.x86_64) scriptlet failed, exit status 1
Error in PREUN scriptlet in rpm package XXX要卸载的包名

解决方案

yum --setopt=tsflags=noscripts remove XXX要卸载的包名

查看软件包

rpm -ql XXX包名

阅读全文

CentOS使用Keepalived搭建双机热备
发表于 2016-8-23 | 浏览(2725) | 服务器

1、环境

CentOS6.5
keepalived-1.2.23

2、准备两台服务器和一个VIP IP

Master      192.168.0.109
Backup      192.168.0.110
VIP         192.168.0.200

3、安装gcc编译器,openssl,wget,如果已经安装则跳过

yum -y install openssl-devel ncurses-devel gcc gcc-c++ make rpm-build wget

4、目录创建

mkdir /data/soft
mkdir /data/nginx
mkdir /data/www

6、安装keepalived

cd /data/soft
wget http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
tar -zxvf keepalived-1.2.23.tar.gz
cd keepalived-1.2.23
./configure --prefix=/usr/local/keepalived
make
make install

7、将keepalived做成启动脚务

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
chmod +x /etc/init.d/keepalived

8.1、MASTER配置文件,位置 /etc/keepalived/keepalived.conf

global_defs {
    notification_email
    {
        fuxiang.gong@qq.com
    }
    notification_email_from 17091959688@163.com
    smtp_server smtp.163.com
    stmp_connect_timeout 30
    router_id lnmp_node1
}

# 服务
vrrp_instance LNMP {
    state MASTER    #设置为主服务器
    interface eth0  #监测网络接口
    virtual_router_id 51  #主、备必须一样
    priority 100   #主机级别,值越大优先级越高
    advert_int 1   #VRRP Multicast广播周期秒数

    authentication {
        auth_type PASS  #VRRP认证方式,主备必须一致
        auth_pass 1111  #密码
    }
    virtual_ipaddress {
        192.168.0.200  #VRRP HA虚拟地址
    }
}

8.2、BACKUP配置文件

global_defs { 
    notification_email
    {
        fuxiang.gong@qq.com
    }
    notification_email_from 17091959688@163.com
    smtp_server smtp.163.com
    stmp_connect_timeout 30
    router_id lnmp_node2
}

# 服务
vrrp_instance LNMP {
    state BACKUP    #设置为备用服务器
    interface eth0  #监测网络接口
    virtual_router_id 51  #主、备必须一样
    priority 90   #主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高 
    advert_int 1   #VRRP Multicast广播周期秒数
    authentication {
        auth_type PASS  #VRRP认证方式,主备必须一致
        auth_pass 1111  #密码
    }
    virtual_ipaddress {
        192.168.0.200  #VRRP HA虚拟地址
    }
}

8.3、允许两台服务器vrrp包通过防火墙,如果关闭防火墙则跳过(两台服务器上都配置)

MASTER
  vim /etc/sysconfig/iptables
  -A INPUT -i eth0 -p vrrp -s 192.168.0.111 -j ACCEPT

BACKUP
  vim /etc/sysconfig/iptables
  -A INPUT -i eth0 -p vrrp -s 192.168.0.110 -j ACCEPT

重启防火墙
service iptables restart

9、keepalived操作,这里做启动操作

service keepalived start      启动
service keepalived stop       停止
service keepalived restart    重启

10、查看已经成功添加虚拟网卡

主服务器  ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d9:a8:bd brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.109/24 brd 192.168.0.255 scope global eth0
    inet 192.168.0.200/32 scope global eth0
    inet6 fe80::20c:29ff:fed9:a8bd/64 scope link 
       valid_lft forever preferred_lft forever


BACKUP服务器  ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d9:8f:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.110/24 brd 192.168.0.255 scope global eth0
    inet6 fe80::20c:29ff:fed9:8f72/64 scope link 
       valid_lft forever preferred_lft forever

11、安装nginx,详细步骤查看博客中其它nginx安装方法文章

12、把nginx访问指向 /data/www目录,在 /data/www 目录中创建index.html内容分别写 MASTER, BACKUP(通过浏览器访问 192.168.0.200 ),默认MASTER,如果关闭MASTER keepalived服务,BACKUP服务器将自动升级为MASTER提供服务。开启MASTER keepalived服务,自动切换MASTER服务器来服务,BACKUP降级为备用服务。

13、查看日志

tail -f /var/log/messages

阅读全文

Linux下一个网卡配置多个ip(虚拟ip)
发表于 2016-8-23 | 浏览(3169) | 服务器

临时设置,关机失效

在同一个网段的虚拟ip(本机,同网段的电脑可以ping通)以下192.168.0.100只能在本机ping通

ifconfig eth0:1 10.0.82.200 netmask 255.255.255.0
ifconfig eth0:1 192.168.0.100 netmask 255.255.255.0

设置永久ip

1、设置子ip配置文件

cd /etc/sysconfig/network-scripts

2、创建新文件 vim ifcfg-eth0:0,添加以下内容,保存退出

DEVICE=eth0:0  # 虚拟网络接口,随意    
ONBOOT=yes  # 系统启动时激活
BOOTPROTO=static  # 使用静态ip地
IPADDR=10.0.82.200  # 该虚拟网络接口的ip别名,随意
NETMASK=255.255.255.0  # 子网掩码,对应ip别名
GATEWAY=10.0.82.1  # 网关,对应ip别名
HWADDR=00:0c:29:d9:a8:bd  # 网卡MAC地址,与 eth0 相同
USERCTL=yes  # 是否给予非root用户设备管理权限

3、重启网卡

service network restart

4、临时清除ip别名

ifconfig eth0:0 down

阅读全文

VMware完整克隆虚拟机后连不上网络
发表于 2016-8-19 | 浏览(2115) | 服务器

完整克隆之后却不能连接到虚拟机了,产生这个问题的原因是克隆之后,新的克隆副本的网卡信息被修改了,所以需要手动设置下。原先使用的eth0网卡已经被使用了所以不能使用。

1、修改 70-persistent-net.rules 文件(删除旧网卡信息,使用新网卡信息)

vi /etc/udev/rules.d/70-persistent-net.rules

上面就是克隆后的文件信息,如果不是克隆的,红色部分没有。因为克隆了,所以就多出来红色部分的了。因为使用的还是旧网卡信息,所以导致了xshell连接不上。这里我们删除黄色框中东西,并将红色最后的eth1改成eth0。同时记住 ATTR{address}的值。

1.jpg

2、修改 ifcfg-eth0文件(将原来的旧网卡的HWADDR改成新网卡的HWADDR)

vi /etc/sysconfig/network-scripts/ifcfg-eth0

HWADDR的值就是上图中ATTR{addres}的值,然后设置一下想要的IP。reboot重启后就可以使用xshell连接了,注释黄色一行,红色一行为新添加。

2.jpg

3、重启服务器即可

init 6

阅读全文

Elasticsearch集群 搜索引擎
发表于 2016-8-17 | 浏览(2024) | 服务器

1、准备,两个ip在同一个网段下,两台linux安装 Elasticsearch

192.168.8.1
192.168.8.2

2、配置文件位置 config/elasticsearch.yml

A、192.168.8.1配置文件

# 开启script
script.inline: on
script.indexed: on
script.file: on

# 绑定的IP地址,可以是IPV4或者IPV6
network.bind_host: 0.0.0.0

# 其它节点交互ip地址
network.publish_host: 192.168.8.1

# 该参数用于同时设置 bind_host 和 publish_host
network.host: 192.168.8.1

# 节点之间tcp通信端口
#transport.tcp.port: 9300

# 开启tcp传输压缩
#transport.tcp.compress: true

# master节点初始化
discovery.zen.ping.unicast.hosts: ["192.168.8.1:9300","192.168.8.2:9300"]

B、192.168.8.2配置文件

# 开启script
script.inline: on
script.indexed: on
script.file: on

# 绑定的IP地址,可以是IPV4或者IPV6
network.bind_host: 0.0.0.0

# 其它节点交互ip地址
network.publish_host: 192.168.8.2

# 该参数用于同时设置 bind_host 和 publish_host
network.host: 192.168.8.2

# 节点之间tcp通信端口
#transport.tcp.port: 9300

# 开启tcp传输压缩
#transport.tcp.compress: true

# master节点初始化
discovery.zen.ping.unicast.hosts: ["192.168.8.1:9300","192.168.8.2:9300"]

3、启动 ElasticSearch ,进入 bin 目录启动

elasticSearch

4、安装 ElasticSearch Head 插件,进入 bin 目录运行

plugin install mobz/elasticsearch-head

5、通过浏览器访问插件

http://192.168.8.1:9200/_plugin/head
http://192.168.8.2:9200/_plugin/head

阅读全文

Elasticsearch入门 搜索引擎
发表于 2016-8-11 | 浏览(3909) | PHP

Elasticsearch最高效开源的搜索引擎框架,我们下面添加几条员工数据进行演示,详情可以在线文档查看,这里用PHP操作Elasticsearch的一些例子,在使用例子的情况下需要先安装Elasticsearch搜索引擎,安装方法可以查看官网,地址:https://www.elastic.co/downloads/elasticsearch

Elasticsearch依赖java,地址:http://www.java.com/

中文文档地址:http://es.xiaoleilu.com/

公共方法,用于操作Elasticsearch

/**
 * [PostCurl post请求]
 * @param  [string]  $url    [请求地址]
 * @param  [string]  $option [参数]
 * @param  [integer] $header [http头部信息]
 * @param  [string]  $type   [请求类型]
 * @return [array]           [返回的数据]
 */
function PostCurl($url, $option = '', $header = '', $type = 'POST')
{
	if(empty($header)) $header = array();
    $curl = curl_init (); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)'); // 模拟用户使用的浏览器
    if (! empty($option)) {
        if($type == 'PUT')
        {
            $options = json_encode($option);
        } else {
            $options = json_encode($option, JSON_FORCE_OBJECT);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $options); // Post提交的数据包
    }
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // 设置HTTP头
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
    $result = curl_exec($curl); // 执行操作
    curl_close($curl); // 关闭CURL会话
    return json_decode($result, true);
}

定义url地址

$url = 'http://localhost:9200/';

1、添加员工数据

关系数据库     ⇒ 数据库 ⇒ 表     ⇒ 行     ⇒ 列(Columns)

Elasticsearch   ⇒ 索引   ⇒ 类型   ⇒ 文档   ⇒ 字段(Fields)

索引 megacorp

类型 employee

文档 1 ~ 3

字段

// 定义员工数据
$param = array(
	1	=>	array(
			'first_name'	=>	'John',
			'last_name'		=>	'Smith',
			'age'			=>	25,
			'about'			=>	'I love to go rock climbing',
			'interests'		=>	array('sports', 'music')
		),
	2	=>	array(
			'first_name'	=>	'Jane',
			'last_name'		=>	'Smith',
			'age'			=>	32,
			'about'			=>	'I like to collect rock albums',
			'interests'		=>	array('music')
		),
	3	=>	array(
			'first_name'	=>	'Douglas',
			'last_name'		=>	'Fir',
			'age'			=>	35,
			'about'			=>	'I like to build cabinets',
			'interests'		=>	array('forestry')
		),
	);


// 循环将员工数据加入 Elasticsearch 搜索引擎中
$success = 0;
foreach($param as $k=>$v)
{
	$result = PostCurl($url.'megacorp/employee/'.$k, $v, '', 'PUT');
	if(isset($result['_shards']['successful']) && $result['_shards']['successful'] == 1) $success++;
}
echo $success.'<br />';


输出 3

2、获取id为1的员工数据

$result = PostCurl($url.'megacorp/employee/1', '', '', 'GET');
print_r($result);

返回数据
Array
(
    [_index] => megacorp
    [_type] => employee
    [_id] => 1
    [_version] => 28
    [found] => 1
    [_source] => Array
        (
            [first_name] => John
            [last_name] => Smith
            [age] => 25
            [about] => I love to go rock climbing
            [interests] => Array
                (
                    [0] => sports
                    [1] => music
                )

        )

)

3、搜索全部员工

$result = PostCurl($url.'megacorp/employee/_search', '', '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 4
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 3
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 2
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => Jane
                                    [last_name] => Smith
                                    [age] => 32
                                    [about] => I like to collect rock albums
                                    [interests] => Array
                                        (
                                            [0] => music
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 1
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => John
                                    [last_name] => Smith
                                    [age] => 25
                                    [about] => I love to go rock climbing
                                    [interests] => Array
                                        (
                                            [0] => sports
                                            [1] => music
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 3
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => Douglas
                                    [last_name] => Fir
                                    [age] => 35
                                    [about] => I like to build cabinets
                                    [interests] => Array
                                        (
                                            [0] => forestry
                                        )

                                )

                        )

                )

        )

)

4、搜索姓 last_name等于Smith的员工, 轻量的搜索方法

$result = PostCurl($url.'megacorp/employee/_search?q=last_name:Smith', '', '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 6
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 2
            [max_score] => 0.30685282
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 2
                            [_score] => 0.30685282
                            [_source] => Array
                                (
                                    [first_name] => Jane
                                    [last_name] => Smith
                                    [age] => 32
                                    [about] => I like to collect rock albums
                                    [interests] => Array
                                        (
                                            [0] => music
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 1
                            [_score] => 0.30685282
                            [_source] => Array
                                (
                                    [first_name] => John
                                    [last_name] => Smith
                                    [age] => 25
                                    [about] => I love to go rock climbing
                                    [interests] => Array
                                        (
                                            [0] => sports
                                            [1] => music
                                        )

                                )

                        )

                )

        )

)

5、使用Query DSL搜索。搜索姓 last_name等于Smith的员工, 轻量的搜索方法

$param = array(
	'query'	=>	array(
			'match'	=>	array('last_name'	=>	'Smith'),
		),
	);
$result = PostCurl($url.'megacorp/employee/_search', $param, '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 1
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 2
            [max_score] => 0.30685282
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 2
                            [_score] => 0.30685282
                            [_source] => Array
                                (
                                    [first_name] => Jane
                                    [last_name] => Smith
                                    [age] => 32
                                    [about] => I like to collect rock albums
                                    [interests] => Array
                                        (
                                            [0] => music
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 1
                            [_score] => 0.30685282
                            [_source] => Array
                                (
                                    [first_name] => John
                                    [last_name] => Smith
                                    [age] => 25
                                    [about] => I love to go rock climbing
                                    [interests] => Array
                                        (
                                            [0] => sports
                                            [1] => music
                                        )

                                )

                        )

                )

        )

)

6、更加复杂的搜索,搜索姓 last_name等于Smith的员工, 轻量的搜索方法。年龄大于30岁的限定条

$param = array(
	'query'	=>	array(
		'filtered'	=>	array(
			'query'		=>	array(
				'match'	=>	array('last_name'	=>	'Smith'),
			),

			'filter'	=>	array(
				'range'	=>	array(
					'age'	=>	array('gt'	=>	30)
				),
			),
		),
	),
);
$result = PostCurl($url.'megacorp/employee/_search', $param, '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 4
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 1
            [max_score] => 0.30685282
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 2
                            [_score] => 0.30685282
                            [_source] => Array
                                (
                                    [first_name] => Jane
                                    [last_name] => Smith
                                    [age] => 32
                                    [about] => I like to collect rock albums
                                    [interests] => Array
                                        (
                                            [0] => music
                                        )

                                )

                        )

                )

        )

)

7、更加复杂的全文搜索。一项在传统数据库很难实现的功能、我们将会搜索所有喜欢 rock climbing 的员工

$param = array(
	'query'	=>	array(
			'match'	=>	array('about'	=>	'rock climbing'),
		),
	);
$result = PostCurl($url.'megacorp/employee/_search', $param, '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 7
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 2
            [max_score] => 0.16273327
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 1
                            [_score] => 0.16273327
                            [_source] => Array
                                (
                                    [first_name] => John
                                    [last_name] => Smith
                                    [age] => 25
                                    [about] => I love to go rock climbing
                                    [interests] => Array
                                        (
                                            [0] => sports
                                            [1] => music
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 2
                            [_score] => 0.016878016
                            [_source] => Array
                                (
                                    [first_name] => Jane
                                    [last_name] => Smith
                                    [age] => 32
                                    [about] => I like to collect rock albums
                                    [interests] => Array
                                        (
                                            [0] => music
                                        )

                                )

                        )

                )

        )

)

8、段落搜索。我们只需要查询到 about 字段只包含 rock climbing 的短语的员工

$param = array(
	'query'	=>	array(
			'match_phrase'	=>	array('about'	=>	'rock climbing'),
		),
	);
$result = PostCurl($url.'megacorp/employee/_search', $param, '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 2
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 1
            [max_score] => 0.23013961
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 1
                            [_score] => 0.23013961
                            [_source] => Array
                                (
                                    [first_name] => John
                                    [last_name] => Smith
                                    [age] => 25
                                    [about] => I love to go rock climbing
                                    [interests] => Array
                                        (
                                            [0] => sports
                                            [1] => music
                                        )

                                )

                        )

                )

        )

)

9、高亮我们的搜索。但是添加一个 highlight 参数

$param = array(
	'query'	=>	array(
			'match_phrase'	=>	array('about'	=>	'rock climbing'),
		),
	'highlight'	=>	array(
			'fields'	=>	array(
					'about'	=>	array()
				)
		),
	);
$result = PostCurl($url.'megacorp/employee/_search', $param, '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 2
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 1
            [max_score] => 0.23013961
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 1
                            [_score] => 0.23013961
                            [_source] => Array
                                (
                                    [first_name] => John
                                    [last_name] => Smith
                                    [age] => 25
                                    [about] => I love to go rock climbing
                                    [interests] => Array
                                        (
                                            [0] => sports
                                            [1] => music
                                        )

                                )

                            [highlight] => Array
                                (
                                    [about] => Array
                                        (
                                            [0] => I love to go rock climbing
                                        )

                                )

                        )

                )

        )

)

10、统计。例如,找一下员工中最受欢迎的兴趣是什么

$param = array(
	'aggs'	=>	array(
		'all_interests'	=>	array(
			'terms'	=>	array('field'	=>	'interests'),
		),
	),
);
$result = PostCurl($url.'megacorp/employee/_search', $param, '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 2
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 3
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 2
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => Jane
                                    [last_name] => Smith
                                    [age] => 32
                                    [about] => I like to collect rock albums
                                    [interests] => Array
                                        (
                                            [0] => music
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 1
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => John
                                    [last_name] => Smith
                                    [age] => 25
                                    [about] => I love to go rock climbing
                                    [interests] => Array
                                        (
                                            [0] => sports
                                            [1] => music
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 3
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => Douglas
                                    [last_name] => Fir
                                    [age] => 35
                                    [about] => I like to build cabinets
                                    [interests] => Array
                                        (
                                            [0] => forestry
                                        )

                                )

                        )

                )

        )

    [aggregations] => Array
        (
            [all_interests] => Array
                (
                    [doc_count_error_upper_bound] => 0
                    [sum_other_doc_count] => 0
                    [buckets] => Array
                        (
                            [0] => Array
                                (
                                    [key] => music
                                    [doc_count] => 2
                                )

                            [1] => Array
                                (
                                    [key] => forestry
                                    [doc_count] => 1
                                )

                            [2] => Array
                                (
                                    [key] => sports
                                    [doc_count] => 1
                                )

                        )

                )

        )

)

11、如果只想要查询姓 smith 的员工的兴趣汇总情况

$param = array(
	'query'	=>	array(
		'match'	=>	array(
			'last_name'	=>	'smith'
		),
	),
	'aggs'	=>	array(
		'all_interests'	=>	array(
			'terms'	=>	array('field'	=>	'interests'),
		),
	),
);
$result = PostCurl($url.'megacorp/employee/_search', $param, '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 2
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 2
            [max_score] => 0.30685282
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 2
                            [_score] => 0.30685282
                            [_source] => Array
                                (
                                    [first_name] => Jane
                                    [last_name] => Smith
                                    [age] => 32
                                    [about] => I like to collect rock albums
                                    [interests] => Array
                                        (
                                            [0] => music
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 1
                            [_score] => 0.30685282
                            [_source] => Array
                                (
                                    [first_name] => John
                                    [last_name] => Smith
                                    [age] => 25
                                    [about] => I love to go rock climbing
                                    [interests] => Array
                                        (
                                            [0] => sports
                                            [1] => music
                                        )

                                )

                        )

                )

        )

    [aggregations] => Array
        (
            [all_interests] => Array
                (
                    [doc_count_error_upper_bound] => 0
                    [sum_other_doc_count] => 0
                    [buckets] => Array
                        (
                            [0] => Array
                                (
                                    [key] => music
                                    [doc_count] => 2
                                )

                            [1] => Array
                                (
                                    [key] => sports
                                    [doc_count] => 1
                                )

                        )

                )

        )

)

12、汇总还允许多个层面的统计。比如我们还可以统计每一个兴趣下的平均年龄

$param = array(
	'aggs'	=>	array(
		'all_interests'	=>	array(
			'terms'	=>	array('field'	=>	'interests'),
			'aggs'	=>	array(
				'avg_age' =>	array(
					'avg'	=>	array(
						'field'	=>	'age'
					),
				),
			),
		),
	),
);
$result = PostCurl($url.'megacorp/employee/_search', $param, '', 'GET');
print_r($result);

返回数据
Array
(
    [took] => 16
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 3
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 2
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => Jane
                                    [last_name] => Smith
                                    [age] => 32
                                    [about] => I like to collect rock albums
                                    [interests] => Array
                                        (
                                            [0] => music
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 1
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => John
                                    [last_name] => Smith
                                    [age] => 25
                                    [about] => I love to go rock climbing
                                    [interests] => Array
                                        (
                                            [0] => sports
                                            [1] => music
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [_index] => megacorp
                            [_type] => employee
                            [_id] => 3
                            [_score] => 1
                            [_source] => Array
                                (
                                    [first_name] => Douglas
                                    [last_name] => Fir
                                    [age] => 35
                                    [about] => I like to build cabinets
                                    [interests] => Array
                                        (
                                            [0] => forestry
                                        )

                                )

                        )

                )

        )

    [aggregations] => Array
        (
            [all_interests] => Array
                (
                    [doc_count_error_upper_bound] => 0
                    [sum_other_doc_count] => 0
                    [buckets] => Array
                        (
                            [0] => Array
                                (
                                    [key] => music
                                    [doc_count] => 2
                                    [avg_age] => Array
                                        (
                                            [value] => 28.5
                                        )

                                )

                            [1] => Array
                                (
                                    [key] => forestry
                                    [doc_count] => 1
                                    [avg_age] => Array
                                        (
                                            [value] => 35
                                        )

                                )

                            [2] => Array
                                (
                                    [key] => sports
                                    [doc_count] => 1
                                    [avg_age] => Array
                                        (
                                            [value] => 25
                                        )

                                )

                        )

                )

        )

)

删除指定日期之前的数据

curl -XDELETE 'http://localhost:9200/logstash-2017.02.25*'

上面这些已经可以基本掌握Elasticsearch的操作,后面更深入的、分布式集群,等...... 敬请关注~

阅读全文

mac安装pkg-config
发表于 2016-7-26 | 浏览(2082) | 服务器

下载源码包

curl http://pkgconfig.freedesktop.org/releases/pkg-config-0.28.tar.gz -o pkg-config-0.28.tar.gz

解压安装包

tar -xf pkgconfig-0.18.tar.gz

开始安装

./configure  --with-internal-glib
make
sudo make install

阅读全文

CentOS下PHP7安装mysql扩展
发表于 2016-7-12 | 浏览(4961) | 服务器

从PHP7开始mysql扩展正式移除,如果系统db需要mysql方式操作数据库,我们可以手动安装(建议使用PDO操作数据库)

1、下载mysql扩展包

git clone https://github.com/php/pecl-database-mysql     mysql   --recursive
cd mysql
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

2、vim /etc/php.ini 添加扩展包

extension=mysql.so

保存后重启php-fpm服务

php -m 查看就有了

阅读全文

终端高亮 vim高亮 彩色 变色
发表于 2016-7-10 | 浏览(2211) | 服务器

mac终端彩色配置   vim ~/.bash_profile   在文件中添加

alias ls="ls -G"
保存后重启终端使用 ls 测试即可

vim语法高亮   vim ~/.vimrc  在文件中添加

彩色
  syntax on

行号
  set nu

底部显示行号和百分比
  set ruler

保存后使用vim打开一个文件瞧一瞧即可看见效果了

阅读全文

Mac phpize错误解决方案
发表于 2016-7-10 | 浏览(2040) | 服务器
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:        
Zend Module Api No:     
Zend Extension Api No:  

解决方案
  sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include /usr/include

  MacOSX10.11.sdk 是当前系统下的文件,系统不同版本对应不同的文件名称,可以使用ls先看一下SDK文件名称做相应的修改再执行命令
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

解决方案
  brew install autoconf

阅读全文

TOP