ubuntu中mysql怎么退出命令_如何使用Python进行MySQL数据库管理
本節(jié)我們將學(xué)習(xí)使用Python進(jìn)行MySQL數(shù)據(jù)庫(kù)管理。Python有多種用于MySQL數(shù)據(jù)庫(kù)管理的模塊,這里使用MySQLdb模塊。MySQLdb模塊是MySQL數(shù)據(jù)庫(kù)服務(wù)器的接口,用于向Python提供數(shù)據(jù)庫(kù)API。
首先我們需要安裝MySQL和Python的MySQLdb包,在終端中運(yùn)行以下命令。
$ sudo apt install mysql-server此命令安裝MySQL服務(wù)器和各種相關(guān)軟件包。安裝軟件包時(shí),系統(tǒng)會(huì)提示我們輸入MySQL root賬戶的密碼。
以下命令用于查看要安裝的MySQLdb包。
$ apt-cache search MySQLdb以下命令安裝MySQL的Python接口。
$ sudo apt-get install python3-mysqldb現(xiàn)在檢查MySQL是否正確安裝,在終端中運(yùn)行以下命令。
student@ubuntu:~$ sudo mysql -u root –p運(yùn)行命令后,如下所示。
Enter password:Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 10Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql>運(yùn)行sudo mysql -u root -p,打開(kāi)MySQL控制臺(tái)。使用以下部分命令列出數(shù)據(jù)庫(kù)和表,并使用數(shù)據(jù)庫(kù)來(lái)存儲(chǔ)我們的操作。
列出所有數(shù)據(jù)庫(kù)。
show databases;使用數(shù)據(jù)庫(kù)。
use database_name;列出所有表。
show tables;以上就是列出所有數(shù)據(jù)庫(kù)、使用數(shù)據(jù)庫(kù)和列出表的命令。
每當(dāng)退出MySQL控制臺(tái)并在一段時(shí)間后再次登錄時(shí),我們就必須使用use database_name命令,將所有操作保存在數(shù)據(jù)庫(kù)中。我們可以通過(guò)以下示例詳細(xì)了解這一點(diǎn)。
現(xiàn)在,我們?cè)贛ySQL控制臺(tái)中使用create database語(yǔ)句創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)。運(yùn)行mysql -u root -p打開(kāi)MySQL控制臺(tái),然后輸入在安裝時(shí)設(shè)置的密碼,按Enter鍵。最后創(chuàng)建數(shù)據(jù)庫(kù)。本節(jié)將創(chuàng)建一個(gè)名為test的數(shù)據(jù)庫(kù),后面也將使用此數(shù)據(jù)庫(kù)。
在終端中運(yùn)行以下命令。
student@ubuntu:~/work/mysql_testing$ sudo mysql -u root –p運(yùn)行命令后,如下所示。
Enter password:Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 16Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql>mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys |+--------------------+4 rows in set (0.10 sec)mysql> create database test;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || sys || test |+--------------------+5 rows in set (0.00 sec)mysql> use test;Database changedmysql>上面的示例程序首先使用show databases列出了所有數(shù)據(jù)庫(kù),接著使用create database創(chuàng)建了數(shù)據(jù)庫(kù)測(cè)試,然后再次使用show databases以查看數(shù)據(jù)庫(kù)是否已創(chuàng)建。我們可以看到數(shù)據(jù)庫(kù)現(xiàn)已創(chuàng)建,接著使用該數(shù)據(jù)庫(kù)來(lái)存儲(chǔ)正在進(jìn)行的操作結(jié)果。
現(xiàn)在將創(chuàng)建一個(gè)用戶并為該用戶授予權(quán)限,運(yùn)行以下命令。
mysql> create user 'test_user'@'localhost' identified by 'test123';Query OK, 0 rows affected (0.06 sec)mysql> grant all on test.* to 'test_user'@'localhost';Query OK, 0 rows affected (0.02 sec)mysql>這里創(chuàng)建了一個(gè)test_user用戶,該用戶的密碼是test123。然后為test_user用戶授予所有權(quán)限。最后通過(guò)quit命令或者exit命令退出MySQL控制臺(tái)。
接下來(lái)我們將學(xué)習(xí)獲取數(shù)據(jù)庫(kù)版本號(hào)、創(chuàng)建表、向表插入一些數(shù)據(jù)、檢索數(shù)據(jù)、更新數(shù)據(jù)和刪除數(shù)據(jù)的示例程序。
1 獲取數(shù)據(jù)庫(kù)版本號(hào)
我們來(lái)看獲取數(shù)據(jù)庫(kù)版本號(hào)的示例程序。創(chuàng)建一個(gè)腳本,命名為get_database_version.py,并在其中寫(xiě)入以下代碼。
import MySQLdb as mdbimport syscon_obj = mdb.connect('localhost', 'test_user', 'test123', 'test')cur_obj = con_obj.cursor()cur_obj.execute("SELECT VERSION()")version = cur_obj.fetchone()print ("Database version: %s " % version)con_obj.close()在運(yùn)行此腳本之前,請(qǐng)務(wù)必保證已經(jīng)完成上述步驟,不應(yīng)該跳過(guò)這些步驟。
運(yùn)行腳本程序,如下所示。
student@ubuntu:~/work/mysql_testing$ python3 get_database_version.py輸出如下。
Database version: 5.7.24-0ubuntu0.18.04.1上面的示例程序獲取了數(shù)據(jù)庫(kù)版本號(hào)。首先導(dǎo)入了MySQLdb模塊,然后根據(jù)給出的包含用戶名、密碼、數(shù)據(jù)庫(kù)名的字符串連接了數(shù)據(jù)庫(kù),接著創(chuàng)建了一個(gè)用于執(zhí)行SQL查詢的cursor對(duì)象。在execute()函數(shù)中,給出了一個(gè)SQL查詢語(yǔ)句。fetchone()函數(shù)獲取了一行查詢結(jié)果。最后輸出結(jié)果。close()方法用于關(guān)閉數(shù)據(jù)庫(kù)連接。
2 創(chuàng)建表并插入數(shù)據(jù)
現(xiàn)在我們創(chuàng)建一個(gè)表,并向其中插入一些數(shù)據(jù)。創(chuàng)建一個(gè)腳本,命名為create_insert_data.py,并在其中寫(xiě)入以下代碼。
import MySQLdb as mdbcon_obj = mdb.connect('localhost', 'test_user', 'test123', 'test')with con_obj: cur_obj = con_obj.cursor() cur_obj.execute("DROP TABLE IF EXISTS books") cur_obj.execute("CREATE TABLE books(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(100))") cur_obj.execute("INSERT INTO books(Name) VALUES('Harry Potter')") cur_obj.execute("INSERT INTO books(Name) VALUES('Lord of the rings')") cur_obj.execute("INSERT INTO books(Name) VALUES('Murder on the Orient Express')") cur_obj.execute("INSERT INTO books(Name) VALUES('The adventures of Sherlock Holmes')") cur_obj.execute("INSERT INTO books(Name) VALUES('Death on the Nile')")print("Table Created !!")print("Data inserted Successfully !!")運(yùn)行腳本程序,如下所示。
student@ubuntu:~/work/mysql_testing$ python3 create_insert_data.py輸出如下。
Table Created !!Data inserted Successfully !!要檢查表是否已成功被創(chuàng)建,需打開(kāi)MySQL控制臺(tái)并運(yùn)行以下命令。
student@ubuntu:~/work/mysql_testing$ sudo mysql -u root -p運(yùn)行命令后,輸出如下。
Enter password:Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 6Server version: 5.7.24-0ubuntu0.18.04.1 (Ubuntu)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql>mysql>mysql> use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+----------------+| Tables_in_test |+----------------+| books |+----------------+1 row in set (0.00 sec)我們可以看到表books已經(jīng)被創(chuàng)建。
3 檢索數(shù)據(jù)
現(xiàn)在我們使用select語(yǔ)句從表中檢索數(shù)據(jù),這里從books表中檢索數(shù)據(jù)。創(chuàng)建一個(gè)腳本,命名為retrieve_data.py,并在其中寫(xiě)入以下代碼。
import MySQLdb as mdbcon_obj = mdb.connect('localhost', 'test_user', 'test123', 'test')with con_obj: cur_obj = con_obj.cursor() cur_obj.execute("SELECT * FROM books") records = cur_obj.fetchall() for r in records: print(r)運(yùn)行腳本程序,如下所示。
student@ubuntu:~/work/mysql_testing$ python3 retrieve_data.py輸出如下。
(1, 'Harry Potter')(2, 'Lord of the rings')(3, 'Murder on the Orient Express')(4, 'The adventures of Sherlock Holmes')(5, 'Death on the Nile')上面的示例程序從表中檢索了數(shù)據(jù)。首先使用了MySQLdb模塊,接著連接了數(shù)據(jù)庫(kù),并創(chuàng)建了一個(gè)cursor對(duì)象來(lái)執(zhí)行SQL查詢。在execute()函數(shù)中,給出了一個(gè)select語(yǔ)句。最后輸出了查詢到的記錄。
4 更新數(shù)據(jù)
如果想在數(shù)據(jù)庫(kù)記錄中進(jìn)行一些更改,我們可以使用update語(yǔ)句,以下是一個(gè)update語(yǔ)句的示例程序。我們創(chuàng)建一個(gè)腳本,命名為update_data.py,并在其中寫(xiě)入以下代碼。
import MySQLdb as mdbcon_obj = mdb.connect('localhost', 'test_user', 'test123', 'test')cur_obj = con_obj.cursor()cur_obj.execute("UPDATE books SET Name = 'Fantastic Beasts' WHERE Id = 1")try: con_obj.commit()except: con_obj.rollback運(yùn)行腳本程序,如下所示。
student@ubuntu:~/work/mysql_testing$ python3 update_data.py現(xiàn)在檢查數(shù)據(jù)庫(kù)中的記錄是否已更新,運(yùn)行retrieve_data.py,如下所示。
student@ubuntu:~/work/mysql_testing$ python3 retrieve_data.py輸出如下。
(1, 'Fantastic Bcasts')(2, 'Lord of the rings')(3, 'Murder on the Orient Express')(4, 'The adventures of Sherlock Holmes')(5, 'Death on the Nile')我們可以看到ID為1的數(shù)據(jù)已更新。上面的示例程序在execute()中給出了一個(gè)update語(yǔ)句,用于更新ID為1的記錄。
5 刪除數(shù)據(jù)
現(xiàn)在我們使用delete語(yǔ)句從表中刪除特定記錄,以下是刪除數(shù)據(jù)的示例程序。創(chuàng)建一個(gè)腳本,命名為delete_data.py,并在其中寫(xiě)入以下代碼。
import MySQLdb as mdbcon_obj = mdb.connect('localhost', 'test_user', 'test123', 'test')cur_obj = con_obj.cursor()cur_obj.execute("DELETE FROM books WHERE Id = 5");try: con_obj.commit()except: con_obj.rollback()運(yùn)行腳本程序,如下所示。
student@ubuntu:~/work/mysql_testing$ python3 delete_data.py現(xiàn)在檢查數(shù)據(jù)庫(kù)中的記錄是否已刪除,運(yùn)行retrieve_data.py,如下所示。
student@ubuntu:~/work/mysql_testing$ python3 retrieve_data.py輸出如下。
(1, 'Fantastic Beasts')(2, 'Lord of the rings')(3, 'Murder on the Orient Express')(4, 'The adventures of Sherlock Holmes')我們可以看到ID為5的記錄已被刪除。上面的示例程序使用delete語(yǔ)句刪除特定記錄,這里刪除了ID為5的記錄。我們還可以使用其他任何字段名稱刪除對(duì)應(yīng)記錄。
本文摘自《寫(xiě)給系統(tǒng)管理員的Python腳本編程指南》,[印度] 甘尼什·桑吉夫·奈克(Ganesh,Sanjiv,Naik) 著,張成悟 譯。
- 從基礎(chǔ)到高級(jí)編程,全面系統(tǒng)地介紹Python腳本在系統(tǒng)管理中的作用。
- 市場(chǎng)上少見(jiàn)的介紹將Python腳本應(yīng)用于系統(tǒng)管理的圖書(shū)。
- 本書(shū)附有配套資源,幫助讀者學(xué)以致用,將所學(xué)應(yīng)用到真實(shí)場(chǎng)景中。
本書(shū)首先介紹Python的安裝,并講解編程基礎(chǔ)知識(shí)。然后,本書(shū)將側(cè)重于解析整個(gè)開(kāi)發(fā)過(guò)程,從配置到準(zhǔn)備再到構(gòu)建 不同的工具,包括IT管理員的日常活動(dòng)(文本處理、正則表達(dá)式、文件存檔和加密)、網(wǎng)絡(luò)管理(套接字編程、電子郵 件處理、使用Telnet/SSH遠(yuǎn)程控制設(shè)備以及SNMP/DHCP等協(xié)議)、創(chuàng)建圖形用戶界面、網(wǎng)站處理(Apache日志 文件處理、SOAP和REST API通信、Web抓取),以及數(shù)據(jù)庫(kù)管理(MySQL和相似數(shù)據(jù)庫(kù)數(shù)據(jù)管理、數(shù)據(jù)分析和報(bào)告)。學(xué)完本書(shū),讀者將能夠使用Python功能構(gòu)建強(qiáng)大的工具以解決具有挑戰(zhàn)性的實(shí)際任務(wù)。
讀者將學(xué)到以下內(nèi)容:
■ 了解如何安裝Python和調(diào)試Python腳本;
■ 了解和編寫(xiě)用于自動(dòng)化測(cè)試和日常管理活動(dòng)的腳本;
■ 了解如何編寫(xiě)用于文本處理、加密、解密和歸檔的腳本;
■ 處理PDF、Excel、CSV和文本文件,并生成報(bào)告;
■ 編寫(xiě)用于遠(yuǎn)程網(wǎng)絡(luò)管理(包括處理電子郵件)的腳本;
■ 使用圖形用戶界面構(gòu)建交互式工具;
■ 處理Apache日志文件,以及SOAP和REST API的通信;
■ 自動(dòng)化數(shù)據(jù)庫(kù)管理并執(zhí)行統(tǒng)計(jì)分析。
總結(jié)
以上是生活随笔為你收集整理的ubuntu中mysql怎么退出命令_如何使用Python进行MySQL数据库管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 转化百分比_小秘诀教你如何快速提升大众点
- 下一篇: centos7 没有pip命令_Linu