質問編集履歴

3

解決策について

2018/12/30 06:00

投稿

NERO06
NERO06

スコア14

test CHANGED
File without changes
test CHANGED
@@ -290,4 +290,8 @@
290
290
 
291
291
 
292
292
 
293
+ ---
294
+
295
+ 結果的に解決策は分からなかったですが、いろんな環境を見直して改めて状況を整理してみようと思います。
296
+
293
- 上記のエラーが出ているということは、
297
+ 一旦解決という形で締めさせてもらいます。

2

エラー内容と諸設定について追記

2018/12/30 06:00

投稿

NERO06
NERO06

スコア14

test CHANGED
File without changes
test CHANGED
@@ -28,8 +28,6 @@
28
28
 
29
29
  remote_bind_address=("mysql~~.xserver.jp", 3306)
30
30
 
31
- #local_bind_address=('127.0.0.1',3306)
32
-
33
31
  ) as ssh:
34
32
 
35
33
  config = {
@@ -76,7 +74,9 @@
76
74
 
77
75
 
78
76
 
79
- (以下追記)----------------------------------------------------------------
77
+ ---------------------------------------------------------------------------------------
78
+
79
+ (以下追記1)
80
80
 
81
81
 
82
82
 
@@ -103,3 +103,191 @@
103
103
  ③SSHサーバ=メインサーバ、リモートサーバ=メインサーバ、python-mysqlのホスト設定=MySQLサーバ
104
104
 
105
105
  =>理由不明もうまくいかず...
106
+
107
+
108
+
109
+ ---------------------------------
110
+
111
+ (追記2)
112
+
113
+ 全てエラー内容は`Can't connect to MySQL server on '~~'`となります。
114
+
115
+
116
+
117
+ セット内容とエラーコードは以下になります。
118
+
119
+
120
+
121
+ ①SSHサーバ=メインサーバ、リモートサーバ=MySQLサーバ、python-mysqlのホスト設定=localhost
122
+
123
+ ```
124
+
125
+ with SSHTunnelForwarder(
126
+
127
+ ("~~xsrv.jp",10022),
128
+
129
+ ssh_pkey="~/~~/id_rsa",
130
+
131
+ ssh_username="ssh_username",
132
+
133
+ ssh_password="ssh_password",
134
+
135
+ remote_bind_address=("mysql~~.xserver.jp", 3306)
136
+
137
+ ) as ssh:
138
+
139
+ print("a")
140
+
141
+ config = {
142
+
143
+ "host": "localhost",
144
+
145
+ "port": ssh.local_bind_port,
146
+
147
+ "user": "db_user",
148
+
149
+ "password": "db_password"
150
+
151
+ }
152
+
153
+ #DB処理
154
+
155
+ ```
156
+
157
+ 'a'は出力され、その後に以下エラーコードが出る
158
+
159
+ ```エラーコード
160
+
161
+ DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'mysql~~.xserver.jp' (60)
162
+
163
+ ```
164
+
165
+
166
+
167
+ ②SSHサーバ=メインサーバ、リモートサーバ=MySQLサーバ、python-mysqlのホスト設定=MySQLサーバ
168
+
169
+ ```
170
+
171
+ with SSHTunnelForwarder(
172
+
173
+ ("~~xsrv.jp",10022),
174
+
175
+ ssh_pkey="~/~~/id_rsa",
176
+
177
+ ssh_username="ssh_username",
178
+
179
+ ssh_password="ssh_password",
180
+
181
+ remote_bind_address=("mysql5009.xserver.jp", 3306)
182
+
183
+ ) as ssh:
184
+
185
+ print("a")
186
+
187
+ config = {
188
+
189
+ "host": "mysql~~.xserver.jp",
190
+
191
+ "port": ssh.local_bind_port,
192
+
193
+ "user": "db_user",
194
+
195
+ "password": "db_password"
196
+
197
+ }
198
+
199
+ #DB処理
200
+
201
+ ```
202
+
203
+ 'a'は出力され、以下エラーが出る
204
+
205
+ ```
206
+
207
+ DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'mysql~~.xserver.jp' (60)
208
+
209
+ ```
210
+
211
+
212
+
213
+
214
+
215
+ ③SSHサーバ=メインサーバ、リモートサーバ=メインサーバ、python-mysqlのホスト設定=MySQLサーバ
216
+
217
+ ```
218
+
219
+ with SSHTunnelForwarder(
220
+
221
+ ("~~xsrv.jp",10022),
222
+
223
+ ssh_pkey="~/~~/id_rsa",
224
+
225
+ ssh_username="ssh_username",
226
+
227
+ ssh_password="ssh_password",
228
+
229
+ remote_bind_address=("localhost", 3306)
230
+
231
+ ) as ssh:
232
+
233
+ print("a")
234
+
235
+ config = {
236
+
237
+ "host": "mysql~~.xserver.jp",
238
+
239
+ "port": ssh.local_bind_port,
240
+
241
+ "user": "db_user",
242
+
243
+ "password": "db_password"
244
+
245
+ }
246
+
247
+ #DB処理
248
+
249
+ ```
250
+
251
+ 'a'は出力され、その後に以下エラーコードが出る
252
+
253
+ ```エラーコード
254
+
255
+ DatabaseError: 2003 (HY000): Can't connect to MySQL server on 'mysql~~.xserver.jp' (60)
256
+
257
+ ```
258
+
259
+
260
+
261
+ DatabaseErrorで、`print("a")`は出力されているので、SSHは無事に繋がっているとは思うのですが、つなぐ場所がおかしいのかなと考えています。
262
+
263
+
264
+
265
+ SSHサーバに手動で繋いだ上だと`"mysql -h mysql~~.xserver.jp -u db_user -p db_password`で入れて言います。
266
+
267
+
268
+
269
+ また、手動でメインサーバにSSH接続した上で以下設定でmysql-connector-pythonを実行すると接続することができます。
270
+
271
+ ```
272
+
273
+ config = {
274
+
275
+ "host": "mysql~~.xserver.jp",
276
+
277
+ "user": "db_user",
278
+
279
+ "password": "db_password"
280
+
281
+ }
282
+
283
+ ```
284
+
285
+
286
+
287
+
288
+
289
+ (mysql.connector.connetでhost=localhostと設定した場合は、もちろん'mysql~~.xserver.jp'の部分が'localhost'に変わります。)
290
+
291
+
292
+
293
+ 上記のエラーが出ているということは、

1

エックスサーバの構成、その他の失敗事例について追記

2018/12/27 05:50

投稿

NERO06
NERO06

スコア14

test CHANGED
File without changes
test CHANGED
@@ -73,3 +73,33 @@
73
73
 
74
74
 
75
75
  ご教示頂ければ幸いです。
76
+
77
+
78
+
79
+ (以下追記)----------------------------------------------------------------
80
+
81
+
82
+
83
+ 色々と調べてみるとエックスサーバとそのMySQLの構成は以下のようになっているようです。
84
+
85
+ メインサーバ => IPアドレスA(複数のホスト名有りもIPアドレスは一つに紐づいている)
86
+
87
+ MySQLサーバ => IPアドレスB
88
+
89
+ MySQLユーザのホスト => メインサーバに紐づいている
90
+
91
+
92
+
93
+ なので、今まで実験して見た下記プログラムでうまくいっていないのかなと思うのですが、他に具体的なやり方浮かばず、立ち往生しています。
94
+
95
+ ①SSHサーバ=メインサーバ、リモートサーバ=MySQLサーバ、python-mysqlのホスト設定=localhost
96
+
97
+ =>MySQLのユーザ設定から拒否されている
98
+
99
+ ②SSHサーバ=メインサーバ、リモートサーバ=MySQLサーバ、python-mysqlのホスト設定=MySQLサーバ
100
+
101
+ =>MySQLのユーザ設定から拒否されている(このMySQLユーザが利用できるホストがおかしい)
102
+
103
+ ③SSHサーバ=メインサーバ、リモートサーバ=メインサーバ、python-mysqlのホスト設定=MySQLサーバ
104
+
105
+ =>理由不明もうまくいかず...