Getting error while executing mysql command in python

250 views Asked by At

I am writing a python3 script to automate installation of radius and daloradius. in the middle, am getting some error. my code is :

import subprocess
import os
import sys

# Updating Resources
os.system("sudo apt-get update -y")


#subprocess.call(["apt-get", "install", "freeradius"])

#Installing Radius & Dependencies
print("\n Installing Free Radius ... \n")
os.system("sudo apt-get install freeradius freeradius-mysql -y")
print("\nInstalling  LAMP  ... \n")
os.system("sudo apt-get install mysql-server mysql-client apache2 php5 libapache2-mod-php5 php5-mysql php5-common php5-gd php-pear php-db php-mail -y")
#os.system("sudo apt-get install python-dev libmysqlclient-dev python3-pip")
#os.system("sudo pip3 install mysqlclient")

#Creating A Database for radius server
os.system("mysql -h localhost -uroot -p123 -e 'CREATE DATABASE foo';")
os.system("mysql -h localhost -uroot -p123 -e 'CREATE USER 'radius2'@'localhost' IDENTIFIED BY 'radpass'';")
os.system("mysql -h localhost -uroot -p123 -e 'GRANT ALL PRIVILEGES ON `radius` . * TO 'radius2'@'localhost'';")

I am getting an error at

os.system("mysql -h localhost -uroot -p123 -e 'CREATE USER 'radius2'@'localhost' IDENTIFIED BY 'radpass'';")

The error is :

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'radpass' at line 1
1

There are 1 answers

0
hongshuning On

There are two many single quotemarks in the SQL statement, use back slash to escape them:

os.system("mysql -hlocalhost -uroot -p123 -e 'CREATE USER \'radius2\'@\'localhost\' IDENTIFIED BY \'radpass\';'")

You can use just one statement to create user and grant privilege to it:

GRANT ALL PRIVILEGES ON radius.* TO 'radius2'@'localhost' IDENTIFIED BY 'radpass';