บันทึกของวีร์ | Vee(r)'s Blog

เมษายน 3, 2012

ลง mysql บน macports แบบมั่วๆ

Filed under: Deep geeky — ป้ายกำกับ:, , — वीर @ 9:59 pm

ผมลงแล้วเจ๊งอยู่หลายรอบ ท่านี้อาจจะไม่่ค่อยดีแต่ก็ใช้ได้แล้ว

sudo port install mysql5
sudo port install mysql5-server

sudo mysql_install_db5
chown -R _mysql:_mysql /opt/local/var/db
chown -R _mysql:_mysql /opt/local/var/run
chown -R _mysql:_mysql /opt/local/var/log

sudo -u _mysql /opt/local/libexec/mysqld –skip-grant-table &

mysql -uroot

ใน mysql client สั่ง
use mysql;
update user set password=PASSWORD(‘password ที่จะตั้ง’) where user=’root’;
flush privileges;

(ออกจาก client)

kill mysqld ทิ้งซะ

สั่ง sudo port load mysql5-server

เสร็จแล้ว

มกราคม 27, 2011

แก้ mysql ให้ใช้ UTF-8 ใน my.ini/my.cnf

Filed under: Deep geeky — ป้ายกำกับ:, , , , , — वीर @ 1:42 am

ผมก็เอามาแปะเลยแล้วกันนะ

[client]
default-character-set=utf8

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
collation_server=utf8_unicode_ci
character_set_server=utf8
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

ผมเพิ่มบรรทัดที่มี utf8 เข้าไป

พฤศจิกายน 28, 2009

MySQL: insert ถ้ายังไม่มี

Filed under: SQL — ป้ายกำกับ:, , , , — वीर @ 1:48 am

ปกติใน MySQL (DBMS อื่นๆ ก็คงเหมือนกัน) ถ้าเราไป insert อะไรซ้ำๆ เข้าไปใน column ที่บังคับไว้ว่าต้อง unique มันก็จะเจ๊ง ก็เลยต้องมาดูก่อนว่ามีข้อมูลแล้วหรือยัง ค่อย insert แต่ว่าจะเขียน SQL อย่างไรให้สั้นๆ ง่ายๆ เท่าที่อ่านมาจาก http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html ผ่านทาง http://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql มาอีกที

ผมเลือกใช้ “on duplicate key update” เพิ่มใน insert เพราะเท่าที่อ่านมา 3 วิธีก็ วิธ๊นี้ก็ดูประหยัด และไม่ไปเพิกเฉยต่อ error อื่น ที่ไม่เกี่ยวข้อง

สมมุติว่าผมจะเพิ่ม 6 เข้าไปใน textunit_conflicts.textunit_id ก็เขียนแบบนี้

insert into textunit_conflicts value (6) on duplicate key update textunit_id = textunit_id;

textunit_id = textunit_id ก็เขียนไปไม่ให้มันผิด syntax เฉยๆ

มีนาคม 5, 2009

AuthKit + OpenID + MysqlStore

Filed under: old posts — ป้ายกำกับ:, , , , , , , , — वीर @ 2:05 pm

In previous examples, I always use file store, which I found that it is quite difficult to use when I deploy BasaAsa to the server so I change to use MySQL store. To use MySQL store, I modified websetup.py in order to create table for OpenID library.

websetup.py:

"""Setup the basaasa application"""
import logging

from basaasa.config.environment import load_environment
from basaasa.users.authkit_elixir_driver import UsersFromDatabase
from basaasa import model
from authkit.authenticate.open_id import make_store #added
from pylons import config #added

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup basaasa here"""
    load_environment(conf.global_conf, conf.local_conf)
    from elixir import metadata
    metadata.create_all(checkfirst=True)
    store_type = config.get('authkit.openid.store.type') #added
    stote_config = config.get('authkit.openid.store.config') #added
    conn, cstore = make_store(store_type, stote_config) #added
    if store_type == 'mysql': #added
        cstore.createTables() #added

Then after I run paster setup-app development.ini, tables for storing OpenID information have been created.

กุมภาพันธ์ 5, 2009

แก้ปัญหา Elixir + Mysql

Filed under: old posts — ป้ายกำกับ:, , , , , , — वीर @ 2:14 pm

จริงๆ ก็เป็นปัญหาเดิม แต่ตอนรีบๆ มากๆ แล้วจะหาไม่เจอ. ปกติผมใช้ SQLite แล้วค่อยไปใส่ MySQL อีกที. ใน Model สำหรับ SQLite ใส่อะไรประมาณนี้

class Document(Entity):
    body = Field(Unicode)

ก็ใช้ได้แต่พอเอาใส่ mysql แล้วมันจะแปลง Unicode เป็น varchar ก็จะเจ๊งทันที แก้ง่ายๆ เป็น

class Document(Entity):
    body = Field(Unicode(10000000))

ก็ใช้ได้แล้ว :-P

พฤศจิกายน 24, 2008

Python + MySQL + UTF-8

Filed under: old posts — ป้ายกำกับ:, , , , , , , — वीर @ 1:27 pm

import MySQLdb as db
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
conn = db.connect(host="localhost", user="root",
                  passwd="", db="kulex",
                  use_unicode=True, charset="UTF8")
c = conn.cursor()
c.execute("SELECT * FROM `Classifier`")
while True:
    row = c.fetchone()
    if row == None:
        break
    for col in row:
        print col
c.close()
conn.close()

สิงหาคม 12, 2008

ใช้ InnoDB ใน Elixir

Filed under: old posts — ป้ายกำกับ:, , , , , — वीर @ 5:16 pm

class Corpus(Entity):
    name = Field(Unicode(255))
    using_table_options(mysql_engine='InnoDB')

เมษายน 25, 2008

CakePHP 1.2 + ภาษาไทย + UTF-8 + MySQL

Filed under: old posts — ป้ายกำกับ:, , , , , , , , , , , , , , , , — वीर @ 4:34 pm

default encoding ของ MySQL เท่าที่ผมใช้ไม่ได้เป็น UTF-8. เวลาใช้ CakePHP ก็เลยต้องแก้ configuration นิดหน่อยเพื่อทำให้ ใช้ UTF-8 และ MySQL ได้เนียนๆ. ใน CakePHP รุ่นก่อนๆ หน้านี้บางทีก็ต้องไปแก้ AppModel ที่ไม่ค่อยเท่เท่าไหร่ เพราะน่าจะต้องมาแก้อีกเวลา port ไปใช้ database ตัวอื่นที่ไม่ใช่ MySQL.

ใน Cake 1.2.x สามารถตั้งค่า encoding/charset แบบรวมศูนย์ได้ใน app/config/database.php เลย. ตามตัวอย่างแบบด้านล่าง

class DATABASE_CONFIG {

    var $default = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'your_username',
        'password' => 'your_password',
        'database' => 'my2',
        'prefix' => '',
        'encoding' => 'UTF8' #ดูบรรทัดนี้เป็นสำคัญ
    );

    var $test = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'user',
        'password' => 'password',
        'database' => 'test_database_name',
        'prefix' => '',
    );
}
?>

เพื่อม ‘encoding’ => ‘UTF8′ เข้าไปก็ทำให้ใช้ภาษาไทยและ UTF-8 ได้เนียนๆ แล้ว.

พอมาเปิดใน phpmyadmin ที่ตั้งค่าให้ใช้ UTF-8 และภาษาไทย

ก็แสดงผลออกมาได้ถูกต้อง.

สรุปว่าถ้าอยากใช้ UTF-8 กับ MySQL ใน CakePHP 1.2.x ก็เข้าไปตั้งค่าได้ใน app/config/database.php

อ้างอิง

Theme: Shocking Blue Green. บลอกที่ WordPress.com .

Follow

Get every new post delivered to your Inbox.

Join 632 other followers