当前位置:网站首页>Small project on log traffic monitoring and early warning | environment foundation 2
Small project on log traffic monitoring and early warning | environment foundation 2
2022-06-11 00:53:00 【Smelly Nian】
Mysql: Facilitate data persistence
MySQL The content of :
- install
- edition :5.7.34 Binary installation
- Business Edition charge Community Edition free
- MySQL Support distributed and clustered Such as : Ali tencent Baidu Meituan Xiaomi and others are used MySQL
- Script installation Installation package mysql Official website
- edition :5.7.34 Binary installation
- Building tables and databases
Library in mysql It is equivalent to a folder , It's a place to store data
Building database :create database Library name ;
Check the library :use Library name ;
# Create table
create table nginxlog (
id int primary key auto_increment,
dt datetime not null,
prov int ,
isp int,
bd float( bandwidth )
) CHARSET=utf8;
surface : A file for storing data , It can be understood as one that only MySQL Readable documents , The data is stored in this file line by line
See the table :show tables;
Field : A field is a column
primary key: Do primary key ( Each row of this column cannot be the same )
auto_increment: Self increasing
datetime: The date will be recorded Hours, minutes and seconds
not null: This column cannot be empty
CHARSET=utf8 Set the character set to utf8
desc indicate ; Look at the structure of this table
show create table Table name : Look at the initial statement that created this table
drop table Table name ; Delete table
drop database Library name ; Delete Library ( Including all the tables inside )
select * from Table name ; Query all the data in this table (* Represents all fields You can also select one or more fields Separated by commas )
insert into indicate ( One or more fields ) values( Each value is separated by a comma , The string is enclosed by a colon ),(),(); Insert multiple data ( The front and back correspond one by one )
varchar( Set the maximum length ): Variable length string
row That's ok
record Record That is, line by line
/data/mysql: There are two structures in the data directory
.frm Storage table structure
.ibd Storing indexes and real data
Delete data :delete from Table name where Conditions ( Such as id=2);
Modifying data :update Table name set Modified value ( Such as isp=3) where Conditions ( Such as id=3);
- Additions and deletions
- Pymysql Library usage
Mysql Configuration file for :/etc/my.cnf
Data directory :/data/mysql
Mysql Two mainstream versions :5.7.34( Stable )8.0.27( new function unstable )
Python Of pymysql library :python In the operation MySQL The library of ( Additions and deletions )
‘[email protected]’: Only allow login on this computer Cannot log on remotely
Create users and authorizations

These two users are not one user ( One is 0 Users of network segment There is also a user connected to any network segment )
first sc User priority of is lower than ( Because the user below is more accurate Therefore, the priority is higher )
Remote login will automatically connect to the higher priority
grant all on *.* to 'sc'@'%';
effect : Giving ‘sc’@‘%’ All permissions of this user ( In addition to the addition, deletion and modification of the search ) Include add delete change check
grant It is an authorized order
all On behalf of all authorities , Include add delete change check , But not including grant jurisdiction
on *.*: On all libraries , Operate on all tables ( first * On behalf of Ku the second * Represents all tables )
to ‘sc’@‘%’ To this user
grant You can also create users while authorizing :
![]()
show warnings; Is to view warnings
show processlist; See who logged in remotely
select version(); Check the version of the database
import pymysql
db=pymysql.connect(host='192.168.0.36',user='sc',password='Sanchuang123#',database='mysql') # Create an object You can also leave the library unspecified ( It's specified here mysql library , After that, there is no need for use Switch Libraries )
cursor=db.cursor() # Create a cursor
Database cursor : Just a temporary cache space in the database , Cursors help execute sql Object of statement
You name .execute(“sql sentence ”): The number returned is the number of rows of the result
You name .fetchone() Get data one by one ( There will be tour marks to which line you are admitted )
You name .fetchmany()
You name .fetchall() Get all the data
Close the connection ( The cursor name disappears ):
Connection name .close()
Mysql Commands are not case sensitive Table name, function name, password, etc. are case sensitive
establish hejin In the database nginxlog surface
Mysql There's a now function Therefore, when transferring values, you can now()
Database transactions : A group of ( Or a ) Operations make up a transaction
A transaction consists of two results : All or nothing Or failure
commit Submit : This means that all operations in this transaction have been successfully completed ( Data is written from memory to disk At first, the data will only be stored in memory buffer in , Then drop the plate to disk Inside Keep forever Persistence )
rollback Roll back ( revoke ): Everything that has just been done is undone , Rollback to the original state ( An operation failed All three operations are undone )
The operation of creating tables and databases will automatically commit and rollback There's no need for us to intervene
And the operation of moving data (insert update delete ) You can use commit and rollback
try:
# perform sql sentence
cursor.execute(sql)
# Commit to database execution
db.commit()
except:
# Roll back if an error occurs
db.rollback()
As long as the primary keys are different, they are different records , If the primary key is set to increase automatically , If it is not specified, it will be added automatically 1
Automatically give the offset to kafka preservation


Judge url get Whether the request is successful 200 On behalf of success

Splitting tables is to reduce storage Convenient access
# For sub table , Our script should be modified accordingly .
# Insert data into nginxlog Before the table
#1、 obtain prov_index and isp_index Data in table
#2、 Determine what to insert prov and isp In the data not in the table
#3、 If prov_index and isp_index Data exists , Get the corresponding id, Store it again
#4、 If prov_index and isp_index The data doesn't exist , Just insert the new data into these two tables , And then get id Field , Store in the data table nginxlog


flush privileges; Refresh the permissions
copy on right: Realistic reproduction
边栏推荐
- 阻塞隊列 — DelayedWorkQueue源碼分析
- 跳转页面后回不去默认页面
- [network planning] 3.2 transport layer - UDP: connectionless service
- Idea setting background picture (simple)
- compiler explorer
- 快手处置超过5.4万个违规账号:如何打击平台上的违规账号
- 哈工大软件构造复习——LSP原则,协变和逆变
- Fastdfs quick start
- [MVC&Core]ASP. Introduction to net core MVC view value transfer
- Canvas drawing line break
猜你喜欢
![[network planning] 2.2.3 user server interaction: cookies](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 2.2.3 user server interaction: cookies

Dictionary sort of array

Download Google gcr IO image

市值215亿,这个四川80后会让电视机成为历史?
![[network counting] 1.4 network delay, packet loss and throughput](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network counting] 1.4 network delay, packet loss and throughput

负载均衡策略图文详解

为什么使用 Golang 进行 Web 开发
![[network planning] 2.2.4 Web cache / proxy server](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 2.2.4 Web cache / proxy server

Random points in non overlapping rectangles

MESI cache consistency protocol for concurrent programming
随机推荐
Dictionary sort of array
SLAM卡尔曼滤波&&非线性优化
[MVC&Core]ASP. Introduction to net core MVC view value transfer
With a market value of 21.5 billion yuan, will the post-80s generation in Sichuan make TV history?
Computer screen recording free software GIF and other format videos
阻塞队列 — DelayedWorkQueue源码分析
Blog recommendation | building IOT applications -- Introduction to flip technology stack
qt程序插件报错plugin xcb
浅谈有赞搜索质量保障体系 v2021
循环结构语句
QT program plug-in reports an error plugin xcb
12324243242
compiler explorer
Kubeflow 1.2.0 installation
Safety training management measures
Qt线程与界面
Room第一次使用
Random points in non overlapping rectangles
年金险还能买吗?年金险安不安全?
The mystery of number idempotent and perfect square