本文整理汇总了Python中_mysql_connector.MySQL.change_user方法的典型用法代码示例。如果您正苦于以下问题:Python MySQL.change_user方法的具体用法?Python MySQL.change_user怎么用?Python MySQL.change_user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类_mysql_connector.MySQL
的用法示例。
在下文中一共展示了MySQL.change_user方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_change_user
# 需要导入模块: from _mysql_connector import MySQL [as 别名]
# 或者: from _mysql_connector.MySQL import change_user [as 别名]
def test_change_user(self):
connect_kwargs = self.connect_kwargs.copy()
connect_kwargs['unix_socket'] = None
connect_kwargs['ssl_disabled'] = False
cmy1 = MySQL(buffered=True)
cmy1.connect(**connect_kwargs)
cmy2 = MySQL(buffered=True)
cmy2.connect(**connect_kwargs)
new_user = {
'user': 'cextuser',
'host': self.config['host'],
'database': self.connect_kwargs['database'],
'password': 'connc',
}
try:
cmy1.query("DROP USER '{user}'@'{host}'".format(**new_user))
except MySQLInterfaceError:
# Probably not created
pass
stmt = ("CREATE USER '{user}'@'{host}' IDENTIFIED WITH "
"caching_sha2_password").format(**new_user)
cmy1.query(stmt)
if tests.MYSQL_VERSION < (8, 0, 5):
cmy1.query("SET old_passwords = 0")
res = cmy1.query("SET PASSWORD FOR '{user}'@'{host}' = "
"PASSWORD('{password}')".format(**new_user))
else:
res = cmy1.query("ALTER USER '{user}'@'{host}' IDENTIFIED BY "
"'{password}'".format(**new_user))
cmy1.query("GRANT ALL ON {database}.* "
"TO '{user}'@'{host}'".format(**new_user))
cmy2.query("SHOW GRANTS FOR {user}@{host}".format(**new_user))
cmy2.query("SELECT USER()")
orig_user = cmy2.fetch_row()[0]
cmy2.free_result()
cmy2.change_user(user=new_user['user'], password=new_user['password'],
database=new_user['database'])
cmy2.query("SELECT USER()")
current_user = cmy2.fetch_row()[0]
self.assertNotEqual(orig_user, current_user)
self.assertTrue(
u"{user}@".format(**new_user) in current_user.decode('utf8'))
cmy2.free_result()