In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Realize Python connection to Mysqln and its application
Python connects to mysql database, which is a very important module of python application. Pytho connection to Mysqln needs to connect to the mysql module imported into python. By connecting to the database through python, we can achieve real-time monitoring of the status of local resources.
1. First, we need to install the MySQL module.
[root@centos mem] # yum install MySQL-python-y
2. Then we can script directly.
[root@centos pytonjiaoben] # cat mysql.py import MySQLdb as mysql # # this is to import the MySQL-python module con = mysql.connect (user= "root", passwd= "123456",\ db= "mem", host= "127.0.0.1") # connect to the local database mem, specify the name of the database and the host address User name and password con.autocommit (True) # set to auto-commit mode, which means that each query operation is processed as a separate transaction Immediately execute cur = con.cursor () # create a cursor object for i in range (10): # here do a for loop to write data sql = 'insert into mem values (% d, "user%d")'% (I I) # define sql statement cur.execute (sql) # # execute sql statement
Note:
# # you must create the database and data table first, and mysql must set a password.
3. Execution result
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 executed the script, we found that the data had been written into the database.
4. Now we may wonder what's the use of writing it in like this. Well, let's go on to write a script about the application of mysql. Now we write the memory usage of the server to mysql, and write it to mysql, and then we can use flask call to monitor local resources.
Let's check our memory files.
[root@centos pytonjiaoben] # cat / proc/meminfo MemTotal: 1528700 kBMemFree: 224072 kBBuffers: 130432 kBCached: 604432 kBSwapCached: 8440 kB
6. Write scripts to get the amount of memory used
[root@centos 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 (): # # get the total, free, and Quantity of 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) 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)'% (mem_used,cur_time) cur.execute (sql) while True:saveMem () time.sleep (1) # sleep 1 second
7. View the results
[root@centos pytonjiaoben] # python mem/mysql.py 562563563563
# View the database
Mysql > select * from memory;+-+-+ | memory | time | +-+-+ | 577012 | 1511869204 | 577004 | 1511869205 | | 576872 | 1511869206 | +-+-+
8. We will find that it will obtain the usage of memory in real time, so that our goal has been achieved, as long as there is data input into mysql, and then as long as it is called through flask to make a graph, we can achieve monitoring.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.