0.环境及各软件版本:
Ubuntu 16.04 Server
MySQL 5.7
JDK 1.8 (已安装不在这里赘述)
golang 1.6
go-mysql-elasticsearch(数据同步)
1.安装MySQL
#更新源
apt update
#安装MySQL,此过程中会提示设置root密码
apt-get install mysql-server mysql-client
#使用mysql_secure_installation安全设置
#可更改root密码、移除MySQL的匿名用户、禁止root远程登录、删除test数据库
mysql_secure_installation
#登陆
mysql -u root -p
2.安装Elasticsearch
#官方安装文档
https://www.elastic.co/guide/en/elasticsearch/reference/5.6/zip-targz.html
#安装Elasticsearch5.6
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.1.tar.gz
sha1sum elasticsearch-5.6.1.tar.gz
tar -xzf elasticsearch-5.6.1.tar.gz
#启动
cd elasticsearch-5.6.1/
./bin/elasticsearch -d -p pid
#访问测试
curl http://localhost:9200/
3.安装Golang,配置go-mysql-elastisearch
apt install golang-go
#查看go的安装目录
whereis go
#配置环境变量
vi /etc/profile
export GOPATH=/usr/lib/go
export PATH=$PATH:$GOPATH/bin
source /etc/profile
#安包管理工具godep
go get github.com/tools/godep
#获取go-mysql-elastisearch插件
go get github.com/siddontang/go-mysql-elasticsearch
#编译
cd $GOPATH/src/github.com/siddontang/go-mysql-elasticsearch
make
#修改.etc/river.toml配置文件,以下是我的配置, 其实按照作者给出的示例还可以做其他配置,这里就不一一列举了
schema = "opt_blog"
tables = ["blog"]
[[rule]]
schema = "opt_blog"
table = "blog"
index = "opt_blog"
type = "blog"
#修改MySQL配置文件,开启binlog,因为go-mysql-elasticsearch是通过binlog来获取数据更新的,所以必须开启binlog
#如果对go-mysql-elasticsearch有兴趣的话可以看下这篇文章 http://www.jianshu.com/p/05cff717563c
[mysqld]
log-bin=mysql-bin #添加这一行就ok
binlog-format=ROW #选择row模式
server_id=1 #开启binlog时需要设置,不然启动不了
#同步数据,可以通过nohup.out文件查看执行状态
nohup ./bin/go-mysql-elasticsearch -config=./etc/river.toml > nohup.out 2>&1 &
#向blog表中插入一条数据,测试搜索
curl http://localhost:9200/opt_blog/blog/1