1. 程式人生 > 實用技巧 >建立MySQL賬戶

建立MySQL賬戶

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#----------------------------------------------------------#
# Date    : xxxx-xx-xx                                     #
# Author  : Created by zhouwanchun.                        #
# Wechat  : loveoracle11g                                  #
# Function: This scripts function is ...                   #
# Version : 1.1 # #----------------------------------------------------------# # 匯入模組 import os import subprocess import mysql.connector import myloginpath # Linux終端清屏 os.system('clear') # 註釋資訊 print("""\033[1;36m ############################################################ # Date : 2020-05-22 # # Author : Created by zhouwanchun. # # Wechat : loveoracle11g # # Function: This scripts function is ... # # Version : v1.1 # ############################################################ \033[0m
""") # 連線資料庫賬號 conn_user = 'dba' print("""\033[1;35m 建立管理賬號,請輸入 : manager 建立開發賬號,請輸入 : dev \033[0m""") choices = input("請輸入你要建立的賬號型別 : ").strip() def create_mysql_user(): user = input("請設定user : ").strip() host = input("請設定host : ").strip() password = input("請設定密碼 : ").strip() create_user
= "create user " + "'" + user + "'" + "@" + "'" + host + "'" + " identified by " + "'" + password + "';" if choices == 'manager': grant_user = "grant all privileges on *.* to " + "'" + user + "'" + "@" + "'" + host + "'" + " with grant option;" else: grant_user = "grant select on *.* to " + "'" + user + "'" + "@" + "'" + host + "'" + " with grant option;" show_grants = "show grants for " + "'" + user + "'" + "@" + "'" + host + "';" subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e ' + '"' + create_user + '"'], shell=True) subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e ' + '"' + grant_user + '"'], shell=True) subprocess.run(['/usr/local/mysql/bin/mysqladmin --login-path=' + conn_user + ' flush-privileges'], shell=True) subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e ' + '"' + show_grants + '"'], shell=True) return if choices == 'manager': create_mysql_user() elif choices == 'dev': create_mysql_user() else: print("\033[1;31m你輸入有誤!\033[0m")