用linux上 用python對mysql進行鏈接 前提遇到的問題記錄: 在linux上安裝 (base) [sqoop@flink-slave5 bin]$ ./pip install mysql Looking in indexes: http://pypi.douban.com/simple Collecting mysql Downloading http://pypi.doubanio.com/packages/bf/5f/b574ac9f70811df0540e403309f349a8b9fa1a25d3653824c32e52cc1f28/mysql-0.0.2.tar.gz (1.9 kB) Collecting mysqlclient Downloading http://pypi.doubanio.com/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85 kB) |████████████████████████████████| 85 kB 4.0 MB/s ERROR: Command errored out with exit status 1: command: /data/python_test/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-s6ioj0n3/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-s6ioj0n3/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-s6ioj0n3/mysqlclient/pip-egg-info cwd: /tmp/pip-install-s6ioj0n3/mysqlclient/ Complete output (12 lines): /bin/sh: mysql_config: command not found /bin/sh: mariadb_config: command not found /bin/sh: mysql_config: command not found Traceback (most recent call last): File "<string>", line 1, in <module> File "/tmp/pip-install-s6ioj0n3/mysqlclient/setup.py", line 16, in <module> metadata, options = get_config() File "/tmp/pip-install-s6ioj0n3/mysqlclient/setup_posix.py", line 61, in get_config libs = mysql_config("libs") File "/tmp/pip-install-s6ioj0n3/mysqlclient/setup_posix.py", line 29, in mysql_config raise EnvironmentError("%s not found" % (_mysql_config_path,)) OSError: mysql_config not found ---------------------------------------- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output. 解決方法: [root@flink-slave5 ~]# yum install mysql-devel gcc gcc-devel python-devel 參考連接 https://www.jianshu.com/p/5b6deb15bd21 (base) [sqoop@flink-slave5 bin]$ pwd /data/python_test/bin (base) [sqoop@flink-slave5 bin]$ ./pip install mysql Looking in indexes: http://pypi.douban.com/simple Collecting mysql Downloading http://pypi.doubanio.com/packages/bf/5f/b574ac9f70811df0540e403309f349a8b9fa1a25d3653824c32e52cc1f28/mysql-0.0.2.tar.gz (1.9 kB) Collecting mysqlclient Downloading http://pypi.doubanio.com/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85 kB) |████████████████████████████████| 85 kB 14.8 MB/s Building wheels for collected packages: mysql, mysqlclient Building wheel for mysql (setup.py) ... done Created wheel for mysql: filename=mysql-0.0.2-py3-none-any.whl size=1243 sha256=81bb94d982e366e617bf2dd96df9735009fea2ad983b60198d054773df25dde2 Stored in directory: /home/sqoop/.cache/pip/wheels/42/09/8b/d90224fd8b5636800fa21b9ede51f208bb1781bf7f52b89085 Building wheel for mysqlclient (setup.py) ... done Created wheel for mysqlclient: filename=mysqlclient-1.4.6-cp37-cp37m-linux_x86_64.whl size=59120 sha256=1d83233315a664a264e51acb1b0288979b0f621da6c15a0fc3688e5f1c09fda4 Stored in directory: /home/sqoop/.cache/pip/wheels/f9/8e/98/19b6ee3ca946f2f107a41bc88a15c3844d406ceeae32446784 Successfully built mysql mysqlclient Installing collected packages: mysqlclient, mysql Successfully installed mysql-0.0.2 mysqlclient-1.4.6 而後執行: (base) [sqoop@flink-slave5 bin]$ ./pip install mysql-connector ... ... ... 在linux環境上測試成功 (base) [sqoop@flink-slave5 majihui_test]$ /data/python_test/bin/python python_connect_mysql.py MySQL版本: 8.0.13 (base) [sqoop@flink-slave5 majihui_test]$ pwd /data/majihui_test (base) [sqoop@flink-slave5 majihui_test]$ cat python_connect_mysql.py # -*- coding: UTF-8 -*- import mysql.connector # 打開數據庫鏈接 db = mysql.connector.connect( host="10.9.36.253", user="rpt", passwd="Rpt1234!", # 寫上你的數據庫密碼 database='rpt', auth_plugin='mysql_native_password' ) # 獲取操做遊標 cursor = db.cursor() # 執行SQL語句 cursor.execute("SELECT VERSION()") # 獲取一條數據 data = cursor.fetchone() print("MySQL版本: %s " % data) # 關閉遊標&數據庫鏈接 cursor.close() db.close() (base) [sqoop@flink-slave5 majihui_test]$ /data/python_test/bin/python python_connect_mysql.py MySQL版本: 8.0.13 第三: 對數據表進行增刪改查 一、首先咱們給student的表增長一個學生用戶: mysql> select * from student ; +-----------+--------+------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | +-----------+--------+------+------+-------+ | 100215122 | 劉晨 | 女 | 19 | CS | | 100215123 | 王敏 | 女 | 18 | MA | | 200215121 | 李勇 | 女 | 20 | CS | +-----------+--------+------+------+-------+ 3 rows in set (0.00 sec) # 插入學生代碼 sql = "INSERT INTO student (Sno, Sname, Ssex,Sage,Sdept) VALUES (%s, %s, %s, %s, %s)" val = (200215122, "馬吉輝", "男", 27, CS) cursor.execute(sql, val) db.commit() print(cursor.rowcount, "記錄插入成功。") 完整代碼以下: 執行了2次,插入了2次數據 (base) [sqoop@flink-slave5 majihui_test]$ cat python_connect_mysql_insert.py # -*- coding: UTF-8 -*- import mysql.connector # 打開數據庫鏈接 db = mysql.connector.connect( host="10.9.36.253", user="rpt", passwd="Rpt1234!", # 寫上你的數據庫密碼 database='rpt', auth_plugin='mysql_native_password' ) # 獲取操做遊標 cursor = db.cursor() # 插入學生代碼 sql = "INSERT INTO student (Sno, Sname, Ssex,Sage,Sdept) VALUES (%s, %s, %s, %s, %s)" val = (200215123, "王金燦", "男", 30, "CS") # 執行SQL語句 cursor.execute(sql, val) # 提交sql語句對事務進行提交 db.commit() # 打印結果 # 使用cursor.rowcount,返回查詢結果集中的行數。若是沒有查詢到數據或者尚未查詢,則結果爲 -1,不然會返回查詢獲得的數據行數; print(cursor.rowcount, "記錄插入成功。") # 關閉遊標&數據庫鏈接 cursor.close() db.close() (base) [sqoop@flink-slave5 majihui_test]$ /data/python_test/bin/python python_connect_mysql_insert.py 1 記錄插入成功。 mysql> select * from student ; +-----------+-----------+------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | +-----------+-----------+------+------+-------+ | 100215122 | 劉晨 | 女 | 19 | CS | | 100215123 | 王敏 | 女 | 18 | MA | | 200215121 | 李勇 | 女 | 20 | CS | | 200215122 | 馬吉輝 | 男 | 27 | CS | | 200215123 | 王金燦 | 男 | 30 | CS | +-----------+-----------+------+------+-------+ 5 rows in set (0.00 sec) 代碼解釋: 咱們使用 cursor.execute 來執行相應的 SQL 語句, val 爲 SQL 語句中的參數,SQL 執行後使用 db.commit() 進行提交。 須要說明的是,咱們在使用 SQL 語句的時候,能夠向 SQL 語句傳遞參數, 這時 SQL 語句裏要統一用(%s)進行佔位,不然就會報錯。 不論插入的數值爲整數類型,仍是浮點類型,都須要統一用(%s)進行佔位。 另外在用遊標進行 SQL 操做以後,還須要使用 db.commit() 進行提交,不然數據不會被插入。 小結: 上面這段代碼中有兩個重要的對象你須要瞭解下,分別是 Connection 和 Cursor。 Connection 就是對數據庫的當前鏈接進行管理,咱們能夠經過它來進行如下操做: 一、經過指定 host、user、passwd 和 port 等參數來建立數據庫鏈接,這些參數分別對應着數據庫 IP 地址、用戶名、密碼和端口號; 二、使用 db.close() 關閉數據庫鏈接; 三、使用 db.cursor() 建立遊標,操做數據庫中的數據; 四、使用 db.begin() 開啓事務; 五、使用 db.commit() 和 db.rollback(),對事務進行提交以及回滾。 當咱們經過cursor = db.cursor()建立遊標後,就能夠經過面向過程的編程方式對數據庫中的數據進行操做: 一、使用cursor.execute(query_sql),執行數據庫查詢; 二、使用cursor.fetchone(),讀取數據集中的一條數據; 三、使用cursor.fetchall(),取出數據集中的全部行,返回一個元組 tuples 類型; 四、使用cursor.fetchmany(n),取出數據集中的多條數據,一樣返回一個元組 tuples; 五、使用cursor.rowcount,返回查詢結果集中的行數。若是沒有查詢到數據或者尚未查詢,則結果爲 -1,不然會返回查詢獲得的數據行數; 六、使用cursor.close(),關閉遊標。 二、# 查詢年齡大於20的 sql = 'SELECT * FROM student WHERE Sage>=20' cursor.execute(sql) data = cursor.fetchall() for each_player in data: print(each_player) 完整代碼以下: (base) [sqoop@flink-slave5 majihui_test]$ /data/python_test/bin/python python_connect_mysql_select.py ('200215121', '李勇', '女', 20, 'CS') ('200215122', '馬吉輝', '男', 27, 'CS') ('200215123', '王金燦', '男', 30, 'CS') (base) [sqoop@flink-slave5 majihui_test]$ cat python_connect_mysql_select.py # -*- coding: UTF-8 -*- import mysql.connector # 打開數據庫鏈接 db = mysql.connector.connect( host="10.9.36.253", user="rpt", passwd="Rpt1234!", # 寫上你的數據庫密碼 database='rpt', auth_plugin='mysql_native_password' ) # 獲取操做遊標 cursor = db.cursor() # 查詢年齡大於20的 sql = 'SELECT * FROM student WHERE Sage>=20' cursor.execute(sql) data = cursor.fetchall() for each_student in data: print(each_student) # 關閉遊標&數據庫鏈接 cursor.close() db.close() (base) [sqoop@flink-slave5 majihui_test]$ /data/python_test/bin/python python_connect_mysql_select.py ('200215121', '李勇', '女', 20, 'CS') ('200215122', '馬吉輝', '男', 27, 'CS') ('200215123', '王金燦', '男', 30, 'CS') ---- 三、如何修改數據呢? # 修改馬吉輝的CS 修改爲SS sql = 'UPDATE student SET Sdept = %s WHERE Sname = %s' val = ("SS", "馬吉輝") cursor.execute(sql, val) db.commit() print(cursor.rowcount, "記錄被修改。") 完整代碼以下: (base) [sqoop@flink-slave5 majihui_test]$ cat python_connect_mysql_update.py # -*- coding: UTF-8 -*- import mysql.connector # 打開數據庫鏈接 db = mysql.connector.connect( host="10.9.36.253", user="rpt", passwd="Rpt1234!", # 寫上你的數據庫密碼 database='rpt', auth_plugin='mysql_native_password' ) # 獲取操做遊標 cursor = db.cursor() sql = 'UPDATE student SET Sdept = %s WHERE Sname = %s' val = ("SS", "馬吉輝") cursor.execute(sql, val) db.commit() print(cursor.rowcount, "記錄被修改。") # 關閉遊標&數據庫鏈接 cursor.close() db.close() (base) [sqoop@flink-slave5 majihui_test]$ /data/python_test/bin/python python_connect_mysql_update.py 1 記錄被修改。 mysql> select * from student where Sname = "馬吉輝"; +-----------+-----------+------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | +-----------+-----------+------+------+-------+ | 200215122 | 馬吉輝 | 男 | 27 | SS | +-----------+-----------+------+------+-------+ 1 row in set (0.00 sec) --------- 四、最後咱們看下如何刪除王金燦這個同窗的數據: # 刪除王金燦這個同窗的數據 sql = 'DELETE FROM student WHERE Sname = %s' val = ("王金燦",) cursor.execute(sql, val) db.commit() print(cursor.rowcount, "記錄刪除成功。") 完整代碼以下: (base) [sqoop@flink-slave5 majihui_test]$ cat python_connect_mysql_delete.py # -*- coding: UTF-8 -*- import mysql.connector # 打開數據庫鏈接 db = mysql.connector.connect( host="10.9.36.253", user="rpt", passwd="Rpt1234!", # 寫上你的數據庫密碼 database='rpt', auth_plugin='mysql_native_password' ) # 獲取操做遊標 cursor = db.cursor() # 刪除王金燦這個同窗的數據 sql = 'DELETE FROM student WHERE Sname = %s' val = ("王金燦",) cursor.execute(sql, val) db.commit() print(cursor.rowcount, "記錄刪除成功。") # 關閉遊標&數據庫鏈接 cursor.close() db.close() (base) [sqoop@flink-slave5 majihui_test]$ /data/python_test/bin/python python_connect_mysql_delete.py 1 記錄刪除成功。 mysql> select * from student ; +-----------+-----------+------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | +-----------+-----------+------+------+-------+ | 100215122 | 劉晨 | 女 | 19 | CS | | 100215123 | 王敏 | 女 | 18 | MA | | 200215121 | 李勇 | 女 | 20 | CS | | 200215122 | 馬吉輝 | 男 | 27 | SS | +-----------+-----------+------+------+-------+ 4 rows in set (0.00 sec) 第四: 針對上面的操做過程,你能夠模擬下數據的 CRUD 操做,但有幾點你須要注意。 1. 打開數據庫鏈接之後,若是再也不使用,則須要關閉數據庫鏈接,以避免形成資源浪費。 2. 在對數據進行增長、刪除和修改的時候,可能會出現異常,這時就須要用try...except捕獲異常信息。 三、好比針對插入同窗王金燦這個操做,你能夠寫成下面這樣: 第五: 加入python異常完整代碼以下: (base) [sqoop@flink-slave5 majihui_test]$ cat python_connect_mysql_insert_traceback.py # -*- coding: UTF-8 -*- import mysql.connector import traceback # 打開數據庫鏈接 db = mysql.connector.connect( host="10.9.36.253", user="rpt", passwd="Rpt1234!", # 寫上你的數據庫密碼 database='rpt', auth_plugin='mysql_native_password' ) # 獲取操做遊標 cursor = db.cursor() # 插入學生代碼 try: sql = "INSERT INTO student (Sno, Sname, Ssex,Sage,Sdept) VALUES (%s, %s, %s, %s, %s)" val = (200215123, "王金燦", "男", 30, "CS") # 執行SQL語句 cursor.execute(sql, val) # 提交sql語句對事務進行提交 db.commit() # 打印結果 # 使用cursor.rowcount,返回查詢結果集中的行數。若是沒有查詢到數據或者尚未查詢,則結果爲 -1,不然會返回查詢獲得的數據行數; print(cursor.rowcount, "記錄插入成功。") except Exception as e: # 打印異常信息 traceback.print_exc() # 回滾 db.rollback() finally: # 關閉遊標&數據庫鏈接 cursor.close() db.close() (base) [sqoop@flink-slave5 majihui_test]$ /data/python_test/bin/python python_connect_mysql_insert_traceback.py 1 記錄插入成功。 mysql> select * from student ; +-----------+-----------+------+------+-------+ | Sno | Sname | Ssex | Sage | Sdept | +-----------+-----------+------+------+-------+ | 100215122 | 劉晨 | 女 | 19 | CS | | 100215123 | 王敏 | 女 | 18 | MA | | 200215121 | 李勇 | 女 | 20 | CS | | 200215122 | 馬吉輝 | 男 | 27 | SS | | 200215123 | 王金燦 | 男 | 30 | CS | +-----------+-----------+------+------+-------+ 5 rows in set (0.00 sec) 再執行一次,就會報錯: (base) [sqoop@flink-slave5 majihui_test]$ /data/python_test/bin/python python_connect_mysql_insert_traceback.py Traceback (most recent call last): File "python_connect_mysql_insert_traceback.py", line 20, in <module> cursor.execute(sql, val) File "/data/python_test/lib/python3.7/site-packages/mysql/connector/cursor.py", line 551, in execute self._handle_result(self._connection.cmd_query(stmt)) File "/data/python_test/lib/python3.7/site-packages/mysql/connector/connection.py", line 490, in cmd_query result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) File "/data/python_test/lib/python3.7/site-packages/mysql/connector/connection.py", line 395, in _handle_result raise errors.get_exception(packet) mysql.connector.errors.IntegrityError: 1062 (23000): Duplicate entry '200215123' for key 'PRIMARY' 第八: 咱們在python代碼中寫了 mysql的帳號和密碼如何 # 建議吧數據庫連接信息寫到配置文件裏,防止密碼泄露。 import json import traceback import mysql.connector # 讀取數據庫連接配置文件 with open('mysql.json', encoding='utf-8') as con_json: con_dict = json.load(con_json) # 打開數據庫連接 db = mysql.connector.connect( host=con_dict['host'], user=con_dict['user'], passwd=con_dict['passwd'], database=con_dict['database'], auth_plugin=con_dict['auth_plugin'], ) # 獲取操做遊標 cursor = db.cursor() try: sql = 'SELECT id, name, hp_max FROM heros WHERE hp_max>6000' cursor.execute(sql) data = cursor.fetchall() print(cursor.rowcount, '查詢成功。') for each_hero in data: print(each_hero) except Exception as e: # 打印異常信息 traceback.print_exc() finally: cursor.close() db.close() # 建議吧數據庫連接信息寫到配置文件裏,防止密碼泄露。