首页 前端知识 阿里巴巴中间件canal的搭建和使用以及linux命令下使用mail发送html格式的邮件

阿里巴巴中间件canal的搭建和使用以及linux命令下使用mail发送html格式的邮件

2024-09-10 23:09:40 前端知识 前端哥 681 324 我要收藏

一、阿里巴巴中间件canal的搭建和使用

    canal可以用来监控数据库数据的变化(binlog日志),从而获得指定数据的变化。canal是应阿里巴巴存在杭州和美国的双机房部署,存在跨机房同步的业务需求时开始逐步的尝试基于数据库的日志解析,获取增量变更进行同步,由此衍生出了增量订阅&消费的业务。现实业务中非常常用的包括数据库镜像、实时备份、多级索引及cache刷新等。搭建canal的使用需要 用到mysql, canal服务端以及client. publish:October 23, 2018 -Tuesday

A:mysql的相关配置修改等处理

1,修改mysql配置文件my.cnf添加以下配置:

[mysqld]
server_id = 1
log-bin=mysql-bin
binlog_format = ROW

 2,重启mysql的准备工作:

[root@123 ~]# /etc/init.d/mysqld restart
[root@123 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. .....
mysql> GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal_user'@'%' IDENTIFIED BY 'password';
mysql> flush privileges;
ERROR 1146 (42S02): Table 'mysql.servers' doesn't exist

     重启mysql后在添加好用户之后执行flush privileges;碰到问题:ERROR 1146 (42S02): Table 'mysql.servers' doesn't exist,推测是在重启的时候造成的系统表异常,那就执行修复这个表repair table servers,但看来还是不行,最后删除原表重建表,然后正常了,感觉这个表原来存在(能删除)但命令中读不到这个表一样。不过删除重建的方案可行。

mysql> repair table servers;
+---------------+--------+----------+-------------------------------------+
| Table         | Op     | Msg_type | Msg_text                            |
+---------------+--------+----------+-------------------------------------+
| mysql.servers | repair | Error    | Table 'mysql.servers' doesn't exist |
| mysql.servers | repair | status   | Operation failed                    |
+---------------+--------+----------+-------------------------------------+
2 rows in set (0.02 sec)
mysql> flush privileges;
ERROR 1146 (42S02): Table 'mysql.servers' doesn't exist
mysql> drop table servers;
ERROR 1051 (42S02): Unknown table 'mysql.servers'
mysql>         ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='MySQL Foreign Servers table';^C
mysql> CREATE TABLE `servers` (
    ->         `Server_name` char(64) NOT NULL,
    ->         `Host` char(64) NOT NULL,`Db` char(64) NOT NULL,
    ->         `Username` char(64) NOT NULL,
    ->         `Password` char(64) NOT NULL,
    ->         `Port` int(4) DEFAULT NULL,
    ->         `Socket` char(64) DEFAULT NULL,
    ->         `Wrapper` char(64) NOT NULL,
    ->         `Owner` char(64) NOT NULL,
    ->         PRIMARY KEY (`Server_name`)
    ->         ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='MySQL Foreign Servers table';
Query OK, 0 rows affected (0.09 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

 B:canal服务端的下载安装及配置启动

    下载最新版的canal,看公告canal在历经了近8个月的沉寂, 有了一个里程碑式的重大版本发布, 对应版本为1.1.*。新版本性能/功能/架构上多方面的重要改造, 对于未来canal发展有着重要的意义, 因此决定直接将1.0.26 alpha5直接release转为1.1.0后续会停止1.0.x版本发布, 会专注在1.1.x版本上的开发和维护, ps. 目前1.1.0是完全向前兼容, 我这里就用最新的了。

[root@123 download]# cd /opt/download/
[root@123 download]# mwget https://github.com/alibaba/canal/releases/download/canal-1.1.0/canal.deployer-1.1.0.tar.gz
#不要直接执行 tar zxvf,带上-C参数,canal还是不够专业,github上的东西解压都会解到一个文件夹里,而canal解压则全部放到了当前目录,搞得不一小心当前目录一大堆文件夹和文件。
[root@123 download]# tar zxvf canal.deployer-1.1.0.tar.gz -C /opt/modules/canal
[root@123 download]# cd /opt/modules/canal/
[root@123 canal]# ll
total 16
drwxr-xr-x 2 root root 4096 Oct 22 11:17 bin
drwxr-xr-x 5 root root 4096 Oct 22 11:17 conf
drwxr-xr-x 2 root root 4096 Oct 22 11:17 lib
drwxrwxrwx 2 root root 4096 Aug 20 13:55 logs
[root@123 canal]# vim conf/example/instance.properties
#此项ID不能和mysql配置中的server_id重复
canal.instance.mysql.slaveId=2
#username/password
canal.instance.dbUsername=canal_user
canal.instance.dbPassword=your_password
canal.instance.connectionCharset=UTF-8
[root@123 canal]# bin/startup.sh
#查看canal日志正常启动the canal server is running now ......
[root@123 canal]# tail -f logs/canal/canal.log
2018-10-22 11:22:41.481 [main] INFO  com.alibaba.otter.canal.deployer.CanalController - ## start the canal server[192.168.90.***:11111]
2018-10-22 11:22:47.752 [main] INFO  com.alibaba.otter.canal.deployer.CanalLauncher - ## the canal server is running now ......
#查看example日志正常启动start successful....
[example/instance.properties]
2018-10-22 15:27:46.673 [main] ERROR com.alibaba.druid.pool.DruidDataSource - testWhileIdle is true, validationQuery not set
2018-10-22 15:27:47.262 [main] INFO  c.a.otter.canal.instance.spring.CanalInstanceWithSpring - start CannalInstance for 1-example 
2018-10-22 15:27:47.289 [main] INFO  c.a.otter.canal.instance.core.AbstractCanalInstance - start successful....
2018-10-22 15:27:47.391 [destination = example , address = /127.0.0.1:3306 , EventParser] WARN  c.a.o.c.p.inbound.mysql.rds.RdsBinlogEventParserProxy - prepare to find start position just show master status

 C: canal客户端的使用

        需要下载和启动canal client,因为canal只要启动了canal client才能让canal去主库拉取binlog日志从而显示出来。网上有两种例子,

        一种是直接在linux终端里郭浩然example执行shell如下。

[root@123 download]# mwget https://github.com/alibaba/canal/releases/download/canal-1.1.0/canal.example-1.1.0.tar.gz
[root@123 download]# tar zxvf canal.example-1.1.0.tar.gz  -C /opt/modules/canan_example/
[root@123 download]# cd /opt/modules/canal_example

        二是可以在windows下下载阿里提供的一个客户端例子:https://github.com/alibaba/canal/archive/canal-1.0.19.zip 并将其导入到Eclipse中。修改 SimpleCanalClientTest类中的newSingleConnector第一个参数ip地址为canal服务端的ip地址(如上:start the canal server[192.168.90.***)运行java, 如下图:

    但很遗憾,目前修改数据库尚未看到成功,目前来看是因为我的mysql问题(5.7),mysql5.7引入了两个系统表mysql.server_cost和mysql.engine_cost。修改配置后重启时出现了很多问题,总是找不到很多系统表engine_cost,从日志上看也有报很多这种日志,导致主丛同步失败。后面再找问题并重试吧。

    其它事项:mysql的日志默认使用的都是UTC,因此会造成显示数字与北京时间相差8个小时,如下示例为下午15:12显示的日志,在MySQL 5.7.2新增了log_timestamps 这个参数,该参数主要是控制 error log、genera log,等等记录日志的显示时间参数

2018-10-22T07:12:00.692211Z 3 [Note]
#查询mysql显示值为UTC
SHOW GLOBAL VARIABLES LIKE 'log_timestamps';
|log_timestamps |              UTC     |
#修改mysql的配置文件my.cnf [mysqld]增加一条log_timestamps的配置
log_timestamps=SYSTEM
#修改重启MYSQl后即恢复正常,示例:
2018-10-22T15:36:20.376849+08:00 14 [Note] Start binlog_dump to master_thread_id(14) slave_server(2), pos(mysql-bin.000002, 4)

 二、linux命令下使用mail发送html格式的邮件

    今天在写一个shell脚本的时候需要发送html格式邮件最佳,在linux命令下使用mail命令可以轻松实现发送html格式邮件,只在邮件头部指定内容格式即可,格式如下:

[online@USER ngx]$ mail -s "$(echo -e "主题\nContent-Type: text/html; charset=utf-8")"  收件人 < 内容来源
[online@USER ngx]$ echo 内容 | mail -s "$(echo -e "主题\nContent-Type: text/html; charset=utf-8")" 收件人

    看上面的代码,真的很精巧,以前使用echo的时候还总觉得echo -e没什么用处,但这个地方精巧地使用echo -e,使得subject后自动输入了一个回车,从而实现在头部定义Content-Type。prefect!

    另外使用sendmail的话可以直接将编辑好的html邮件文本发送出去,比如编辑好文件mail.htm,格式内容示例如下:

From: sender<sender@xxx.com>
To: receiver<receiver@xxx.com>
Subject: subject
Content-Type: text/html;charset=utf-8
<html>
<body>
内容
</body>
</html>

        直接使用此命令:sendmail -t $sendmail -t < mail.htm  即可发送html邮件。sendmail没有试验,留在这里做个笔记,下次要用的时候再来看吧,

转载请注明出处或者链接地址:https://www.qianduange.cn//article/18054.html
评论
发布的文章

关于HTML的知识

2024-09-18 23:09:36

js简单实现轮播图效果

2024-09-18 23:09:36

CSS3美化网页元素

2024-09-18 23:09:27

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!