最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Alternative way to set MySQL root password - Stack Overflow

programmeradmin1浏览0评论

I am trying to build a Python script to automate MySQL installs,

so far it creates the mysql user, and downloads, untars & installs the rpm bundle for MySQL 8.

Post-install, I am trying to login as mysql root user with the temporary password using the mysql connector, in order to change the password.

The way I am trying to connect looks like this:

def login_to_mysql_db_inst(username, password):

mydb = mysql.connector.connect(
host="localhost",
user=username,
password=password
)

print(mydb)
return mydb


if __name__ == "__main__":

create_mysql_user(target_user) #create mysql user
download_mysql(db_version) #download and unzip mysql files
make_dirs_fn() #create data and log directories
chown_dirs_fn() #grant permissions and ownership on directories to mysql user

##need to update /etc/cfengine-hostclasses.txt
resolve_dependencies() #remove files that may conflict with the install
install() #installs mysql
write_config_to_file(cfengine_hostclass_lines, '/etc/cfengine-hostclasses.txt') ##write lines into /etc/cfengine-hostclasses.txt
write_config_to_file(mysql_config_lines, '/etc/myf') ##write lines into /etc/myf

run_cmd(["service","mysqld","start"]) #start mysql services
run_cmd(["service","mysqld","status"]) ##check status of mysql services
run_cmd(["chkconfig", "mysqld", "on"]) #enable mysqld.service -- enabling auto start

temp_password = get_temporary_mysql_password() 

login_to_mysql_db_inst('root',temp_password)
curr = mydb.cursor()

curr.execute("ALTER USER 'root'@'localhost' IDENTIFIED BY '*************';")

When I try this, I get an expired-password error

Traceback (most recent call last):


File "/usr/local/lib64/python3.6/site-packages/mysql/connector/connection_cext.py", line 236, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Your password has expired. To log in you must change it using a client that supports expired passwords.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mysql_install_script.py", line 283, in <module>
    login_to_mysql_db_inst('root',temp_password)
  File "mysql_install_script.py", line 250, in login_to_mysql_db_inst
    password=password
  File "/usr/local/lib64/python3.6/site-packages/mysql/connector/__init__.py", line 272, in connect
    return CMySQLConnection(*args, **kwargs)
  File "/usr/local/lib64/python3.6/site-packages/mysql/connector/connection_cext.py", line 85, in __init__
    self.connect(**kwargs)
  File "/usr/local/lib64/python3.6/site-packages/mysql/connector/abstracts.py", line 1028, in connect
    self._open_connection()
  File "/usr/local/lib64/python3.6/site-packages/mysql/connector/connection_cext.py", line 242, in _open_connection
    sqlstate=exc.sqlstate)
mysql.connector.errors.DatabaseError: 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.


 

I am able to manually reset it in the terminal by entering mysql -u root -p {temp_password} and then using the ALTER USER command to reset the password, but we are looking at a way to automate this via a python script.

Would very much appreciate any ideas?

I am trying to build a Python script to automate MySQL installs,

so far it creates the mysql user, and downloads, untars & installs the rpm bundle for MySQL 8.

Post-install, I am trying to login as mysql root user with the temporary password using the mysql connector, in order to change the password.

The way I am trying to connect looks like this:

def login_to_mysql_db_inst(username, password):

mydb = mysql.connector.connect(
host="localhost",
user=username,
password=password
)

print(mydb)
return mydb


if __name__ == "__main__":

create_mysql_user(target_user) #create mysql user
download_mysql(db_version) #download and unzip mysql files
make_dirs_fn() #create data and log directories
chown_dirs_fn() #grant permissions and ownership on directories to mysql user

##need to update /etc/cfengine-hostclasses.txt
resolve_dependencies() #remove files that may conflict with the install
install() #installs mysql
write_config_to_file(cfengine_hostclass_lines, '/etc/cfengine-hostclasses.txt') ##write lines into /etc/cfengine-hostclasses.txt
write_config_to_file(mysql_config_lines, '/etc/my.cnf') ##write lines into /etc/my.cnf

run_cmd(["service","mysqld","start"]) #start mysql services
run_cmd(["service","mysqld","status"]) ##check status of mysql services
run_cmd(["chkconfig", "mysqld", "on"]) #enable mysqld.service -- enabling auto start

temp_password = get_temporary_mysql_password() 

login_to_mysql_db_inst('root',temp_password)
curr = mydb.cursor()

curr.execute("ALTER USER 'root'@'localhost' IDENTIFIED BY '*************';")

When I try this, I get an expired-password error

Traceback (most recent call last):


File "/usr/local/lib64/python3.6/site-packages/mysql/connector/connection_cext.py", line 236, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Your password has expired. To log in you must change it using a client that supports expired passwords.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mysql_install_script.py", line 283, in <module>
    login_to_mysql_db_inst('root',temp_password)
  File "mysql_install_script.py", line 250, in login_to_mysql_db_inst
    password=password
  File "/usr/local/lib64/python3.6/site-packages/mysql/connector/__init__.py", line 272, in connect
    return CMySQLConnection(*args, **kwargs)
  File "/usr/local/lib64/python3.6/site-packages/mysql/connector/connection_cext.py", line 85, in __init__
    self.connect(**kwargs)
  File "/usr/local/lib64/python3.6/site-packages/mysql/connector/abstracts.py", line 1028, in connect
    self._open_connection()
  File "/usr/local/lib64/python3.6/site-packages/mysql/connector/connection_cext.py", line 242, in _open_connection
    sqlstate=exc.sqlstate)
mysql.connector.errors.DatabaseError: 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.


 

I am able to manually reset it in the terminal by entering mysql -u root -p {temp_password} and then using the ALTER USER command to reset the password, but we are looking at a way to automate this via a python script.

Would very much appreciate any ideas?

Share Improve this question asked Feb 7 at 16:58 madraruamadrarua 93 bronze badges 3
  • I would double-check what user the python is trying to authenticate with. root@ localhost and [email protected] are two different users and that could provide an explanation. – Shadow Commented Feb 7 at 17:42
  • The code in the question is missing indentation. Please fix it to match the actual script. – Barmar Commented Feb 7 at 18:08
  • The error message seems pretty clear, the temporary password has expired before you run the script. – Barmar Commented Feb 7 at 18:11
Add a comment  | 

1 Answer 1

Reset to default 0

You need the flag CAN_HANDLE_EXPIRED_PASSWORDS on the client_flags in the connection to be able to reset the password (and make the connection not fail).

发布评论

评论列表(0)

  1. 暂无评论