Wednesday, 27 March 2013

More MySql tips

dump db

mysqldump --host='127.0.0.1' --user=root --password='passwd' --column-statistics=0  db_name>dw.sql

redirect result to a csv file

select * from usermanager_users
 into outfile '/tmp/test.csv' fields terminated by ',' enclosed by '"' 
 lines terminated by '\n';

redirect result on command line

mysql -u [username ] -p[password] [db_name] -e " SELECT * FROM [tbl_name] " > file_name

Case sensitive compare

#Will match first name Mary, but not match mary
SELECT first_name FROM  `name` WHERE first_name  REGEXP BINARY 'Mary';

Show stored procedures

SHOW PROCEDURE STATUS WHERE Db = 'db_name'\G

# show details of procedure
SHOW CREATE PROCEDURE procedure_name\G

Create a mysql user

CREATE USER lshen@'10.18.276.170' IDENTIFIED   BY 'eikdkdk123scx';

select * from mysql.user where User="lshen"\G

GRANT SELECT  ON *.* TO lshen@'10.18.276.170';

FLUSH PRIVILEGES;

Query Cache

If swap between different MySQL versions, cache may cause problem. For example, they may use the different end character. Therefore, need to flush cache.

#To check whether the query cache is present in your MySQL server
mysql> SHOW VARIABLES LIKE 'have_query_cache';

#To monitor query cache performance
mysql> SHOW STATUS LIKE 'Qcache%';

#removes all query results from the query cache
mysql> RESET QUERY CACHE ;