Writing MySQL Scripts with Python DB-API

[code lang=”python”]
import MySQLdb

conn = MySQLdb.connect (host = "localhost",
user = "testuser",
passwd = "testpass",
db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()
[/code]

ที่มา: kitebird.com/

MySQL change root password

mysqladmin command to change root password
If you have never set a root password for MySQL, the server does not require a password at all for connecting as root. To setup root password for first time, use mysqladmin command at shell prompt as follows:

$ mysqladmin -u root password NEWPASSWORD
However, if you want to change (or update) a root password, then you need to use following command

$ mysqladmin -u root -p’oldpassword’ password newpass
For example, If old password is abc, and set new password to 123456, enter:

$ mysqladmin -u root -p’abc’ password ‘123456’
Change MySQL password for other user
To change a normal user password you need to type (let us assume you would like to change password for vivek):

$ mysqladmin -u vivek -p oldpassword password newpass
Changing MySQL root user password using MySQL sql command
This is another method. MySQL stores username and passwords in user table inside MySQL database. You can directly update password using the following method to update or change password for user vivek:
1) Login to mysql server, type following command at shell prompt:

$ mysql -u root -p
2) Use mysql database (type command at mysql> prompt):

mysql> use mysql;
3) Change password for user vivek:

mysql> update user set password=PASSWORD(“NEWPASSWORD”) where User=’vivek’;
4) Reload privileges:

mysql> flush privileges;
mysql> quit
This method you need to use while using PHP or Perl scripting.
ที่มา: cyberciti.biz

Install MySQLdb for Python

Download and install MySQL package installer
Download MySQLdb mysql-python (1.2.3)
Install MySQLdb
ARCHFLAGS=’-arch x86_64′ python setup.py build
ARCHFLAGS=’-arch x86_64′ python setup.py install
If you get the error “mysql_config not found” follow the instructions here,
which are essentially this: Open up site.cfg from the binding distribution and
make sure it has this (the path to mysql_config should match the actual one
on your system):
mysql_config = /usr/local/mysql/bin/mysql_config

$ python -c “import MySQLdb”
Traceback (most recent call last):
File “<string>”, line 1, in <module>
File “build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py”, line 19, in <module>
ImportError: dlopen(./_mysql.so, 2): Library not loaded: libmysqlclient.16.dylib
Referenced from: /Users/phaisarn/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
Reason: image not found
สาเหตุคือ _mysql.so อ้างอิงไปที่ไฟล์ libmysqlclient.16.dylib แต่ไม่สามารถหาไฟล์นี้พบ
เนื่องจาก libmysqlclient.16.dylib เป็น MySQL client ดังนั้น จึงต้องตรวจสอบไปที่ MySQL
แก้ไข หาไฟล์ ibmysqlclient.16.dylib ว่าอยู่ที่ไหน ปรากฏว่าอยู่ที่/opt/local/lib/mysql5/mysql
ทำการตรวจสอบ DYLD_LIBRARY_PATH ด้วยคำสั่ง echo $DYLD_LIBRARY_PATH
ทำการ export DYLD_LIBRARY_PATH=/opt/local/lib/mysql5/mysql
เรียกคำสั่ง $ python -c “import MySQLdb” ก็จะไม่มี error เป็นอันเรียบร้อย

แต่ถ้าหาไฟล์ libmysqlclient.16.dylib ไม่เจอ ให้ใช้ MacPorts
http://www.macports.org/install.php
ทำการติดตั้ง MacPorts จากนั้นเรียกคำสั่ง
/opt/local/bin/port install py26-mysql
ก็จะได้ไฟล์ libmysqlclient.16.dylib มา

การให้ Python เรียก MySQLdb
import MySQLdb
conn = MySQLdb.connect(host=’localhost’,user=’xxx’,passwd=’xxx’,db=’mysql’, unix_socket=’/tmp/mysql.sock’)

ที่มา: birdhouse.orgstackoverflow.commacports.orgmangoorange.com (1.2.2)

การให้สิทธิใน MySQL

การให้สิทธิใน MySQL ใช้คำสั่ง Grant เช่น

[code]
grant all on *.* to jack@localhost identified by ‘xxxx’;
[/code]

เป็นการให้สิทธิทั้งหมดแก่ user jack@localhost

แต่ถ้าต้องการให้สิทธิบางส่วนเช่น ให้สิทธิเฉพาะฐานข้อมูลที่กำหนดให้ใช้คำสั่ง

[code]
grant all on mydb.* to jack@localhost identified by ‘xxxx’;
[/code]

Link

กำหนด Name และ Collate ในไฟล์ query ของ MySQL

การรัน MySQL query จากไฟล์ บางครั้งอาจข้อมูลที่ Import เข้าไปอาจกลายเป็นภาษาต่างดาว เนื่องจากรหัสอักขระที่เก็บไว้ในไฟล์ ดังนั้นให้แก้ไขได้โดยการกำหนดคำสั่งไว้ที่ด้านบนของไฟล์ดังนี้

[code lang=”sql”]
SET NAMES ‘utf8’ COLLATE ‘utf8_unicode_ci’;
[/code]