简体   繁体   English

在python中执行mysql命令时出现错误

[英]Getting error while executing mysql command in python

I am writing a python3 script to automate installation of radius and daloradius. 我正在编写一个python3脚本来自动安装radius和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

There are two many single quotemarks in the SQL statement, use back slash to escape them: SQL语句中有两个单引号,请使用反斜杠将其转义:

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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM