MySQL missing databases - python

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.

Related

Execute mysql commands in linux terminal using Python

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)

How to access (local) mysql database with python on amazon ubuntu linux

So I just set-up an ubuntu server on amazon and installed mysql. It is working with:
mysql -u root -p
And then the mysql_password. I have added via command line a database and tables. But now I wonder how I can access this database with python locally (and eventually remotely).
If I try locally something like:
db = MySQLdb.connect(host="localhost", user="root", passwd="mysql_password", db="dbname")
I get the following error:
_mysql_exceptions.OperationalError: (1049, "Unknown database 'dbname'")
However, the database dbname does exist at /var/lib/mysql. So how to access this via python (first locally and eventually remotely)?
Solved it, it was a spelling mistake in dbname

Peewee can't connect to MySQL on Remote Host

I'm using Python 3.5 + Spyder 2 (from Anaconda) on Windows 10. I have an Ubuntu 16.04 desktop machine which is running a MySQL server on a LAN addressable IP. PhpMyAdmin works fine with this remote server. However, every time I attempt to connect to this server from my Windows 10 Spyder I get the following error:
OperationalError: (1045, "Access denied for user 'root'#'10.0.0.30' (using password: YES)")
The command I'm using is:
import os
from peewee import *
from playhouse.db_url import connect
db = MySQLDatabase('test', host="10.0.0.80", port=3306, user='root', passwd='*********')
db.connect()
The IP of the machine I'm using to make the call is 10.0.0.30 shown in the Error. I originally tried it before the "test" db existed. Then I created the "test" db using PhpMyAdmin. I then tested again. Same error. I have created a table in the "test" db via PhpMyAdmin to confirm that root has the appropriate privileges (which wasn't really a question but I wanted confirm).
I have downloaded and installed what I could find via several StackOverflow questions prior to posting this question. I have also rebooted to make sure that any new drivers and such that I installed were actually up and running.
Note my issue is not the same as this one:
Peewee - Can't connect to MySQL server on host
My connection error shows that despite using the host argument and setting it to "10.0.0.80" that peewee is still trying to connect to 10.0.0.30. IP 10.0.0.30 is the Windows machine I'm running peewee from while 10.0.0.80 is the machine I'm attempting to connect to.
TIA

Setting up psycopg2 for the first time, how do you configure it for some test data bases?

This is my first time using PostgreSQL, I'm used to using MySQL where once you install MySQL server on Ubuntu for the first time, it'll ask you for a MySQL username and password. Once that's finished you can connect to it simply by providing:
MySQL Host: 127.0.0.1
Username: root
Password: 123pie
And from there you're ready to open up your favorite DB software like sequel pro and connect to it via SSH using the localhost IP of MySQL like shown above.
However right now my client is using PostgreSQL and so I need to learn how to set it up fast.
So far I've done this:
pip install psycopg2==2.4.5
Looks like this is just a library required for Django and Python to open up a PostgreSQL database. It doesn't look like something identical to MySQL Server.
Is installing PostgreSQL Server the next thing that I have to do? After I install that via the command line, is it exactly the same as setting up MySQL server?
I've never used PostgreSQL, I expect that it's very identical to using MySQL?
There's a nice, detailed guide on the Ubuntu-specific setup for PostgreSQL on the ubuntu community wiki. That should be your starting point.
For most applications it is sufficient to create a PostgreSQL user for the web server's username:
sudo -u postgres createuser www-data
then create a database owned by that user for the app to use:
sudo -u postgres createdb -O www-data test_django
and configure the app to connect with username www-data to the test_django database with no password. PostgreSQL on most distros, including Ubuntu, defaults to peer authentication where it requires you to have the same unix username as the postgres user you're connecting as, and doesn't require a password.
If the app forces you to supply a password and won't accept a blank one, or if you want the app to use a username different to the user that the web server runs as (i.e. to isolate multiple web apps from each other a bit), you need to add an entry to pg_hba.conf specifying md5 password authentication for that database/username combo - or just all users for all dbs.

How to open psql prompt for PostgreSQL in FreeBSD?

I have installed PostgreSQL on my FreeBSD 8.2 box. How can I open the SQL prompt so I can write SQL queries?
I can do:
su pgsql
Which opens a prompt but I cannot write SQL queries like SELECT or CREATE there. Sorry, I am new to PostgreSQL.
I use django's management command python manage.py dbshell to open the database-specific connection (same across dev / production).
It does other useful things like specifying the database and username listed in settings.py
Otherwise I'd have to type in something like psql db_name -U user
Don't you have to connect to an existing database first, before making queries, on PostgreSQL?
(BTW are you sure it's pgsql the command you want, not psql?)
If that's really your problem, you can connect to an existing database either when you open the shell:
psql mydatabase
Or after using the \c command:
psql
myuser=> \c mydatabase

Categories