Implement Pytho connection Mysqln and applications
Python connects MySQL database, is a very important module of Python application, Pytho connection mysqln need to connect the MySQL module that import python , connect database through Python, We can implement real-time monitoring of local resource status.
1, first we need Install first MySQL Module
[email protected] mem]# Yum install mysql-python-y
2, Next we can script directly
[[Email protected] pytonjiaoben]# cat mysql.py import mysqldb as mysql ## #这是导入 Mysql-python module Con = mysql.connect (user= "root", passwd= "123456", db= "Mem", host= "127.0.0.1") ## #连接本地的数据库mem, specify the name of the database, the host address, Username and password Con.autocommit (True) ## #设置为自动提交模式, which indicates that each query operation is handled as 1 separate transactions, Immediately execute cur = con.cursor () ## #创建1个游标对象for i in range ( : ## #这里做个for循环写入数据 sql = ' Insert into mem values (%d, "user%d") '% (i,i) ## #定义sql语句 cur.execute (SQL) # #执Row SQL statements
Attention:
# #数据库和数据表必须是你先创建的, and MySQL must set the password.
3. Implementation results
Mysql> SELECT * FROM mem;+------+---------+| ID | Name |+------+---------+| 0 | User0 | | 1 | User1 | | 2 | User2 | | 3 | User3 | | 4 | User4 | | 5 | User5 | | 6 | User6 | | 7 | User7 | | 8 | User8 | | 9 | User9 |
When we execute the script, we find that the data is already written in the database.
4, now we may have questions, so what is the use of writing it, well, we go on to write a script, about the application of MySQL. Now our memory usage of the server, written to MySQL, written on MySQL can be called through flask, to implement monitoring of local resources.
Let's take a look at our memory files.
[Email protected] pytonjiaoben]# cat/proc/meminfo memtotal:1528700 kbmemfree:224072 kbbuffers: 130432 kbcached:604432 kbswapcached:8440 KB
6. Write a script to get the amount of memory used
[[email protected] pytonjiaoben]# cat mem/mysql.py # -*- coding:utf-8 -*-import timeimport osimport mysqldb as mysqldb = mysql.connect (user= "root ", passwd=" 123456 ", db=" Memory ", host=" localhost ") db.autocommit (True) cur=db.cursor () def Savemem (): Amount of # #获取memory的total, free, buff a= "awk ' nr==1{print $2} ' /proc/meminfo" file = os.popen (a) total=int (File.read ()) b= "awk ' nr==2{print $2} ' /proc/meminfo" file =os.popen (b) free =int (File.read ()) c= "awk ' nr==3{print $2} ' /proc/meminfo" file = os.popen (c) &nbSp; buffer =int (File.read ()) d= "awk " NR==4{print $2} ' /proc/meminfo ' file =os.popen (d) cache =int (File.read ()) mem_used=total-free-buffer-cache print mem_used/1024 #mem = mem_used/1024 cur_time = int (Time.time ()) sql = ' insert into memory (memory, time) value (%s,%s) '% (mem_used,cur_ TIME) cur.execute (SQL) While true:savemem () time.sleep (1) # sleep 1 second
7. View Results
[[email protected] pytonjiaoben]# python mem/mysql.py 562563563563
# # #查看数据库
Mysql> SELECT * from memory;+--------+------------+| Memory | Time |+--------+------------+| 577012 | 1511869204 | | 577004 | 1511869205 | | 576872 | 1511869206 |+--------+------------+
8, we will find that it will be real-time access to the use of memory, so that our purpose is achieved, as long as there is data input to MySQL, and then as long as the call through the flask, make a diagram, you can achieve monitoring.
Mysql;python;memory;
Implement Pytho connection Mysqln and applications