File tree Expand file tree Collapse file tree 1 file changed +12
-6
lines changed Expand file tree Collapse file tree 1 file changed +12
-6
lines changed Original file line number Diff line number Diff line change @@ -35,33 +35,39 @@ def wait_for_mysql():
3535 connection .close ()
3636 return
3737
38+
3839def check_and_create_mysql_user ():
39- connection = pymysql .connect (host = DB_HOST , port = DB_PORT , user = DB_ROOT_USER , passwd = DB_ROOT_PASSWD )
40+ connection = pymysql .connect (
41+ host = DB_HOST , port = DB_PORT , user = DB_ROOT_USER , passwd = DB_ROOT_PASSWD
42+ )
4043
4144 try :
4245 with connection .cursor () as cursor :
43- if db_user != ' root' :
46+ if db_user != " root" :
4447 query_user_exists = f"SELECT EXISTS(SELECT 1 FROM mysql.user WHERE User='{ db_user } ' AND Host='%') AS user_exists;"
4548 cursor .execute (query_user_exists )
4649 result = cursor .fetchone ()
4750 user_exists = result [0 ] == 1
4851 if not user_exists :
49- create_user_sql = f"CREATE USER '{ db_user } '@'%' IDENTIFIED BY '{ db_passwd } ';"
52+ create_user_sql = (
53+ f"CREATE USER '{ db_user } '@'%' IDENTIFIED BY '{ db_passwd } ';"
54+ )
5055 cursor .execute (create_user_sql )
5156 print (f"Create user '{ db_user } '@'%' sucessfully." )
52-
57+
5358 grant_privileges_sql = (
5459 f"GRANT ALL PRIVILEGES ON { DATABASE_NAME } .* TO '{ db_user } '@'%';"
5560 )
56- for stmt in grant_privileges_sql .split (';' ):
61+ for stmt in grant_privileges_sql .split (";" ):
5762 if stmt .strip ():
5863 cursor .execute (stmt )
5964 print (f"Granted user '{ db_user } '@'%' privileges sucessfully." )
60-
65+
6166 cursor .execute ("FLUSH PRIVILEGES;" )
6267 finally :
6368 connection .close ()
6469
70+
6571wait_for_mysql ()
6672
6773sql = 'mysql -h %s -u%s -p%s -e "CREATE DATABASE IF NOT EXISTS %s;"' % (
You can’t perform that action at this time.
0 commit comments