This article mainly explains how to synchronize Mysql and Mongodb data through the logstash-output-mongodb plugin. The source data is stored in Mysql, and the target database is Mongodb, a non-relational database.
0. Premise
1) The source database has been installed: Mysql;
2) The destination database has been installed: Mongodb;
3) The logstash and related plugins logstash-output-mongodb have been installed.
Installation steps reference: http://blog.csdn.net/laoyang360/ article/details/65448962
1. Detailed explanation of synchronization conf configuration
[root@la bin]# cat ./logstash_jdbc_mongo/jdbc_mongo.conf
input {
stdin {
}
jdbc {
‘# 源Mysql数据库地址
jdbc_connection_string => "jdbc:mysql://20.18.11.4:3306/tech?zeroDateTimeBehavior=convertToNull"
#源Mysql的用户名和密码
jdbc_user => "root"
jdbc_password => "rot123"
‘ #true代表记录最后存储的关联列值
record_last_run => "true"
use_column_value => "true"
tracking_column => "id"
‘ #存储位置
last_run_metadata_path => "/opt/logstash/bin/logstash_jdbc_mongo/mongo_info"
clean_run => "false"
‘ #jdbc路径地址
jdbc_driver_library => "/home/lib/mysql-connector-java-5.1.38.jar"
‘ # the name of the driver class for mysql
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "500"
‘ #以下对应着要执行的sql的绝对路径。
statement_filepath => "/opt/logstash/bin/logstash_jdbc_mongo/jdbc_mongo.sql"
‘ #定时字段 各字段含义(由左至右)分、时、天、月、年,全部为*默认含义为每分钟都更新
schedule => "* * * * *"
}
}
filter {
json {
source => "message"
remove_field => ["message"]
}
}
’#输出mongodb的配置
output {
stdout { codec => rubydebug }
mongodb {
‘#对应mongodb的输出集合
collection => "N_CLASS"
’#对应mongodb的输出数据库名称
database => "data"
uri => "mongodb://110.0.12.45:27017"
}
}
2. SQL that needs to synchronize the table
[root@lalogstash_jdbc_mongo]# cat jdbc_mongo.sql
select
*
from n_class
where n_class.id > :sql_last_value
3. Summary
The above realizes the full and incremental synchronization operation between a database table table in the Mysql database and a collection collection in Mongodb.
Full realization: realized through sql statement;
incremental realization: realized through timing and sql_last_value.
When synchronizing, you don't need to do anything in mongodb. After synchronization, you can see the new collection collection after synchronization on the windows client of mongodb: Robomongo.
4. Off-topic (welcome to leave a message for discussion)
PS: The following link refers to the speech topic "The King of NoSQL - Migrating from Relational to MongoDB in One Minute", but so far, I have not found relevant details such as PPT and other materials. Bo friends, who has participated or a better synchronization plan welcomes comments and discussions.
http://geek.csdn.net/news/detail/186266