Well…
Hurry up! Time to begin a tutorial about databases using linux and mysql. I will try to put some python code together, maybe some tools to do basic operations like create database, give some permissions, create tables and the most important: create a sql script to migrate databases to another server. lets keep in touch, I will post that script very soon.
– First basic operation to do with mysql – access the ‘mysql’ command prompt, to work with mysql.
At linux shell:
mysql -u user -p
Where:
user- Its a user to connect on mysql server
-p – Its a parameter to prompt for password
E.g. mysql -u gabriel -p
Please pay attention: The user and password maybe its not the same of /etc/passwd. Actually, in first startup of mysql service, mysql ask to you a new password of root, normally blank. NEVER LEAVE BLANK PASSWORD FOR THE ROOT USER.
– WORK WITH DATABASES
use name_of_database
– Creating databases
CREATE DATABASE name_of_database;
E.g. CREATE DATABASE gabriel;
– Granting basic permissions
GRANT <PRIVILEGES> ON OBJECT.object TO user@<HOST> IDENTIFIED BY ‘password’;
Where:
– TYPE OF PRIVILEGES
Privilege Description
ALL/ALL PRIVILEGES All the basic permissions
ALTER Permission to run ALTER statements
CREATE Permission to CREATE tables or databases
CREATE TEMPORARY TABLES Permission to run CREATE TEMPORARY TABLE statements
DELETE Permission to run DELETE statements
DROP Permission to DROP tables or databases
EXECUTE Permission to run stored procedures (in MySQL 5)
FILE Permission to read and write files (e.g. LOAD DATA INFILE statements)
GRANT Permission to GRANT available permissions to other users
INDEX Permission to create, change or drop indexes
INSERT Permission to run INSERT statements
LOCK TABLES Permission to LOCK tables which the user has SELECT access to
PROCESS Permission to view or kill MySQL processes
REFERENCES Currently unused
RELOAD Permission to reload the database (e.g. FLUSH statements)
REPLICATION CLIENT Permission to ask about replication
REPLICATION SLAVE Permission to replicate from the server
SHOW DATABASES Permission to see all databases
SELECT Permission to run SELECT statements
SHUTDOWN Permission to SHUTDOWN the MySQL server
SUPER Permission to connect, even if the number of connections is exceeded, and perform maintenance commands
UPDATE Permission to run UPDATE statements
USAGE Permission to connect and and perform basic commands only
– OBJECT
*.* All tables in a database
* All tables in the current database
dbname.* All tables in the named database
dbname.tbname The named table in the named database
E.g. GRANT ALL ON gabriel.* TO gabriel@localhost IDENTIFIED BY ‘mypassword’;
– HOST
Its the host with permissions to connect to database(can be an IP, and hostname, and normally to webservers with mysql is localhost)
– Import sql script file
mysql -u user -p < script.sql
Where:
user- Its a user to connect on mysql server
-p – Its a parameter to prompt for password
< - Put on(for example, execute the sql script inside mysql)
script.sql- A file with Transact SQL, like INSERT, CREATE …
Hint: a lot of sql scripts file don´t have basic instructions in the begining, like USE database. If you aren’t using a database, its not possible to create tables, for example.
E.g.*:
NORMAL FILE
CREATE TABLE xxxx(X,Y,Z)
MODIFY THE FILE TO:
USE X;
CREATE TABLE xxxx(X,Y,Z)
——– Its enough for now. Later I will publish more about MySQL. If you have a post about PostgreSQL, please let me know using the comments. ——–