龚哥哥 爱生活、做自己!
Elasticsearch集群 搜索引擎
发表于 2016-8-17 | 浏览(712) | 服务器

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 | 浏览(1032) | 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 | 浏览(790) | 服务器

下载源码包

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 | 浏览(3091) | 服务器

从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 | 浏览(927) | 服务器

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

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

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

彩色
  syntax on

行号
  set nu

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

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

阅读全文

Mac phpize错误解决方案
发表于 2016-7-10 | 浏览(667) | 服务器
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

阅读全文

Mac安装PHP Swoole扩展
发表于 2016-7-10 | 浏览(1317) | 服务器

系统软件版本

Swoole Version swoole-1.8.9
PHP    Version 5.6.30
MAC    Version 10.12.4

开始安装

wget http://pecl.php.net/get/swoole-1.8.9.tgz
tar -zxvf swoole-1.8.9.tgz
cd swoole-1.8.9
/usr/bin/phpize    # phpize(如果执行 phpize 出错的话,在博客中搜索 phpize 查看解决方案)
./configure --with-php-config=/usr/bin/php-config
make
sudo make install

在 /etc/php.ini 中添加扩展记录

extension=swoole.so

重启apache

sudo apachectl restart

make可能会报错

错误1
/usr/include/php/ext/pcre/php_pcre.h:29:10: fatal error: 'pcre.h' file not found #include "pcre.h"

排查问题 php -m 已经安装pcre扩展了,是本机未安装pcre服务
我们安装一下pcre服务
  brew install pcre
  sudo cp /usr/local/include/pcre.h /usr/include/php/ext/pcre/


错误2
./php_swoole.h:22:10: fatal error: 'php.h' file not found
sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include /usr/include

阅读全文

MySQL中MyISAM引擎表级锁
发表于 2016-7-6 | 浏览(640) | 数据库

表名:lock_read

表类型:MyISAM(此引擎只支持表级锁,不支持行级锁,下面是测试表级锁)

锁类型:表锁

表结构:

CREATE TABLE `lock_read` (
	`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增id',
	`name` char(30) NOT NULL DEFAULT '' COMMENT '姓名',
	`age` tinyint(2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '年龄',
	PRIMARY KEY (`id`)
) ENGINE=`MyISAM` AUTO_INCREMENT=1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ROW_FORMAT=FIXED COMMENT='测试表级锁' CHECKSUM=0 DELAY_KEY_WRITE=0;

添加一条数据:INSERT INTO `lock_read` (`name`, `age`) VALUES ('小龚', 22);

终端一

1、查询本身数据
  SELECT * FROM lock_read;
  +----+--------+-----+
  | id | name   | age |
  +----+--------+-----+
  | 1  | 小龚   | 22  |
  +----+--------+-----+

2、加表锁
  LOCK TABLES lock_read READ;

3、更新数据
  UPDATE lock_read SET age=23 WHERE id=1;
  报错:Table 'lock_read' was locked with a READ lock and can't be updated
  解释:意思是表当前加锁中,不能被任何人更新(包括当前进程)

终端二

1、查询本身数据
  SELECT * FROM lock_read;
  +----+--------+-----+
  | id | name   | age |
  +----+--------+-----+
  | 1  | 小龚   | 22  |
  +----+--------+-----+

2、加表锁
  LOCK TABLES lock_read READ;

3、更新数据
  UPDATE lock_read SET age=24 WHERE id=1;
  报错:Table 'lock_read' was locked with a READ lock and can't be updated
  解释:意思是表当前加锁中,不能被任何人更新(包括当前进程)

  但是如果当前进程跳过步骤2,不对表进行加锁,那么是这样子的:
    前面进程已加锁了,那么当前进程就会进入阻塞状态,等到终端一的进程释放锁后,这条sql才能被执行成功

终端一

1、解锁
  UNLOCK TABLES;

2、更新数据
  UPDATE lock_read SET age=25 WHERE id=1;
  A、如果终端二加锁过了,那么当前进程就会进入阻塞状态,等待终端二的进程解锁后,这条sql才能被只成功
  B、如果终端二没有加锁,那么当前sql将执行成功

阅读全文

PHP中4种排序算法
发表于 2016-7-6 | 浏览(759) | PHP
<?php

/**
 * PHP排序算法Demo
 */

/**
 * [bubble_sort 冒泡排序]
 * @param  [array] $data [需要排序的数据(相邻的数据进行比较调换位置)]
 * @return [array]       [排序好的数据]
 */
function bubble_sort($data)
{
	if(!empty($data) && is_array($data))
	{
		$len = count($data);
		for($i=0; $i<$len; $i++)
		{
			for($k=0; $k<$len-1; $k++)
			{
				if($data[$k] > $data[$i])
				{
					$data[$i] = $data[$i] ^ $data[$k];
					$data[$k] = $data[$i] ^ $data[$k];
					$data[$i] = $data[$i] ^ $data[$k];
				}
			}
		}
	}
	return $data;
}

/**
 * [select_sort 选择排序]
 * @param  [array] $data [需要排序的数据(选择最小的值与第一个调换位置)]
 * @return [array]       [排序好的数据]
 */
function select_sort($data)
{
	if(!empty($data) && is_array($data))
	{
		$len = count($data);
		for($i=0; $i<$len; $i++)
		{
			$t = $i;
			for($j=$i+1; $j<$len; $j++)
			{
				if($data[$t] > $data[$j])
				{
					$t = $j;
				}
			}
			if($t != $i)
			{
				$data[$i] = $data[$i] ^ $data[$t];
				$data[$t] = $data[$i] ^ $data[$t];
				$data[$i] = $data[$i] ^ $data[$t];
			}
		}
	}
	return $data;
}

/**
 * [insert_sort 插入排序]
 * @param  [array] $data [需要排序的数据(把第n个数插到前面的有序数组中,以此反复循环直到排序好)]
 * @return [array]       [排序好的数据]
 */
function insert_sort($data)
{
	if(!empty($data) && is_array($data))
	{
		$len = count($data);
		for($i=1; $i<$len; $i++)
		{
			$tmp = $data[$i];
			for($j=$i-1; $j>=0; $j--)
			{
				if($data[$j] > $tmp)
				{
					$data[$j+1] = $data[$j];
					$data[$j] 	= $tmp;
				} else {
					break;
				}
			}
		}
	}
	return $data;
}

/**
 * [quick_sort 快速排序]
 * @param  [array] $data [需要排序的数据(选择一个基准元素,将待排序分成小和打两罐部分,以此类推递归的排序划分两罐部分)]
 * @return [array]       [排序好的数据]
 */
function quick_sort($data)
{
	if(!empty($data) && is_array($data))
	{
		$len = count($data);
		if($len <= 1) return $data;

		$base = $data[0];
		$left_array = array();
		$right_array = array();
		for($i=1; $i<$len; $i++)
		{
			if($base > $data[$i])
			{
				$left_array[] = $data[$i];
			} else {
				$right_array[] = $data[$i];
			}
		}
		if(!empty($left_array)) $left_array = quick_sort($left_array);
		if(!empty($right_array)) $right_array = quick_sort($right_array);

		return array_merge($left_array, array($base), $right_array);
	}
}


/**
 * 测试
 */
$data = array(10, 3, 6, 1, 24, 18, 7, 8);
echo '<pre>';

// 冒泡排序
print_r(bubble_sort($data));

// 选择排序
print_r(select_sort($data));

// 插入排序
print_r(insert_sort($data));

// 快速排序
print_r(quick_sort($data));


// 以下是结果
Array
(
    [0] => 1
    [1] => 3
    [2] => 6
    [3] => 7
    [4] => 8
    [5] => 10
    [6] => 18
    [7] => 24
)
Array
(
    [0] => 1
    [1] => 3
    [2] => 6
    [3] => 7
    [4] => 8
    [5] => 10
    [6] => 18
    [7] => 24
)
Array
(
    [0] => 1
    [1] => 3
    [2] => 6
    [3] => 7
    [4] => 8
    [5] => 10
    [6] => 18
    [7] => 24
)
Array
(
    [0] => 1
    [1] => 3
    [2] => 6
    [3] => 7
    [4] => 8
    [5] => 10
    [6] => 18
    [7] => 24
)

?>

阅读全文

MySQL默认编码修改
发表于 2016-6-30 | 浏览(615) | 数据库

mac下my.cnf默认位置

/usr/local/mysql/support-files
拷贝一份到/etc目录下 cp my-default.cnf /etc/my.cnf

mysql默认编码并不是utf-8,而是latin1(不支持中文,存储中文乱码)登录后使用 show variables 可以查看

1、vim /etc/my.cnf
  在 [mysqld] 下面添加默认编码
  character_set_server=utf8 
  init_connect='SET NAMES utf8'

2、重启mysql即可

阅读全文

TOP