def connect_with_mycnf(use_mysqlx=False, file=None): try: import configparser except: print("ERROR: Module Python configparser must be installed") return if not file: try: from pathlib import Path file = str(Path.home()) + "/.my.cnf" except: print( "ERROR: Module Pathlib must be installed or provide a filename" ) return import getpass print("let's use info from %s to connect" % file) config = configparser.ConfigParser() config.read(file) if not config.has_section('client'): print("ERROR: your my.cnf file should contain a section '[client]'") return user = config['client'].get('user', getpass.getuser()) hostname = config['client'].get('host', 'localhost') password = config['client'].get('password', '') import mysqlsh shell = mysqlsh.globals.shell if use_mysqlx: scheme = "mysqlx" port = config['client'].get('port', '33060') connection_dict = { "scheme": scheme, "user": user, "password": password, "host": hostname, "port": port } session = mysqlx.get_session(connection_dict) else: scheme = "mysql" port = config['client'].get('port', '3306') connection_dict = { "scheme": scheme, "user": user, "password": password, "host": hostname, "port": port } session = mysql.get_session(connection_dict) shell.set_session(session) return
def _connect_to_std_protocol(session): port = _get_std_protocol_port(session) uri_json = shell.parse_uri(session.get_uri()) uri_json['port'] = port uri_json['scheme'] = "mysql" uri = shell.unparse_uri(uri_json) print("Using %s to connect to standard protocol..." % uri) session2 = mysql.get_session(uri) return session2
def connect_with_mycnf(use_mysqlx=False, file=None): """ Connect to MySQL using old .my.cnf file. This function reads the login information from .my.cnf. Args: use_mysqlx (bool): Optional boolean value to use or not MySQL X protocol (default: false). file: The optional location of the my.cnf file (default: ~/.my.cnf). """ try: import configparser except: print("ERROR: Module Python configparser must be installed") return if not file: try: from pathlib import Path file=str(Path.home()) + "/.my.cnf" except: print("ERROR: Module Pathlib must be installed or provide a filename") return import getpass print("let's use info from %s to connect" % file) config = configparser.ConfigParser() config.read(file) if not config.has_section('client'): print("ERROR: your my.cnf file should contain a section '[client]'") return user=config['client'].get('user', getpass.getuser()) hostname=config['client'].get('host', 'localhost') password=config['client'].get('password', '') import mysqlsh shell = mysqlsh.globals.shell if use_mysqlx: scheme = "mysqlx" port=config['client'].get('port', '33060') connection_dict = { "scheme": scheme, "user": user, "password": password, "host": hostname, "port": port } session = mysqlx.get_session(connection_dict) else: scheme = "mysql" port=config['client'].get('port', '3306') connection_dict = { "scheme": scheme, "user": user, "password": password, "host": hostname, "port": port } session = mysql.get_session(connection_dict) shell.set_session(session) return
def _connect_to_secondary(shell, session, secondary): uri_json = shell.parse_uri(session.get_uri()) uri_json['host'] = secondary.split(':')[0] uri_json['port'] = secondary.split(':')[1] uri_json['scheme'] = "mysql" uri = shell.unparse_uri(uri_json) #print("Using %s to connect to secondary..." % uri) #_check_session_cred(shell, uri) password = shell.prompt('Please enter password for %s: ' % uri, {'type': 'password'}) session2 = mysql.get_session(uri, password) return session2
def collect_topology_diagnostics(zf, prefix, session, creds, innodb_mutex=False, slow_queries=False, ignore_errors=False): instance_data = [] members = get_topology_members(session) for instance_id, endpoint in members: data = {"instance_id": instance_id, "endpoint": endpoint} print(f"Collecting information from {endpoint}...") try: dest = dict(creds) dest["host"], dest["port"] = endpoint.split(":") session = mysql.get_session(dest) except Error as e: print(f"Could not connect to {endpoint}: {e}") with zf.open(f"{prefix}/{instance_id}.connect_error.txt", "w") as f: f.write((str(e) + "\n").encode("utf-8")) if e.code == mysql.ErrorCode.ER_ACCESS_DENIED_ERROR: raise continue info = collect_member_info(zf, prefix, f"{instance_id}", session, slow_queries=slow_queries, ignore_errors=ignore_errors) data["member_info"] = info info = collect_innodb_metrics(zf, prefix, f"{instance_id}", session, innodb_mutex=innodb_mutex, ignore_errors=ignore_errors) data["innodb_info"] = info print() instance_data.append(data) return instance_data
def __init__(self, uri=False): self.user = None self.ip = None self.port = None self.members = [] self.hosts = [] self.version = None self.w_hostgroup = 2 self.b_w_hostgroup = 4 self.r_hostgroup = 3 self.o_hostgroup = 1 self.max_writer = 1 self.writer_is_reader = 0 self.max_transaction_behind = 100 self.monitor_user = "******" self.monitor_pwd = "monitor" self.uri = uri self.user = shell.parse_uri(self.uri)['user'] if self.user == 'admin': sys.tracebacklimit = 0 raise Exception( "You should not use the default 'admin' user, please create a new one in ProxySQL" ) self.ip = shell.parse_uri(self.uri)['host'] self.port = shell.parse_uri(self.uri)['port'] if not "password" in shell.parse_uri(self.uri): self.__password = shell.prompt('Password: '******'type': 'password'}) else: self.__password = shell.parse_uri(self.uri)['password'] try: self.session = mysql.get_session( "%s:%s@%s:%s?ssl-mode=DISABLED" % (self.user, self.__password, self.ip, self.port)) stmt = "select version()" result = self.session.run_sql(stmt) self.version = result.fetch_one()[0] print("Connected to ProxySQL (%s)" % self.version) except: sys.tracebacklimit = 0 raise Exception( "Not possible to connect to ProxySQL admin interface !")
# __user: <user> # __uripwd: <uri>:<pwd>@<host> # __pwd: <pwd> #@ mysql module: exports all_exports = dir(mysql) # Remove the python built in members exports = [] for member in all_exports: if not member.startswith('__'): exports.append(member) # The dir function appends 3 built in members print 'Exported Items:', len(exports) validateMember(exports, 'get_classic_session') validateMember(exports, 'get_session') validateMember(exports, 'help') #@# get_classic_session errors mysql.get_classic_session() mysql.get_classic_session(1, 2, 3) mysql.get_classic_session(["bla"]) mysql.get_classic_session("some@uri", 25) #@# get_session errors mysql.get_session() mysql.get_session(1, 2, 3) mysql.get_session(["bla"]) mysql.get_session("some@uri", 25)
# __uripwd: <uri>:<pwd>@<host> # __pwd: <pwd> #@ mysql module: exports all_exports = dir(mysql) # Remove the python built in members exports = [] for member in all_exports: if not member.startswith('__'): exports.append(member) # The dir function appends 3 built in members print 'Exported Items:', len(exports) validateMember(exports, 'get_classic_session'); validateMember(exports, 'get_session'); validateMember(exports, 'help'); #@# get_classic_session errors mysql.get_classic_session() mysql.get_classic_session(1, 2, 3) mysql.get_classic_session(["bla"]) mysql.get_classic_session("some@uri", 25) #@# get_session errors mysql.get_session() mysql.get_session(1, 2, 3) mysql.get_session(["bla"]) mysql.get_session("some@uri", 25)