|
6 | 6 | import re |
7 | 7 | import fileinput |
8 | 8 | from collections import namedtuple |
| 9 | +from pwd import getpwuid |
9 | 10 | from time import time |
10 | 11 | from datetime import datetime |
11 | 12 | from random import choice |
|
48 | 49 | from .lexer import MyCliLexer |
49 | 50 | from .__init__ import __version__ |
50 | 51 | from .compat import WIN |
51 | | -from .packages.filepaths import dir_path_exists |
| 52 | +from .packages.filepaths import dir_path_exists, guess_socket_location |
52 | 53 |
|
53 | 54 | import itertools |
54 | 55 |
|
@@ -317,7 +318,7 @@ def read_my_cnf_files(self, files, keys): |
317 | 318 | """ |
318 | 319 | cnf = read_config_files(files, list_values=False) |
319 | 320 |
|
320 | | - sections = ['client'] |
| 321 | + sections = ['client', 'mysqld'] |
321 | 322 | if self.login_path and self.login_path != 'client': |
322 | 323 | sections.append(self.login_path) |
323 | 324 |
|
@@ -382,10 +383,11 @@ def connect(self, database='', user='', passwd='', host='', port='', |
382 | 383 | # Fall back to config values only if user did not specify a value. |
383 | 384 |
|
384 | 385 | database = database or cnf['database'] |
385 | | - if port or host: |
| 386 | + # Socket interface not supported for SSH connections |
| 387 | + if port or host or ssh_host or ssh_port: |
386 | 388 | socket = '' |
387 | 389 | else: |
388 | | - socket = socket or cnf['socket'] |
| 390 | + socket = socket or cnf['socket'] or guess_socket_location() |
389 | 391 | user = user or cnf['user'] or os.getenv('USER') |
390 | 392 | host = host or cnf['host'] |
391 | 393 | port = port or cnf['port'] |
@@ -430,11 +432,11 @@ def _connect(): |
430 | 432 | raise e |
431 | 433 |
|
432 | 434 | try: |
433 | | - if (socket is host is port is None) and not WIN: |
434 | | - # Try a sensible default socket first (simplifies auth) |
435 | | - # If we get a connection error, try tcp/ip localhost |
| 435 | + if not WIN and socket: |
| 436 | + socket_owner = getpwuid(os.stat(socket).st_uid).pw_name |
| 437 | + self.echo( |
| 438 | + f"Connecting to socket {socket}, owned by user {socket_owner}") |
436 | 439 | try: |
437 | | - socket = '/var/run/mysqld/mysqld.sock' |
438 | 440 | _connect() |
439 | 441 | except OperationalError as e: |
440 | 442 | # These are "Can't open socket" and 2x "Can't connect" |
|
0 commit comments