Guest User

Untitled

a guest
Oct 4th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. ssh ssh_user@ssh_server_host_name -p 22
  2. mysql -h mysql_server_host_name -P 3306 -u mysql_user -p
  3.  
  4. # -*- coding: utf-8 -*-
  5. from sshtunnel import SSHTunnelForwarder
  6. # モジュール読み込み
  7. import pymysql.cursors
  8.  
  9. # SSH関連の設定
  10. with SSHTunnelForwarder(
  11. ("ssh_server_host_name", 22),
  12. ssh_username="ssh_user",
  13. ssh_password="ssh_pass",
  14. remote_bind_address=('mysql_server_host_name', 3306)
  15. ) as server:
  16. print("connected server")
  17.  
  18. # # MySQLに接続する
  19. conn = pymysql.connect(host='mysql_server_host_name',
  20. user='mysql_user',
  21. password='pass',
  22. db='db_name',
  23. charset='utf8',
  24. port=server.local_bind_port,
  25. cursorclass=pymysql.cursors.DictCursor)
  26. # select
  27. # SQLを実行する
  28. cursor = conn.cursor()
  29. sql = "show tables"
  30. cursor.execute(sql)
  31.  
  32. # Select結果を取り出す
  33. rets = cursor.fetchall()
  34. for r in rets:
  35. print(r)
  36.  
  37. # MySQLから切断する
  38. conn.close()
  39.  
  40. Traceback (most recent call last):
  41. File "/Users/user/venv/lib/python3.7/site-packages/pymysql/connections.py", line 582, in connect
  42. **kwargs)
  43. File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 727, in create_connection
  44. raise err
  45. File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 716, in create_connection
  46. sock.connect(sa)
  47. socket.timeout: timed out
  48.  
  49. During handling of the above exception, another exception occurred:
  50.  
  51. Traceback (most recent call last):
  52. File "/Users/user/PycharmProjects/bitflyerPredict/database.py", line 25, in <module>
  53. cursorclass=pymysql.cursors.DictCursor)
  54. File "/Users/user/PycharmProjects/bitflyerPredict/venv/lib/python3.7/site-packages/pymysql/__init__.py", line 94, in Connect
  55. return Connection(*args, **kwargs)
  56. File "/Users/user/PycharmProjects/bitflyerPredict/venv/lib/python3.7/site-packages/pymysql/connections.py", line 327, in __init__
  57. self.connect()
  58. File "/Users/user/PycharmProjects/bitflyerPredict/venv/lib/python3.7/site-packages/pymysql/connections.py", line 629, in connect
  59. raise exc
  60. pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '(hostname)' (timed out)")
  61.  
  62. conn = pymysql.connect(host='localhost',
  63. user='mysql_user',
  64. password='pass',
  65. db='db_name',
  66. charset='utf8',
  67. port=server.local_bind_port,
  68. cursorclass=pymysql.cursors.DictCursor)
Add Comment
Please, Sign In to add comment