MySQL notes

From Noah.org
Revision as of 15:22, 30 April 2007 by Root (talk | contribs)
Jump to navigationJump to search

create a user

This create a new root level user named USERNAME.

 mysql -u root -e "grant all on *.* to USERNAME@localhost;"
 mysql -u root -e "grant all on *.* to USERNAME@'%';"

dump shared memory files

It is pretty easy to read directly from MySQL memmapped files. This can be handy when you need to hack a search through data files rather than doing a proper query. Sometimes it's faster to dump data this way.

#!/usr/bin/env python

import mmap
import os, sys, string

def hex_filter (b, offset, block_size = 8):
    """Any unprintable characters are converted to line feeds, but
    only one line feed is printed per cluster of unprintable characters.
    """
    unprintable_flag = True # used to track unprintable chunk state.
    for i in xrange (offset, offset+block_size):
        #if b[i] in string.printable:
        if ord(b[i])>=ord(' ') and ord(b[i])<=ord('~'):
            sys.stdout.write("%s"%b[i])
            unprintable_flag = False
        else:
            if not unprintable_flag:
                sys.stdout.write("\n")
            unprintable_flag = True

def main ():
    filename="/var/lib/mysql/var/test/rmscrub.MYI"

    file = open(filename, "rb")
    size = os.path.getsize(filename)
    data = mmap.mmap(file.fileno(), size, access=mmap.ACCESS_READ)

    print "Note that 'data' is a memmapped file and 'size' is taken from the memmapped file size,"
    print "so len(data) and size should be the same:"
    print "len(data):", len(data)
    print "size:     ", size

    block_size = 2048
    size = 1000000
    for i in xrange(0,size, block_size):
        hex_filter(data,i, block_size)

main()