Hướng dẫn which connector connects mysql to python? - trình kết nối nào kết nối mysql với python?

Trình xây dựng connect() tạo kết nối với máy chủ MySQL và trả về đối tượng

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
0.

Ví dụ sau đây cho thấy cách kết nối với máy chủ MySQL:

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees')
cnx.close()

Phần & NBSP; 7.1, Trình kết nối/Đối số kết nối Python mô tả các đối số kết nối được phép.

Cũng có thể tạo các đối tượng kết nối bằng lớp Connection.MysQlConnection ():

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()

Cả hai hình thức (hoặc sử dụng hàm tạo connect() hoặc lớp trực tiếp) đều hợp lệ và bằng nhau về mặt chức năng, nhưng sử dụng connect() được ưa thích và sử dụng bởi hầu hết các ví dụ trong hướng dẫn này.

Để xử lý các lỗi kết nối, hãy sử dụng câu lệnh

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
3 và bắt tất cả các lỗi bằng cách sử dụng lỗi.

import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()

Xác định các đối số kết nối trong từ điển và sử dụng toán tử

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
4 là một tùy chọn khác:

import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()

Sử dụng phần mở rộng đầu nối/Python Python hoặc C

Trình kết nối/Python cung cấp hai triển khai: Giao diện Python thuần túy và tiện ích mở rộng C sử dụng thư viện máy khách MySQL C (xem Chương & NBSP; 8, Phần mở rộng Trình kết nối/Python C). Điều này có thể được cấu hình trong thời gian chạy bằng đối số kết nối

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
5. Nó mặc định là
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6 kể từ MySQL 8, có nghĩa là tiện ích mở rộng C được sử dụng. Nếu tiện ích mở rộng C không có sẵn trên hệ thống thì
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
5 mặc định là
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8. Cài đặt
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
9 khiến kết nối sử dụng tiện ích mở rộng C nếu cài đặt đầu nối/Python của bạn bao gồm nó, trong khi
import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
0 đến
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6 có nghĩa là việc triển khai Python được sử dụng nếu có.

Ghi chú

Tùy chọn

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
5 và phần mở rộng C đã được thêm vào đầu nối/Python 2.1.1.

Ví dụ sau đây cho thấy cách đặt

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
5 thành sai.

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()

Cũng có thể sử dụng tiện ích mở rộng C trực tiếp bằng cách nhập mô -đun

import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
4 thay vì mô -đun
import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
5. Để biết thêm thông tin, hãy xem Phần & NBSP; 8.2, Mô -đun mở rộng _MysQL_Connector C.

Một kết nối với máy chủ MySQL có thể được thiết lập bằng hàm

import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
6 hoặc lớp
import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
7:

cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')

Bảng sau đây mô tả các đối số có thể được sử dụng để bắt đầu kết nối. Dấu hoa thị (*) theo một đối số cho biết tên đối số đồng nghĩa, chỉ có sẵn để tương thích với các trình điều khiển Python MySQL khác. Oracle khuyến nghị không sử dụng các tên thay thế này.

Bảng & nbsp; 7.1 & nbsp; đối số kết nối cho đầu nối/python

Tên đối sốMặc địnhSự mô tả
import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
8 (________ 29*)
Tên người dùng được sử dụng để xác thực với máy chủ MySQL.
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
0 (________ 31*)
Mật khẩu để xác thực người dùng với máy chủ MySQL.
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
2,
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
3 và
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
4
Cho xác thực đa yếu tố (MFA);
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
2 là bí danh cho
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
0. Được thêm vào trong 8.0,28.
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
7 (________ 38*)
Tên cơ sở dữ liệu để sử dụng khi kết nối với máy chủ MySQL.
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
9
127.0.0.1 Tên máy chủ hoặc địa chỉ IP của máy chủ MySQL.
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
0
Vị trí của tệp ổ cắm UNIX.
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
1
3306 Cổng TCP/IP của máy chủ MySQL. Phải là số nguyên.
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
2

Giá trị

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
3 tiêu chuẩn được gửi; Sử dụng
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
2 để tùy chọn đặt các thuộc tính kết nối tùy chỉnh bổ sung như được xác định bởi một từ điển, chẳng hạn như config ['Conn_attrs'] = {"foo": "Bar"}.config['conn_attrs'] = {"foo": "bar"}.

Các triển khai Python C-Ext và Pure Python khác nhau. Việc triển khai C-ext phụ thuộc vào thư viện MySQLClient để các giá trị Conn_attrs tiêu chuẩn của nó bắt nguồn từ nó. Ví dụ: '_client_name' là 'libmysql' với c-ext nhưng 'mysql-connector-python' với python thuần túy. C-ext thêm các thuộc tính bổ sung này: '_connector_version', '_connector_license', '_connector_name' và '_source_host'.

Tùy chọn này đã được thêm vào trong 8.0.17, như là hành vi phiên bản mặc định_connect_attrs.

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
5
Lệnh (truy vấn SQL) được thực hiện ngay sau khi kết nối được thiết lập như một phần của quá trình khởi tạo. Được thêm vào trong 8.0.32.
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
6
Plugin xác thực để sử dụng. Được thêm vào 1.2.1.
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
7

Một cuộc gọi được xác định bởi tùy chọn

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
7 tùy chọn được thực thi khi nó sẵn sàng cho tương tác người dùng với thiết bị FIDO phần cứng. Tùy chọn này có thể là một đối tượng có thể gọi được hoặc đường dẫn chuỗi mà trình kết nối có thể nhập trong thời gian chạy và thực thi. Nó không chặn và chỉ được sử dụng để thông báo cho người dùng về nhu cầu tương tác với thiết bị FIDO phần cứng.

Chức năng này chỉ có sẵn trong phần mở rộng C. Một NotSupportedError được nâng lên khi sử dụng triển khai Python thuần túy.NotSupportedError is raised when using the pure Python implementation.

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
9
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8
Có sử dụng unicode không.
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
1
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
2
Mà nhân vật MySQL được thiết lập để sử dụng.
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
3
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
4 (là
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
5 trong 2.x
Mà tôi sử dụng đối chiếu MySQL. Các giá trị mặc định 8.x được tạo từ mặc định MySQL Server 8.0 mới nhất.
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
6
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Có giao dịch tự động.
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
8
Đặt biến phiên
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
8 tại thời điểm kết nối.
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
0
Đặt biến phiên
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
0 tại thời điểm kết nối.
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
2
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Có tìm kiếm cảnh báo.
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
4
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Có nên nêu một ngoại lệ về cảnh báo.
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
6 (________ 67*)
Thời gian chờ cho các kết nối ổ cắm TCP và UNIX.
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
8
Cờ khách hàng MySQL.
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
9
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Liệu các đối tượng con trỏ có lấy kết quả ngay sau khi thực hiện các truy vấn hay không.
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
1
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Cho dù kết quả MySQL được trả về như là, thay vì chuyển đổi thành các loại Python.
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
3
SaiCó tự động đọc các bộ kết quả.
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
4
["TLSV1.2", "TLSV1.3"]]Phiên bản TLS để hỗ trợ; Các phiên bản được phép là TLSV1.2 và TLSV1.3. Các phiên bản TLSV1 và TLSV1.1 đã được xóa trong đầu nối/Python 8.0.28.
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
5
Tệp chứa cơ quan chứng chỉ SSL.
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
6
Tệp chứa tệp chứng chỉ SSL.
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
7
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8 Tắt việc sử dụng SSL/TLS. Các giao thức kết nối TLSV1 và TLSV1.1 được không dùng nữa là đầu nối/Python 8.0.26 và được loại bỏ như đầu nối/Python 8.0.28.
# Note (Example is valid for Python v2 and v3)
from __future__ import print_function

import sys

#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))

import mysql.connector
from mysql.connector.constants import ClientFlag

config = {
    'user': 'ssluser',
    'password': 'password',
    'host': '127.0.0.1',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': '/opt/mysql/ssl/ca.pem',
    'ssl_cert': '/opt/mysql/ssl/client-cert.pem',
    'ssl_key': '/opt/mysql/ssl/client-key.pem',
}

cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()
0
Tệp chứa khóa SSL.
# Note (Example is valid for Python v2 and v3)
from __future__ import print_function

import sys

#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))

import mysql.connector
from mysql.connector.constants import ClientFlag

config = {
    'user': 'ssluser',
    'password': 'password',
    'host': '127.0.0.1',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': '/opt/mysql/ssl/ca.pem',
    'ssl_cert': '/opt/mysql/ssl/client-cert.pem',
    'ssl_key': '/opt/mysql/ssl/client-key.pem',
}

cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()
1
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Khi được đặt thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8, hãy kiểm tra chứng chỉ máy chủ so với tệp chứng chỉ được chỉ định bởi tùy chọn
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
5. Bất kỳ sự không phù hợp nào cũng gây ra ngoại lệ
# Note (Example is valid for Python v2 and v3)
from __future__ import print_function

import sys

#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))

import mysql.connector
from mysql.connector.constants import ClientFlag

config = {
    'user': 'ssluser',
    'password': 'password',
    'host': '127.0.0.1',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': '/opt/mysql/ssl/ca.pem',
    'ssl_cert': '/opt/mysql/ssl/client-cert.pem',
    'ssl_key': '/opt/mysql/ssl/client-key.pem',
}

cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()
5.
# Note (Example is valid for Python v2 and v3)
from __future__ import print_function

import sys

#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))

import mysql.connector
from mysql.connector.constants import ClientFlag

config = {
    'user': 'ssluser',
    'password': 'password',
    'host': '127.0.0.1',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': '/opt/mysql/ssl/ca.pem',
    'ssl_cert': '/opt/mysql/ssl/client-cert.pem',
    'ssl_key': '/opt/mysql/ssl/client-key.pem',
}

cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()
6
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Khi được đặt thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8, hãy thực hiện thêm xác minh nhận dạng tên máy chủ bằng cách kiểm tra tên máy chủ mà máy khách sử dụng để kết nối với máy chủ với danh tính trong chứng chỉ mà máy chủ gửi cho máy khách. Tùy chọn được thêm vào đầu nối/Python 8.0.14.
# Note (Example is valid for Python v2 and v3)
from __future__ import print_function

import sys

#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))

import mysql.connector
from mysql.connector.constants import ClientFlag

config = {
    'user': 'ssluser',
    'password': 'password',
    'host': '127.0.0.1',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': '/opt/mysql/ssl/ca.pem',
    'ssl_cert': '/opt/mysql/ssl/client-cert.pem',
    'ssl_key': '/opt/mysql/ssl/client-key.pem',
}

cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()
9
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Khi được đặt thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8, sử dụng IPv6 khi địa chỉ giải quyết được cả IPv4 và IPv6. Theo mặc định, IPv4 được sử dụng trong các trường hợp như vậy.
connect()2connect()3

Tùy chọn xác định một đường dẫn cụ thể đến tệp cấu hình xác thực phía máy chủ connect()4.

connect()5 Không được hỗ trợ (tăng connect()6 khi sử dụng).
connect()7 Tên nhóm kết nối. Tên nhóm được giới hạn ở các ký tự chữ và số và các ký tự đặc biệt connect()8, connect()9,
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
00,
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
01 và
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
02. Tên nhóm phải dài không quá
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
03 ký tự (mặc định 64).
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
04
5 Kích thước nhóm kết nối. Kích thước nhóm phải lớn hơn 0 và nhỏ hơn hoặc bằng
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
05 (mặc định 32).
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
06
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8
Có đặt lại các biến phiên khi kết nối được trả về nhóm.
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
08
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Có sử dụng giao thức máy khách/máy chủ nén hay không.
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
10
Lớp chuyển đổi để sử dụng.
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
11
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6
Bật chuyển đổi thành STR của các loại giá trị không được hỗ trợ bởi lớp chuyển đổi đầu nối/Python hoặc bởi một lớp chuyển đổi tùy chỉnh.
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
13
Chuỗi chuyển đổi dự phòng máy chủ.
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
14
Tùy chọn nào các tập tin để đọc. Được thêm vào trong 2.0.0.
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
15
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
16
Nhóm nào để đọc từ các tệp tùy chọn. Được thêm vào trong 2.0.0.
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
17
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8
Có đặt lại các biến phiên khi kết nối được trả về nhóm.
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
5
Có sử dụng giao thức máy khách/máy chủ nén hay không.Lớp chuyển đổi để sử dụng.mysql.connector.connect() but not MySQLConnection.connect(). Added in 2.1.1.
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
24
Bật chuyển đổi thành STR của các loại giá trị không được hỗ trợ bởi lớp chuyển đổi đầu nối/Python hoặc bởi một lớp chuyển đổi tùy chỉnh.Chuỗi chuyển đổi dự phòng máy chủ.


Tùy chọn nào các tập tin để đọc. Được thêm vào trong 2.0.0.

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
16

Nhóm nào để đọc từ các tệp tùy chọn. Được thêm vào trong 2.0.0.

Có cho phép

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
19 không. Được thêm vào trong 2.0.0.

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6 kể từ 8.0.11 và
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8 trong các phiên bản trước. Nếu chỉ có một triển khai (C hoặc Python) có sẵn, thì giá trị mặc định được đặt để cho phép triển khai có sẵn.

Phương thức connect() hỗ trợ đối số

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
6 có thể được sử dụng để buộc sử dụng một plugin cụ thể. Ví dụ: nếu máy chủ được cấu hình để sử dụng
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
36 theo mặc định và bạn muốn kết nối với tài khoản xác thực bằng cách sử dụng
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
41, kết nối bằng SSL hoặc chỉ định
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
42.

Ghi chú

Trình kết nối MySQL/Python không hỗ trợ các giao thức mật khẩu cũ, kém an toàn của các phiên bản MySQL trước 4.1.

Trình kết nối/Python hỗ trợ giao thức xác thực Kerberos để xác thực không có mật khẩu. Các máy khách Linux được hỗ trợ kể từ Trình kết nối/Python 8.0.26 và hỗ trợ Windows đã được thêm vào đầu nối/Python 8.0.27 với triển khai mở rộng C và trong Trình kết nối/Python 8.0.29 với việc triển khai Python thuần túy.

Trình kết nối/Python hỗ trợ xác thực đa yếu tố (MFA) kể từ V8.0.28 bằng cách sử dụng

import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
2 (bí danh của
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
0),
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
3 và các tùy chọn kết nối
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
4.

Trình kết nối/Python hỗ trợ xác thực có thể cắm FIDO như Trình kết nối/Python 8.0,29; Một cơ chế xác thực được thêm vào trong MySQL Enterprise Edition 8.0.27.

Hạn chế: Chức năng xác thực FIDO chỉ có sẵn trong triển khai mở rộng C (được cài đặt theo mặc định); Một NotSupportedError được nâng lên khi sử dụng triển khai Python thuần túy của đầu nối này. Ngoài ra, chỉ có cấu trúc 2 cấp được hỗ trợ, trong đó đầu nối và phím vật lý nằm trên cùng một máy.NotSupportedError is raised when using the pure Python implementation of this connector. Also only a 2-level structure is supported, in that the connector and the physical key are on the same machine.

Xem Xác thực có thể cắm FIDO để biết chi tiết cài đặt và tùy chọn sử dụng tùy chọn Kết nối đầu nối/Python Fido_Callback để thông báo cho người dùng rằng họ cần chạm vào thiết bị phần cứng.

Mã hóa ký tự

Theo mặc định, các chuỗi đến từ MySQL được trả lại dưới dạng chữ Python Unicode. Để thay đổi hành vi này, đặt

import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
9 thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6. Bạn có thể thay đổi cài đặt ký tự cho kết nối máy khách thông qua đối số
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
1. Để thay đổi bộ ký tự sau khi kết nối với MySQL, hãy đặt thuộc tính
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
1 của thể hiện
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
0. Kỹ thuật này được ưu tiên hơn bằng cách sử dụng câu lệnh
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
52 SQL trực tiếp. Tương tự như thuộc tính
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
1, bạn có thể đặt
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
3 cho phiên MySQL hiện tại.

Giao dịch

Giá trị

cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
6 mặc định là
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6, vì vậy các giao dịch không tự động cam kết. Gọi phương thức
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
57 của thể hiện
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
0 trong ứng dụng của bạn sau khi thực hiện một tập hợp các hoạt động chèn, cập nhật và xóa liên quan. Đối với tính nhất quán của dữ liệu và thông lượng cao cho các hoạt động ghi, tốt nhất bạn nên tắt tùy chọn cấu hình
cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
6 khi sử dụng
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
60 hoặc các bảng giao dịch khác.

Múi giờ

Múi giờ có thể được đặt trên mỗi kết nối bằng đối số

cnx = mysql.connector.connect(user='joe', database='test')
cnx = MySQLConnection(user='joe', database='test')
8. Điều này rất hữu ích, ví dụ, nếu máy chủ MySQL được đặt thành các giá trị UTC và
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
62 sẽ được trả về bởi MYSQL được chuyển đổi thành múi giờ
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
63.

Chế độ SQL

MySQL hỗ trợ cái gọi là chế độ SQL. thay đổi hành vi của máy chủ trên toàn cầu hoặc trên mỗi kết nối. Ví dụ, để có các cảnh báo được nêu là lỗi, đặt

from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
0 thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
65. Để biết thêm thông tin, hãy xem các chế độ SQL máy chủ.

Khắc phục sự cố và xử lý lỗi

Các cảnh báo được tạo bởi các truy vấn được tìm nạp tự động khi

from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
2 được đặt thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8. Bạn cũng có thể ngay lập tức nâng một ngoại lệ bằng cách đặt
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
4 thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8. Cân nhắc sử dụng cài đặt MySQL SQL_Mode để biến cảnh báo thành lỗi.

Để đặt giá trị thời gian chờ cho các kết nối, hãy sử dụng

from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
6.

Bật và vô hiệu hóa các tính năng bằng cờ máy khách

MySQL sử dụng cờ máy khách để bật hoặc tắt các tính năng. Sử dụng đối số

from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
8, bạn có quyền kiểm soát những gì được đặt. Để tìm hiểu những lá cờ có sẵn, hãy sử dụng như sau:

from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())

Nếu

from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
8 không được chỉ định (nghĩa là bằng không), mặc định được sử dụng cho MySQL 4.1 trở lên. Nếu bạn chỉ định số nguyên lớn hơn
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
73, hãy đảm bảo tất cả các cờ được đặt đúng. Một cách tốt hơn để đặt và các cờ không đặt riêng là sử dụng danh sách. Ví dụ: để đặt
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
74, nhưng vô hiệu hóa mặc định
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
75:

flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)

Kết quả thiết lập xử lý

Theo mặc định, Trình kết nối/Python MySQL không đệm hoặc kết quả trước. Điều này có nghĩa là sau khi truy vấn được thực thi, chương trình của bạn chịu trách nhiệm tìm nạp dữ liệu. Điều này tránh sử dụng bộ nhớ quá mức khi các truy vấn trả về các bộ kết quả lớn. Nếu bạn biết rằng tập kết quả đủ nhỏ để xử lý tất cả cùng một lúc, bạn có thể tìm nạp kết quả ngay lập tức bằng cách đặt

from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
9 thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8. Cũng có thể đặt điều này trên mỗi con trỏ (xem Phần & NBSP; 10.2.6, MySqlConnection.cursor () phương thức).

Kết quả được tạo bởi các truy vấn thường không được đọc cho đến khi chương trình khách hàng tìm nạp chúng. Để tự động tiêu thụ và loại bỏ các bộ kết quả, hãy đặt tùy chọn

flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
3 thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8. Kết quả là tất cả các kết quả được đọc, mà đối với các bộ kết quả lớn có thể chậm. (Trong trường hợp này, có thể tốt hơn là đóng và mở lại kết nối.)

Nhập chuyển đổi

Theo mặc định, các loại MySQL trong các bộ kết quả được chuyển đổi tự động thành các loại Python. Ví dụ: giá trị cột

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
80 trở thành đối tượng DateTime.Datetime. Để vô hiệu hóa chuyển đổi, đặt tùy chọn
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
1 thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8. Bạn có thể làm điều này để có được hiệu suất tốt hơn hoặc tự mình thực hiện các loại chuyển đổi khác nhau.

Kết nối thông qua SSL

Sử dụng kết nối SSL là có thể khi cài đặt Python của bạn hỗ trợ SSL, nghĩa là khi nó được biên dịch với các thư viện OpenSSL. Khi bạn cung cấp các tùy chọn

flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
5,
# Note (Example is valid for Python v2 and v3)
from __future__ import print_function

import sys

#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))

import mysql.connector
from mysql.connector.constants import ClientFlag

config = {
    'user': 'ssluser',
    'password': 'password',
    'host': '127.0.0.1',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': '/opt/mysql/ssl/ca.pem',
    'ssl_cert': '/opt/mysql/ssl/client-cert.pem',
    'ssl_key': '/opt/mysql/ssl/client-key.pem',
}

cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()
0 và
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
6, kết nối sẽ chuyển sang SSL và tùy chọn
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
8 bao gồm giá trị
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
87 tự động. Bạn có thể sử dụng điều này kết hợp với tùy chọn
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
88 được đặt thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8.

Kể từ đầu nối/Python 2.2.2, nếu máy chủ MySQL hỗ trợ các kết nối SSL, Trình kết nối/Python sẽ cố gắng thiết lập kết nối an toàn (được mã hóa) theo mặc định, rơi trở lại kết nối không được mã hóa.

Từ Trình kết nối/Python 1.2.1 đến đầu nối/Python 2.2.1, có thể thiết lập kết nối SSL chỉ sử dụng opion

flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
5. Các đối số
# Note (Example is valid for Python v2 and v3)
from __future__ import print_function

import sys

#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))

import mysql.connector
from mysql.connector.constants import ClientFlag

config = {
    'user': 'ssluser',
    'password': 'password',
    'host': '127.0.0.1',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': '/opt/mysql/ssl/ca.pem',
    'ssl_cert': '/opt/mysql/ssl/client-cert.pem',
    'ssl_key': '/opt/mysql/ssl/client-key.pem',
}

cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()
0 và
flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
mysql.connector.connect(client_flags=flags)
6 là tùy chọn. Tuy nhiên, khi được đưa ra, cả hai phải được đưa ra hoặc
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
93 được nâng lên.

# Note (Example is valid for Python v2 and v3)
from __future__ import print_function

import sys

#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))

import mysql.connector
from mysql.connector.constants import ClientFlag

config = {
    'user': 'ssluser',
    'password': 'password',
    'host': '127.0.0.1',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': '/opt/mysql/ssl/ca.pem',
    'ssl_cert': '/opt/mysql/ssl/client-cert.pem',
    'ssl_key': '/opt/mysql/ssl/client-key.pem',
}

cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()

Kết nối gộp

Với đối số connect()7 hoặc

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
04 hiện tại, Trình kết nối/Python tạo ra nhóm mới. Nếu đối số connect()7 không được đưa ra, cuộc gọi connect() sẽ tự động tạo tên, được tạo từ bất kỳ ai trong số các đối số kết nối
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
9,
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
1,
import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
8 và
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
7 được đưa ra theo thứ tự đó. Nếu đối số
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
04 không được đưa ra, kích thước mặc định là 5 kết nối.

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
06 cho phép kiểm soát xem các biến phiên có được thiết lập lại khi kết nối được trả về nhóm hay không. Mặc định là đặt lại chúng.

Để biết thêm thông tin về gộp kết nối, xem Phần & NBSP; 9.1, Trình kết nối/Kết nối Python gộp kết nối.

Nén giao thức

Đối số Boolean

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
08 cho biết có nên sử dụng giao thức máy khách/máy chủ được nén hay không (mặc định
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6). Điều này cung cấp một sự thay thế dễ dàng hơn để đặt cờ
import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
06. Đối số này có sẵn như của Trình kết nối/Python 1.1.2.

Lớp chuyển đổi

Đối số

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
10 lấy một lớp và đặt nó khi định cấu hình kết nối. Một
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
93 được nâng lên nếu lớp chuyển đổi tùy chỉnh không phải là một lớp con của
import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
09.

Chuyển đổi chuyển đổi máy chủ

Phương thức connect() chấp nhận đối số

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
13 cung cấp thông tin để sử dụng cho chuyển đổi dự phòng máy chủ trong trường hợp lỗi kết nối. Giá trị đối số là một tuple hoặc danh sách từ điển (tuple được ưa thích vì nó không thể). Mỗi từ điển chứa các đối số kết nối cho một máy chủ nhất định trong chuỗi chuyển đổi dự phòng. Giá trị từ điển được phép là:
import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
8,
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
0,
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
9,
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
1,
import mysql.connector

cnx = mysql.connector.connect(user='scott', password='password',
                              host='127.0.0.1',
                              database='employees',
                              use_pure=False)
cnx.close()
0,
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
7, connect()7,
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
04. Tùy chọn chuyển đổi dự phòng này đã được thêm vào đầu nối/Python 1.2.1.

Tùy chọn Hỗ trợ tệp

Kể từ đầu nối/Python 2.0.0, các tệp tùy chọn được hỗ trợ bằng hai tùy chọn cho connect():

  • from mysql.connector import (connection)
    
    cnx = connection.MySQLConnection(user='scott', password='password',
                                     host='127.0.0.1',
                                     database='employees')
    cnx.close()
    14: Các tệp tùy chọn nào cần đọc. Giá trị có thể là tên đường dẫn tệp (một chuỗi) hoặc chuỗi chuỗi tên đường dẫn. Theo mặc định, Trình kết nối/Python không có tệp tùy chọn nào, vì vậy đối số này phải được đưa ra một cách rõ ràng để gây ra các tệp tùy chọn được đọc. Các tập tin được đọc theo thứ tự được chỉ định.

  • from mysql.connector import (connection)
    
    cnx = connection.MySQLConnection(user='scott', password='password',
                                     host='127.0.0.1',
                                     database='employees')
    cnx.close()
    15: Nhóm nào sẽ đọc từ các tệp tùy chọn, nếu các tệp tùy chọn được đọc. Giá trị có thể là một tên nhóm tùy chọn (một chuỗi) hoặc chuỗi các chuỗi tên nhóm. Nếu đối số này không được đưa ra, giá trị mặc định là
    from mysql.connector import (connection)
    
    cnx = connection.MySQLConnection(user='scott', password='password',
                                     host='127.0.0.1',
                                     database='employees')
    cnx.close()
    16 để đọc các nhóm
    import mysql.connector
    from mysql.connector import errorcode
    
    try:
      cnx = mysql.connector.connect(user='scott',
                                    database='employ')
    except mysql.connector.Error as err:
      if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
      elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
      else:
        print(err)
    else:
      cnx.close()
    24 và
    import mysql.connector
    from mysql.connector import errorcode
    
    try:
      cnx = mysql.connector.connect(user='scott',
                                    database='employ')
    except mysql.connector.Error as err:
      if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
      elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
      else:
        print(err)
    else:
      cnx.close()
    25.

Để biết thêm thông tin, xem Phần & NBSP; 7.2, Trình kết nối/hỗ trợ tùy chọn Python.

Tải dữ liệu cục bộ

Trước Trình kết nối/Python 2.0.0, để cho phép sử dụng

import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
26, khách hàng phải đặt cờ
import mysql.connector
from mysql.connector import errorcode

try:
  cnx = mysql.connector.connect(user='scott',
                                database='employ')
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with your user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)
else:
  cnx.close()
27 một cách rõ ràng. Tính đến 2.0.0, cờ này được bật theo mặc định. Để vô hiệu hóa nó, tùy chọn kết nối
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
17 có thể được đặt thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6 tại thời điểm kết nối (mặc định là
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8).

Tương phản với các giao diện kết nối khác

import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
1,
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
8 và
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
7 có giá trị đối với khả năng tương thích với các giao diện MYSQL khác và tương ứng giống như
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
0,
import mysql.connector

config = {
  'user': 'scott',
  'password': 'password',
  'host': '127.0.0.1',
  'database': 'employees',
  'raise_on_warnings': True
}

cnx = mysql.connector.connect(**config)

cnx.close()
7 và
from mysql.connector.constants import ClientFlag
print '\n'.join(ClientFlag.get_full_info())
6. Sau này được ưu tiên. Tên nguồn dữ liệu Cú pháp hoặc connect()5 không được sử dụng; Nếu được chỉ định, nó sẽ tăng ngoại lệ connect()6.

Thực hiện giao thức khách/máy chủ

Trình kết nối/Python có thể sử dụng giao diện Python thuần túy cho MySQL hoặc tiện ích mở rộng C sử dụng thư viện máy khách MySQL C.

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
5 mysql.connector.connect () đối số kết nối xác định cái nào. Mặc định đã thay đổi trong Trình kết nối/Python 8 từ
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8 (sử dụng triển khai Python thuần túy) thành
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6. Cài đặt
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
5 Thay đổi việc thực hiện được sử dụng.mysql.connector.connect() connection argument determines which. The default changed in Connector/Python 8 from
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
8 (use the pure Python implementation) to
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
6. Setting
from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
5 changes the implementation used.

Đối số

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()
5 có sẵn như của Trình kết nối/Python 2.1.1.Để biết thêm thông tin về tiện ích mở rộng C, xem Chương & NBSP; 8, phần mở rộng đầu nối/Python C.