MySQL Administration

How to check show-processlist-every-second ?

mysqladmin -u root -p -i 1 processlist

-u username
-p you will be promted for a password
-i 1 interval one second

Monitor MySQL SQLs

single command:

mysql -u root -proot -e"show full processlist;"

loop command:

perl -e 'while(1) { print "---------------------------------------------------------------------------------n"; print `mysql -uneoxadmin -pneoxadmin -e"show full processlist;"`; sleep(1);};'

How to examine and kill MySQL client processes


Listing A

| Id | User | Host | db | Command | Time | State | Info |
| 27 | root | | NULL | Query | 0 | NULL | show processlist |
| 40 | admin | | db1 | Sleep | 0 | | NULL |
| 41 | root | | db1 | Sleep | 1 | | NULL |
3 rows in set (0.11 sec)

mysql> KILL 27;

Query OK, 0 rows affected (0.05 sec)

To kill the query being executed by a thread but leave the connection active (yes, MySQL even allows such fine-grained control),
use the KILL QUERY command instead, followed by the appropriate thread ID.

How to Restore MySQL’s root password ?

Stop mysql service

# /etc/init.d/mysql stop

Start in safe mode w/o password

# mysqld_safe --skip-grant-tables &

Connect to mysql server using mysql client:

# mysql -u root

Setup new MySQL root user password

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

Stop MySQL Server:

# /etc/init.d/mysqld stop
# service mysqld stop

Start MySQL server and test it

# /etc/init.d/mysql start
# mysql -u root -p



How to repair the table ?

Mysql Error : ERROR 145 (HY000): Table ‘./databasename/tablename’ is marked as crashed and should be repaired

Solution : Run Following Query (Depending on how big the table is, this may take a long time.)

repair table tablename;

Output of Query:

| Table                       | Op     | Msg_type | Msg_text |
| databasename.tablename      | repair | status   | OK       |

Command for Mysql Backup
mysqldump -u username -p databasename > backup_filename.sql

Query To Know DataBase Engine
show table status from ;
The engine column in the result will give the database engine.

Configure Read Only User in MySQL
Connect to your database as root, then add your new user like so:

 CREATE USER 'tester'@'%' IDENTIFIED BY 'password';

The % here means the user ‘tester’ connecting from any host, you can place a network hostname here instead if you want to restrict access further. Naturally you will also want to substitute password with something a little stronger 😉

Now run the following to grant the SELECT privilege to the new user on all databases:

 GRANT SELECT ON *.* TO 'tester'@'%';

Or if you want to restrict access to only one database:

 GRANT SELECT ON database.* TO 'tester'@'%';

Connect as the new user:

 mysql -u tester -p

And now see if you can update a record:

mysql> use alpha;
mysql> update Person set version_num = 5 where OID = 1;
ERROR 1142 (42000): UPDATE command denied to user 'tester'@'localhost' for table 'Person'

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.