Generate MySQL Settings for Wordpress


/ Published in: Python
Save to your folder(s)

This script was written to handle the creation of a wordpress database and provide the relevant output for wp-config.php.


Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2. import string
  3. import random
  4. import sys
  5. import MySQLdb
  6.  
  7. # pass_gen will generate a random string of characters
  8.  
  9.  
  10. def pass_gen(size=10,
  11. chars=string.ascii_uppercase +
  12. string.ascii_lowercase +
  13. string.digits):
  14. return ''.join(random.choice(chars) for x in range(size))
  15.  
  16.  
  17. def main(argv):
  18. sitename = argv[0]
  19.  
  20. '''
  21. Generate semi-random names
  22. Random number is based off of 16 minus the length of databse name given
  23. '''
  24.  
  25. db_name = sitename + pass_gen(16 - len(sitename))
  26. db_user = sitename + pass_gen(16 - len(sitename))
  27. db_password = sitename + pass_gen(16 - len(sitename))
  28.  
  29. # Create the database with the settings
  30. db = MySQLdb.connect(host='localhost', user='root', passwd='Superb33')
  31. cur = db.cursor()
  32.  
  33. create_db_sql = 'CREATE DATABASE %s' % (db_name)
  34. grant_user_sql = 'GRANT ALL PRIVILEGES ON %s.* to "%s"@"localhost" \
  35. identified by "%s"' % (db_name, db_user, db_password)
  36. cur.execute(create_db_sql)
  37. cur.execute(grant_user_sql)
  38.  
  39. # Concatnate DB Strings
  40. db_name_string = "/** The name of the database for WordPress */\
  41. \ndefine('DB_NAME', '%s');\n" % (db_name)
  42. db_user_string = "/** MySQL database username */\
  43. \ndefine('DB_USER', '%s');\n" % (db_user)
  44. db_pass_string = "/** MySQL database password */\
  45. \ndefine('DB_PASSWORD', '%s');\n" % (db_password)
  46.  
  47. # Print the result
  48. print "Copy and paste this to the wp-config.php settings file\n"
  49. print db_name_string
  50. print db_user_string
  51. print db_pass_string
  52.  
  53. if __name__ == "__main__":
  54. main(sys.argv[1:])

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.