python
Metadata locking has been an exciting adventure for the last couple years in MySQL and MariaDB. Users and applications using only MyISAM tables are learning the joys of locking conflicts between transactions/connections. Users with InnoDB tables who either rely on global autocommit behavior or have explicitly set that globally in their configuration might be surprised to find Some users are seeing more than their fair share of these crop up in some very surprising places due to a particular, pernicious Python behavior:PEP-249 mandates that autocommit"must be initially off". Every time your Python programs/scripts connect to MySQL or MariaDB, a new transaction is started, and, crucially:by default, every time youSELECT
in a Python program, your connection acquires a metadata lock.
These locks are not released until your program ends or explicitly ends the transaction usingCOMMIT
orROLLBACK
.
A long-running monitoring tool or something that may connect, execute a couple simple SELECT statements, and keep its connection for some period of time, can cause havoc if you have other processes that try to executeALTER TABLE
or other DDL on those tables.
MariaDB 10.0 gives us a great plugin to inspect metadata locks, so we can see this behavior in action.
MariaDB> create table test.t1 (id int unsigned not null auto_increment primary key) engine=myisam;Query OK, 0 rows affected (0.02 sec)MariaDB> insert into test.t1 values (),(),();Query OK, 3 rows affected (0.00 sec)Records: 3Duplicates: 0Warnings: 0
$ cat simple.py#!/usr/bin/env pythonimport sys, osimport mysql.connectordb = mysql.connector.Connect(user='root')cursor = db.cursor()cursor.execute("SELECT 1 FROM test.t1")cursor.fetchall()cursor.close()cursor = db.cursor()cursor.execute("SELECT SLEEP(60)")cursor.fetchall()cursor.close()db.close()$ python simple.py
MariaDB> install soname 'metadata_lock_info';Query OK, 0 rows affected (0.00 sec)MariaDB> show processlist;+----+------+-----------------+------+---------+------+------------+------------------+----------+| Id | User | Host| db | Command | Time | State| Info | Progress |+----+------+-----------------+------+---------+------+------------+------------------+----------+|3 | root | localhost | test | Query |0 | init | show processlist |0.000 || 14 | root | localhost:59875 | NULL | Query | 40 | User sleep | SELECT SLEEP(60) |0.000 |+----+------+-----------------+------+---------+------+------------+------------------+----------+2 rows in set (0.00 sec)MariaDB> select * from information_schema.metadata_lock_info;+-----------+-----------------+-----------------+---------------------+--------------+------------+| THREAD_ID | LOCK_MODE | LOCK_DURATION | LOCK_TYPE | TABLE_SCHEMA | TABLE_NAME |+-----------+-----------------+-----------------+---------------------+--------------+------------+|14 | MDL_SHARED_READ | MDL_TRANSACTION | Table metadata lock | test | t1 |+-----------+-----------------+-----------------+---------------------+--------------+------------+1 row in set (0.00 sec)
Here's an excerpt from the general query log, to show what statements this connection has executed since it began:
140624 17:24:4814 Connect root@localhost as anonymous on 14 Query SET NAMES 'utf8' COLLATE 'utf8_general_ci' 14 Query SET @@session.autocommit = OFF 14 Query SELECT 1 FROM test.t1 14 Query SELECT SLEEP(60)
There we can see theSET @@session.autocommit = OFF
statement that disables autocommit. This behavior can be influenced in a few different ways, when usingMySQL Connector/Python:
- Set the
autocommit
connection argument to "True". - Modify the
autocommit
property of the connection after connecting. - Execute
SET AUTOCOMMIT=0
orAUTOCOMMIT=OFF
after connecting.
The facilities available may depend on the specific Python driver you are using. TheMySQLdbdriver, for instance, does not support anautocommit
connection argument.
If you have Python programs that connect to MySQL or MariaDB, make sure you understand their autocommit behavior. It's a good practice, no matter the language or API you're using, to explicitly set the autocommit behavior that your program relies on.
Good luck avoiding those metadata locks!

この記事では、MySQLのAlter Tableステートメントを使用して、列の追加/ドロップ、テーブル/列の名前の変更、列データ型の変更など、テーブルを変更することについて説明します。

記事では、証明書の生成と検証を含むMySQL用のSSL/TLS暗号化の構成について説明します。主な問題は、セルフ署名証明書のセキュリティへの影響を使用することです。[文字カウント:159]

記事では、MySQLで大規模なデータセットを処理するための戦略について説明します。これには、パーティション化、シャード、インデックス作成、クエリ最適化などがあります。

記事では、MySQLワークベンチやPHPMyAdminなどの人気のあるMySQL GUIツールについて説明し、初心者と上級ユーザーの機能と適合性を比較します。[159文字]

この記事では、ドロップテーブルステートメントを使用してMySQLのドロップテーブルについて説明し、予防策とリスクを強調しています。これは、バックアップなしでアクションが不可逆的であることを強調し、回復方法と潜在的な生産環境の危険を詳述しています。

記事では、外部キーを使用してデータベース内の関係を表すことで、ベストプラクティス、データの完全性、および避けるべき一般的な落とし穴に焦点を当てています。

この記事では、クエリパフォーマンスを強化するために、PostgreSQL、MySQL、MongoDBなどのさまざまなデータベースでJSON列にインデックスの作成について説明します。特定のJSONパスのインデックス作成の構文と利点を説明し、サポートされているデータベースシステムをリストします。

記事では、準備されたステートメント、入力検証、および強力なパスワードポリシーを使用して、SQLインジェクションおよびブルートフォース攻撃に対するMySQLの保護について説明します。(159文字)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

SublimeText3 中国語版
中国語版、とても使いやすい

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

メモ帳++7.3.1
使いやすく無料のコードエディター

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

ホットトピック



