I would like to automate the setup of a database and I would like to do it by using a python script to execute some commands in a linux terminal.
But I cannot see any way of executing commands in the terminal after connection to mysql database.
Below you can see a part of the script:
from time import sleep
from os import system
print("Setting up the database...\n")
system("sudo mysql -u root")
sleep(2)
This starts mysql, and after this none of the commands I try to execute from python get executed.
For example, I would like to run commands like these:
system("CREATE DATABASE mydatabase;")
system("CREATE USER 'user'#'localhost' IDENTIFIED BY '[PASSWORD]';")
system("GRANT ALL PRIVILEGES ON mydatabase.* TO 'user'#'localhost';")
Is this possible?
Edit 1: Potential solution as suggested by #AndHeFallen:
In python I create an sql file named db.sql, then run this in the terminal:
with open("db.sql","w") as db:
db.write("CREATE DATABASE mydatabase;\n")
db.write("CREATE USER 'user'#'localhost' IDENTIFIED BY '[PASSWORD]';\n")
db.write("GRANT ALL PRIVILEGES ON mydatabase.* TO 'user'#'localhost';\n")
system("sudo mysql -u root < db.sql")
I don't think it's possible to do that. If you want to interact with your MySQL database with a Python script, you'll need to connect directly to your db with an api like MySQLdb. (I may be wrong but the way you want to do may cause security issues)
Related
I have a SQL Server Agent job that executes some python scripts using CmdExec. Everything is set up with a proxy account as expected.
When I run the job I get:
Message
Executed as user: domain\proxyaccount. 'python' is not recognized as an internal or external command, operable program or batch file. Process Exit Code 1. The step failed.
I'm using Anaconda and Python is in the system PATH variable. When I run python from command line, it works. When I run python cutting and pasting the specific command from the job, it works. When I use runas to mimic the proxy account it works. The only place Python doesn't run is form inside the job.
What else do I need to look at to trouble shoot this issue?
You should restart SQL Server Agent after you installed Python on the server.
It is necessary for SQL Server Agent to load new environment variables, including the updated PATH with Python in it.
There are also suggestions to restart SQL Server too, but I believe restarting SQL Server Agent will be enough.
I am currently writing a program to help me with some database administration. It's supposed to execute commands in MariaDB. I thought since this is a CLI application I can just do that with os.system, but I'm having problems doing so. So lets say I have the following code
import os
os.system('mysql --user=%s --password=%s' %(user, password)
os.system('USE database;')
This code logs me into MariaDB, but it does not execute the second command to choose the database. Is this possible with os.system, and if not, what are my alternatives? Thanks.
Edit: I tried using subprocess instead, which gives me another problem: It immediately exits MariaDB after logging in.
I recomand you to use Subprocess instead of os python module, using Popen
try to check this link out :
Calling an external command in Python
all, I am trying to use Python to run Hive commands on the Hadoop Edge node. I went through all related questions on this website. But I still cannot solve it.
Currently, I can run hive after I ssh the server using terminal:
ssh user-admin#abc-hadoop-edge01.endor.lan
Then type hive, I am able to run hive commands.
However, I cannot run hive using python. I use pyhs2. My codes are as follows:
import pyhs2
conn = pyhs2.connect('abc-hadoop-edge01.endor.lan', port = 10000, user = 'user-admin', password = '125438a', database='default')
cursor = conn.cursor()
conn.close()
The error is: TTransportException: TTranspo...:10000
Is there anyone who knows how to solve this ?
BTW: I use Mac.
I am working on one of the MPP databases and would like to run a single SQL query using multiple sessions in python or UNIX shell script. Can somebody share your thoughts on spawning a SQL in python/UNIX utility. Any inputs would be appreciated. Thank you.
Code :-
for i in {1..$n}
do
( sh run_sql.sh test.sql touchstone_test & )
done
For python you could download the MySQLdb module. MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0 and is built on top of the MySQL C API. More info.
Depending on scale, you can choose either option.
If you have small tasks to accomplish, running a shell script is fine. Note that you can also pipe the query to the mysql CLI client, e.g.
mysql_cmd="mysql -h<host> -u<user> -p<pwd> <db>"
echo "SELECT name, id FROM myobjects WHERE ...." | $mysql_cmd
For a larger scale project, I would go with Python and the mysqldb interface that was mentioned already.
I'm running MySQL 5.1 under Windows 7. If I start the MySQL Command Line Client and type:
show databases;
it returns:
information_schema
aircraft_taxiing
dvd_collection
eqndb
mydb
mysql
test
test_db
all of which are in the directory in my.ini:
datadir="C:/ProgramData/MySQL/MySQL Server 5.1/Data/"
If I open a command prompt window and type mysql it only returns:
information_schema
test
test_db.
What happened to the rest of the databases? I've been trying to connect Python to MySQL using MySQLdb and can open any of the three databases, but none of the missing ones.
My goal is to make the Python connection in the end, but I'd like to understand what's going on at the command prompt, too.
It depends on the user you are logging in as. If your user doesn't have any priviledges on those databases, you won't be able to see them.