目录
一:MariaDB的安装与启动
1.安装前需要删除系统已存在的mysql及mariadb
2.安装mariadb
3.启动并添加开机自启
4.常用启动停止命令
二:登录建库建表
1.登录
2.建库建表
三:编写脚本清洗数据并进行入库
效果展示
[root@kafka-2 ~]# rpm -qa|grep mysql
[root@kafka-2 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
[root@kafka-2 ~]#
使用 yum remove 全部卸载
[root@kafka-2 ~]# yum remove mysql*
已加载插件:fastestmirror
参数 mysql* 没有匹配
不删除任何软件包
[root@kafka-2 ~]# yum remove mariadb*
已加载插件:fastestmirror
删除多余的文件
[root@kafka-2 ~]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/lib64/mysql
/usr/share/filebeat/module/mysql
/usr/share/filebeat/module/zeek/mysql
[root@kafka-2 ~]# find / -name mariadb
[root@kafka-2 ~]#
[root@kafka-2 ~]# yum install mariadb* -y
[root@kafka-2 ~]# systemctl start mariadb
[root@kafka-2 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@kafka-2 ~]#
systemctl start mariadb #启动MariaDB
systemctl stop mariadb #停止MariaDB
systemctl restart mariadb #重启MariaDB
systemctl enable mariadb #设置开机启动
[root@kafka-2 ~]# mysql -u root -p
Enter password:
首次登录不需要密码,为了安全最好设置密码。
MariaDB [(none)]> set password for root@localhost = password("123456");
# 查看哪些数据库
show databases;
# 新建数据库
create database mysql;
# 切换数据库
use mysql;
# 查看数据库内容,有哪些表
show tables;
# 查看user表内容
select * from user \G;
# 8.0版本的user表里,没有password字段,取而代之的是authentication_string
# 允许任何人登录进来 密码是123456
grant all on *.* to "sc"@"%" identified by "123456";
# 更新表内容
flush privileges;
# 建立数据表
create table nginxlog2(
id int primary key auto_increment,
dt varchar(256) ,
prov varchar(256),
isp varchar(256),
bd varchar(256)
) charset=utf8;
# 查看表结构
desc 表名
# 修改表名
rename table 原表名 to 新表名;
# 表里添加内容
insert into nginxlog2(dt,pro,isp,bd) values("xxx",xxx,"xxx",xxx) ;
# 查看表属性
show create table user
# 删除表
drop table user
# 删除表里内容
delete from user where username="sc"
[root@kafka-2 ~]# cat consumers.py
import json
import requests
import time
import pymysql
taobao_url = "https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip="
# 查询ip地址的信息(省份和运营商isp),通过taobao网的接口
def resolv_ip(ip):
response = requests.get(taobao_url + ip)
if response.status_code == 200:
tmp_dict = json.loads(response.text)
prov = tmp_dict["data"]["region"]
isp = tmp_dict["data"]["isp"]
return prov, isp
return None, None
# 将日志里读取的格式转换为我们指定的格式
def trans_time(dt):
# 把字符串转成时间格式
timeArray = time.strptime(dt, "%d/%b/%Y:%H:%M:%S")
# timeStamp = int(time.mktime(timeArray))
# 把时间格式转成字符串
new_time = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
return new_time
# 从kafka里获取数据,清洗为我们需要的ip,时间,带宽
from pykafka import KafkaClient
client = KafkaClient(hosts="192.168.149.151:9092,192.168.149.152:9092,192.168.149.153:9092")
topic = client.topics['nginxlog']
balanced_consumer = topic.get_balanced_consumer(
consumer_group='testgroup',
auto_commit_enable=True,
zookeeper_connect='kafka-1:2181,kafka-2:2181,kafka-3:2181'
)
# consumer = topic.get_simple_consumer()
for message in balanced_consumer:
if message is not None:
line = json.loads(message.value.decode("utf-8"))
log = line["message"]
tmp_lst = log.split()
ip = tmp_lst[0]
dt = tmp_lst[3].replace("[", "")
bt = tmp_lst[9]
dt = trans_time(dt)
prov, isp = resolv_ip(ip)
if prov and isp:
print(prov, isp, dt,bt)
db = pymysql.connect(host="192.168.149.152", user="root", password="654321wq", port=3306, db="consumers", charset="utf8")
cursor = db.cursor()
try:
cursor.execute('insert into nginxlog(dt,prov,isp,bd) values("%s", "%s", "%s", "%s")' % (dt, prov, isp, bt))
db.commit()
print("保存成功")
except Exception as err:
print("修改失败", err)
db.rollback()
db.close()
数据已入库,后续则需要使用python进行web平台的开发,实现在web页面对日志进行分析,报警等功能的图形展示..