다음 상황을 이해하는 데 도움이 필요합니다. 저장된 mysql 프로 시저를 호출하고 변수에 출력을 저장 한 다음 내용에 액세스하여 텍스트가 있는지 확인하려고합니다. 저는 파이썬에 익숙하지 않고 어떻게해야하는지 이해하는 데 어려움을 겪고 있습니다. 필자는 mysql 쿼리 show slave status;
대신에이 프로 시저를 호출해야합니다. 하지만 기본적으로 함수는 해당 쿼리를 수행합니다. cursor.callproc의 결과에 액세스하는 Python 3.X
Python 3.3.2+ (default, Feb 28 2014, 00:52:16)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>> conn = mysql.connector.connect(host = "xxxx", port = xxxx, user = "xxxx", password = "xxxx", database = "xxxx")
>>> cursor = conn.cursor()
>>> cursor.callproc('show_slave_status')
()
>>> for result in cursor.stored_results():
... print(result.fetchall())
...
[]
내가 대신 cursor.callproc
를 사용하는 쿼리를 실행하려고하면
>>> cursor.execute("call show_slave_status")
Traceback (most recent call last):
File "/usr/local/lib/python3.3/dist-packages/mysql/connector/cursor.py", line 508, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/usr/local/lib/python3.3/dist-packages/mysql/connector/connection.py", line 640, in cmd_query
'Use cmd_query_iter for statements with multiple queries.')
mysql.connector.errors.InterfaceError: Use cmd_query_iter for statements with multiple queries.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.3/dist-packages/mysql/connector/cursor.py", line 512, in execute
"Use multi=True when executing multiple statements")
mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements
을 내가 multi=True
를 설정하면 그것은 더 나은하지 않습니다.
여기 여기에 기능 출력 내가 MySQL의 콘솔
mysql> call show_slave_status;
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id |
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+
| Waiting for master to send event | localhost | xxxx | xxxx | 60 | mysql-bin.000025 | 5782876 | relay-bin.000075 | 5782979 | mysql-bin.000025 | Yes | Yes | | | | | | xxxx%.%\_NUM\_MEM | 0 | | 0 | 5782876 | 5783275 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | xxxx |
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+
1 row in set (0.10 sec)
에 쿼리를 실행하고하면 제대로 stored_results()
방법을 사용하여하고있는 프로 시저 정의
mysql> SHOW CREATE PROCEDURE show_slave_status\G
*************************** 1. row ***************************
Procedure: show_slave_status
sql_mode:
Create Procedure: CREATE DEFINER=`xxxx`@`localhost` PROCEDURE `show_slave_status`()
BEGIN show slave status; END
character_set_client: latin1
collation_connection: latin1_swedish_ci
Database Collation: latin1_swedish_ci
1 row in set (0.27 sec)
'SLAVE STATUS 표시'는 순전히 인쇄용이므로 'callproc'이 볼 수있는 모든 항목을 반환하지 않습니다. 테이블에서 행을 반환하는 프로 시저를 변경하여 검사 할 수 있습니다. –
@JonClements : SHOW SLAVE STATUS (다른 SHOW 문과 마찬가지로)는 MySQL 서버로 전달되어 SELECT와 같은 결과 집합을 반환합니다. 대부분의 SHOW 문은 기본적으로 INFORMATION_SCHEMA에서 선택하는 바로 가기입니다. – geertjanvdk