su - postgres psql -d template1 \l -> list databases \d -> list tables \d-> show table description \c -> connect to database \q -> quit
Category Archives: MySQL / PostgreSQL
Migrating from a mysql DB to WordPress
My wife had a website of a recipe DB. I wanted to migrate it to WordPress automatically.
#!/usr/bin/python # -*- coding: utf-8 -*- import MySQLdb import datetime, xmlrpclib import sys import string wp_url = "http://www.website.com/wordpress/xmlrpc.php" wp_username = "user" wp_password = "password" wp_blogid = "" html_escape_table = { "&": "&", '"': """, "'": "'", ">": ">", "<": "<", "’": "'", } def html_escape(text): """Produce entities within text.""" return "".join(html_escape_table.get(c,c) for c in text) db = MySQLdb.connect(host="localhost", # your host, usually localhost user="user", # your username passwd="password", # your password db="oohaheats") # name of the data base # you must create a Cursor object. It will let # you execute all the query you need cur = db.cursor() server = xmlrpclib.ServerProxy(wp_url) cur.execute("SELECT id, name, created_at from recipes where active = 1"); recipe_rows = cur.fetchall() for row in recipe_rows : recipe_id = row[0] recipe_name = row[1] date_start = row[2] categories = [] title = html_escape(recipe_name) date_created = xmlrpclib.DateTime(date_start) status_draft = 1 status_published = 0 # Load categories cur.execute("select name from categories where id in (select category_id from categorizations where recipe_id = " + str(recipe_id) + ")") category_rows = cur.fetchall() for c_row in category_rows: categories.append(c_row[0]) #print categories.size() tags = categories # Take care of ingredients. cur.execute("SELECT measurement_id, grocery_id from ingredients where recipe_id = " + str(recipe_id) + " order by order_num") ingredient_rows = cur.fetchall() content = "<strong>Ingredients</strong>\n" content += "<ul>\n" for i_row in ingredient_rows: measurement_id = i_row[0] grocery_id = i_row[1] grocery_name = "" measurement_name = "" content += "<li>" cur.execute("SELECT name from measurements where id = " + str(measurement_id)) measurement_rows = cur.fetchall() for m_row in measurement_rows: measurement_name = m_row[0] content += html_escape(measurement_name) + " " cur.execute("SELECT name from groceries where id = " + str(grocery_id)) grocery_rows = cur.fetchall() for g_row in grocery_rows: grocery_name = html_escape(g_row[0]) content += html_escape(grocery_name) content += "</li>\n" content += "</ul>\n" content += "<div><strong>Steps</strong></div>\n" content += "<ol>\n" cur.execute("select instruction from steps where recipe_id = " + str(recipe_id) + " order by order_num") steps_rows = cur.fetchall() for s_row in steps_rows: instruction = s_row[0] #print instruction if len(instruction) > 0 and not all(c in string.whitespace for c in instruction): content += "<li>" + html_escape(instruction) + "</li>\n" content += "</ol>\n" content += "</div>\n" data = {'title': title, 'description': content, 'dateCreated': date_created, 'categories': categories, 'mt_keywords': tags} try: post_id = server.metaWeblog.newPost(wp_blogid, wp_username, wp_password, data, status_published) except Exception, e: print("ERROR POSTING " + str(recipe_id) + " = " + recipe_name) print str(e) print "-----------------------------" sys.exit(1)
mysql import dump
[user@userbox]/Users/user/Downloads/mysql>mysql -u myuser -p -h localhost dbname < ~/Downloads/dbname.sql Enter password:
MySQL – Create User
mysql> create database dbname; Query OK, 1 row affected (0.02 sec) mysql> grant all privileges on dbname.* to 'username'@'%' identified by 'password'; Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on dbname.* to 'username'@'localhost' identified by 'password'; Query OK, 0 rows affected (0.00 sec)
MySQL – Clone DB
mysqladmin create <new_db_name> -u <user> --password=<password> && mysqldump -u <user> --password=<password> <db_name> | mysql -u <user> --password=<password> -h localhost <new_db_name>