Difference between revisions of "MySQL notes"

From Noah.org
Jump to navigationJump to search
Line 5: Line 5:
  
 
== ~/.my.cnf ==
 
== ~/.my.cnf ==
 +
 
<pre>
 
<pre>
 
[mysql]
 
[mysql]
Line 50: Line 51:
  
 
== lost root password ==
 
== lost root password ==
 +
 +
This is how you can reset your root password if it is lost (you cannot recover the password).
  
 
Stop mysql server.
 
Stop mysql server.
Line 80: Line 83:
 
<pre>
 
<pre>
 
show full processlist\G
 
show full processlist\G
 +
</pre>
 +
 +
== show server status ==
 +
 +
This will show server status:
 +
 +
<pre>
 +
show status;
 +
</pre>
 +
 +
You can reset many of the counters like this:
 +
 +
<pre>
 +
flush status;
 +
</pre>
 +
== show run-time server config ==
 +
 +
This shows the state of the server as currently running:
 +
 +
<pre>
 +
show variables;
 +
</pre>
 +
 +
You can also set some run-time variables.
 +
 +
<pre>
 +
SET GLOBAL
 
</pre>
 
</pre>
  

Revision as of 16:35, 5 August 2008

MySQL isn't my first choice of database, but it isn't a bad choice either. I prefer PostgreSQL, but everyone uses MySQL.

\G on the end of a query for pretty output.

~/.my.cnf

[mysql]
database = my_favorite_database
user = noah
password = ze_zecret_passwort
host = localhost
auto-rehash=true
prompt="\\R:\\m:\\s[\d] mysql> "
#i-am-a-dummy

Create a user

This creates a new root level user named USERNAME with no password.

 $ mysql -u root -e "GRANT ALL ON *.* to USERNAME@localhost;"
 $ mysql -u root -e "GRANT ALL ON *.* to USERNAME@'%';"

This creates a new user with a password.

 $ mysql -u root -e "GRANT ALL ON *.* to USERNAME@localhost IDENTIFIED BY 'some_pass' WITH GRANT OPTION;"
 $ mysql -u root -e "GRANT ALL ON *.* to USERNAME@'%' IDENTIFIED BY 'some_pass' WITH GRANT OPTION;"

Error mixing old MySQL client with newer MySQL server

If you are using an old MySQL client to connect to a later version of MySQL you may get an error:

 Client does not support authentication protocol requested by server; consider upgrading MySQL client

This happens when using a MySQL client prior to version 4.1 with a server later than version 4.1. The best thing to do is to upgrade the client. If that cannot be done then set the password on the server to use the old password hash:

 SET PASSWORD FOR 'USERNAME'@'some_host' = OLD_PASSWORD('some_pass');

create root user directly

Some automatic installations will install without creating a root user. If you start mysqld with 'skip-grant-tables' then it won't allow you to create or update a user. You can still create the root user directly by inserting into the mysql.user table. This will create a root user and set the password to "password". After running this INSERT statement you should restart mysqld.

INSERT INTO `user` VALUES ('localhost','root','*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0),
('%','root','*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0);

lost root password

This is how you can reset your root password if it is lost (you cannot recover the password).

Stop mysql server.

 /etc/init.d/mysql stop

Edit /etc/my.cnf or /etc/mysql/my.cnf and add this line anywhere after the [mysqld] section:

 skip-grant-tables

Start mysql server:

 /etc/init.d/mysql start

Now you can connect without a password. Grant commands won't work. You can reset the password by directly updating the User table:

 mysql> use mysql
 mysql> update mysql.user set Password=password('newpassword') where User='root';

Remove skip-grant-tables from /etc/my.cnf.

Restart mysql server:

 /etc/init.d/mysql restart

show process info on server

See what is locking and blocking other sessions on the server.

show full processlist\G

show server status

This will show server status:

show status;

You can reset many of the counters like this:

flush status;

show run-time server config

This shows the state of the server as currently running:

show variables;

You can also set some run-time variables.

SET GLOBAL

select into file

I always forget this:

SELECT * FROM foo INTO OUTFILE '/tmp/foo.sql';

select into CSV file

SELECT * FROM foo INTO OUTFILE '/tmp/foo.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n';

load table from file

LOAD DATA INFILE '/tmp/foo.csv' INTO TABLE foo FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n';

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()