當前位置:首頁 » 編程語言 » pythonmysql超時

pythonmysql超時

發布時間: 2022-07-12 08:13:22

python3.6.5 始終連接不上mysql8.0

#createdatabase.py
#!/usr/bin/envpython
importos
importre
importsys
importpymysql
fromimportlibimportimport_mole

#searchthedirnameofsettings.pyandimportit
withopen('manage.py')asf:
s=f.read()
d=re.search(r'DJANGO_SETTINGS_MODULE.*?,s*"(.+?).settings',s).group(1)
assert'settings.py'inos.listdir(d)
mo=import_mole('{d}.localsettings'.format(d=d))


defgetconf(alias='default'):
dbconf=mo.DATABASES.get(alias)
config={'host':dbconf.get('HOST'),
'user':dbconf.get('USER'),
'passwd':dbconf.get('PASSWORD'),
'port':dbconf.get('PORT'),
'charset':'utf8',}
config={k:vfork,vinconfig.items()ifvisnotNone}
db_name=dbconf.get('NAME')
returnconfig,db_name


defcreat_db(config,db_name):
try:
conn=pymysql.connect(**config)
cur=conn.cursor()
if'-d'insys.argv:
cur.execute('dropdatabase{}'.format(db_name))
print('successtoexecute`dropdatabase{};`'.format(db_name))
command='createdatabase{}_general_ci'.format(db_name)
cur.execute(command)
print('successtoexecute`{};`'.format(command))
#conn.select_db(database)
conn.commit()

cur.close()
conn.close()
exceptExceptionase:
print("SQLError:{e}".format(e=e))


defmain():
creat_db(*getconf())
#creat_db(*getconf('mysql_property'))

估計是密碼錯了,你直接在命令行運行:

mysql-uroot-proot

看看密碼是不是root

㈡ python連接mysql資料庫出錯,已經嘗試了網上的幾種解決方法

這里的意思是:資料庫連不上啊。

可能是網路問題,可能是防火牆問題,可能是3306埠沒開。你先排除這些問題吧。用一些mysql工具連接測試看,比如SQLyog 測試。

㈢ python如何設置超時重新運行

python通過subprocess模塊調用系統命令。實際使用中,有一次是命令進入了交互模式,結果web端直接卡死了。
調用時設置一個超時時間,時間用完後自動斷開。
這樣就避免了系統因為調用命令而僵死的問題。

㈣ MySQL-python連接MySQL資料庫問題,總是拋異常。

不要剛開始學多線程編程就這樣玩。connection 和 cursor 都不是線程安全的。

如果測試環境用多個線程,每個線程要在線程裡面獲取自己的 connection,然後從這個connection 獲取 cursor.

如果生產環境用多個線程,建議使用線程安全的連接池。

㈤ 如何給 Python 的 MySQLDB 模塊增加 Timeout 超時功能

fromthreadingimportTimer
def_timeout():
raiseTimeoutExc("Timeoutforquery.")
timer=Timer(TIMEOUT,_timeout)#TIMEOUT:超時時間,單位是秒
timer.start()
try:
MySQLDB的查詢語句
exceptException,e:
raisee
finally:
timer.cancel()

㈥ 如何給 Python 的 MySQLDB 模塊增加 Timeout 超時功能

使用Python操作MySQL資料庫的時候常使用MySQLdb這個模塊。

今天在開發的過程發現MySQLdb.connect有些參數沒法設置。通過這個頁面我們可以看到在connect的時候,可以設置的option和client_flags和MySQL
c api相比差不少。

一個很重要的參數
MYSQL_OPT_READ_TIMEOUT沒法設置,這個參數如果不設置,極致狀況MySQL處於hang住,自動切換IP漂移,客戶端無法重連到新MySQL。

給MySQLdb加Option很簡單,只要修改_mysql.c這個把Python對象映射到MySQL操作的文件,添加參數,再加一段mysql_option即可。

下面是修改後的git diff 文件

diff --git a/_mysql.c b/_mysql.c
index d42cc54..61a9b34 100644
--- a/_mysql.c
+++ b/_mysql.c
@@ -489,9 +489,10 @@ _mysql_ConnectionObject_Initialize( "named_pipe", "init_command", "read_default_file", "read_default_group", "client_flag", "ssl",
- "local_infile",
+ "local_infile", "read_timeout",
NULL } ;
int connect_timeout = 0;
+ int read_timeout = 0;
int compress = -1, named_pipe = -1, local_infile = -1;
char *init_command=NULL,
*read_default_file=NULL,
@@ -500,7 +501,7 @@ _mysql_ConnectionObject_Initialize(
self->converter = NULL;
self->open = 0;
check_server_init(-1);
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssisOiiisssiOi:connect",
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssisOiiisssiOii:connect",
kwlist,
&host, &user, &passwd, &db,
&port, &unix_socket, &conv,
@@ -509,7 +510,8 @@ _mysql_ConnectionObject_Initialize(
&init_command, &read_default_file,
&read_default_group,
&client_flag, &ssl,
- &local_infile /* DO NOT PATCH FOR RECONNECT, IDIOTS
+ &local_infile, &read_timeout
+ /* DO NOT PATCH FOR RECONNECT, IDIOTS
IF YOU DO THIS, I WILL NOT SUPPORT YOUR PACKAGES. */
)) return -1;
@@ -540,6 +542,12 @@ _mysql_ConnectionObject_Initialize(
mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT,
(char *)&timeout);
}
+
+ if (read_timeout) {
+ unsigned int timeout = read_timeout;
+ mysql_options(&(self->connection), MYSQL_OPT_READ_TIMEOUT, (char *)&timeout);
+ }
+ if (compress != -1) {
mysql_options(&(self->connection), MYSQL_OPT_COMPRESS, 0);
client_flag |= CLIENT_COMPRESS;

代碼修改完畢,python
setup.py install 即可,如果出現mysql_config找不到的問題。你還要修改setup_posix.py文件。

OperationalError: (2013, 'Lost connection to MySQL server ring query')
>/home/hoterran/Projects/dbaas/trunk/dbtest.py(18)()
>mydb.execute_sql(conn, sql)
(Pdb)
--Return--
> /home/hoterran/Projects/dbaas/trunk/dbtest.py(18)()->None
> mydb.execute_sql(conn, sql)
(Pdb)
OperationalError: (2013, 'Lost connection to MySQL server ring query')
> (1)()->None

ps: 在_mysql.c找到一句很搞的話

/* DO NOT PATCH FOR RECONNECT, IDIOTS
IF YOU DO THIS, I WILL NOT SUPPORT YOUR PACKAGES. */

轉載僅供參考,版權屬於原作者。祝你愉快,滿意請採納哦

㈦ 如何給 Python 的 MySQLDB 模塊增加 Timeout 超時功能

使用Python操作MySQL資料庫的時候常使用MySQLdb這個模塊。

今天在開發的過程發現MySQLdb.connect有些參數沒法設置。通過這個頁面我們可以看到在connect的時候,可以設置的option和client_flags和MySQL
c api相比差不少。

一個很重要的參數
MYSQL_OPT_READ_TIMEOUT沒法設置,這個參數如果不設置,極致狀況MySQL處於hang住,自動切換IP漂移,客戶端無法重連到新MySQL。

給MySQLdb加Option很簡單,只要修改_mysql.c這個把Python對象映射到MySQL操作的文件,添加參數,再加一段mysql_option即可。

下面是修改後的git diff 文件

diff --git a/_mysql.c b/_mysql.c
index d42cc54..61a9b34 100644
--- a/_mysql.c
+++ b/_mysql.c
@@ -489,9 +489,10 @@ _mysql_ConnectionObject_Initialize( "named_pipe", "init_command", "read_default_file", "read_default_group", "client_flag", "ssl",
- "local_infile",
+ "local_infile", "read_timeout",
NULL } ;
int connect_timeout = 0;
+ int read_timeout = 0;
int compress = -1, named_pipe = -1, local_infile = -1;
char *init_command=NULL,
*read_default_file=NULL,
@@ -500,7 +501,7 @@ _mysql_ConnectionObject_Initialize(
self->converter = NULL;
self->open = 0;
check_server_init(-1);
- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssisOiiisssiOi:connect",
+ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssisOiiisssiOii:connect",
kwlist,
&host, &user, &passwd, &db,
&port, &unix_socket, &conv,
@@ -509,7 +510,8 @@ _mysql_ConnectionObject_Initialize(
&init_command, &read_default_file,
&read_default_group,
&client_flag, &ssl,
- &local_infile /* DO NOT PATCH FOR RECONNECT, IDIOTS
+ &local_infile, &read_timeout
+ /* DO NOT PATCH FOR RECONNECT, IDIOTS
IF YOU DO THIS, I WILL NOT SUPPORT YOUR PACKAGES. */
)) return -1;
@@ -540,6 +542,12 @@ _mysql_ConnectionObject_Initialize(
mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT,
(char *)&timeout);
}
+
+ if (read_timeout) {
+ unsigned int timeout = read_timeout;
+ mysql_options(&(self->connection), MYSQL_OPT_READ_TIMEOUT, (char *)&timeout);
+ }
+ if (compress != -1) {
mysql_options(&(self->connection), MYSQL_OPT_COMPRESS, 0);
client_flag |= CLIENT_COMPRESS;

代碼修改完畢,python
setup.py install 即可,如果出現mysql_config找不到的問題。你還要修改setup_posix.py文件。

hoterran@hoterran-laptop:~/Projects/MySQL-python-1.2.3$ git diff setup_posix.py diff --git a/setup_posix.py b/setup_posix.py
index 86432f5..f4f08f1 100644
--- a/setup_posix.py
+++ b/setup_posix.py
@@ -23,7 +23,7 @@ def mysql_config(what): if ret/256 > 1:
raise EnvironmentError("%s not found" % (mysql_config.path,)) return data
-mysql_config.path = "mysql_config"
+mysql_config.path = "/usr/local/mysql/bin/mysql_config"
def get_config(): import os, sys

編譯通過,我們來試試添加的read_timeout這個參數。

conn = MySQLdb.connect(host = DB_SERVER,user = DB_USERNAME,passwd = DB_PASSWORD,db = DB_NAME, port=int(DB_PORT), client_flag = 2, read_timeout = 10)

然後執行語句前,你試著把mysql用gdb hang住10s後,python就會異常拋錯

OperationalError: (2013, 'Lost connection to MySQL server ring query')
>/home/hoterran/Projects/dbaas/trunk/dbtest.py(18)()
>mydb.execute_sql(conn, sql)
(Pdb)
--Return--
> /home/hoterran/Projects/dbaas/trunk/dbtest.py(18)()->None
> mydb.execute_sql(conn, sql)
(Pdb)
OperationalError: (2013, 'Lost connection to MySQL server ring query')
> (1)()->None

ps: 在_mysql.c找到一句很搞的話

/* DO NOT PATCH FOR RECONNECT, IDIOTS
IF YOU DO THIS, I WILL NOT SUPPORT YOUR PACKAGES. */

㈧ python 連接mysql 時,connect 出現錯誤,怎麼解決

下面是我用過的連接mysql資料庫的方法,你試試吧,用戶名和密碼都是root,如果密碼不一樣記得修改code中的密碼,還有就是檢查一下你的資料庫是不是正常的

如果報錯--截圖,追問

importMySQLdb
defdo_mysql_operate(str):
try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='task',port=3306)
cur=conn.cursor()
cur.execute(str)
conn.commit()
cur.close()
conn.close()
exceptMySQLdb.Error,e:
print"mysqlerror%d:%s"%(e.args[0],e.args[1])

㈨ 如何給 Python 的 MySQLDB 模塊增加 Timeout 超時功能

使用Python操作MySQL資料庫的時候常使用MySQLdb這個模塊。 今天在開發的過程發現MySQLdb.connect有些參數沒法設置。通過這個頁面我們可以看到在connect的時候,可以設置的option和client_flags和MySQL c api相比差不少。 一個很重要的參數 MYSQL...

㈩ python 連接 mysql 超時,請問怎麼解決

使用Python操作MySQL資料庫的時候常使用MySQLdb這個模塊。 今天在開發的過程發現MySQLdb.connect有些參數沒法設置。通過這個頁面我們可以看到在connect的時候,可以設置的option和client_flags和MySQL c api相比差不少。

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:432
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:536
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:146
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:232
java駝峰 發布:2025-02-02 09:13:26 瀏覽:651
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:532
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726