Skip to main content

Some basic MySQL commands for system administrators



# HOW TO CREATE A USER IN MYSQL
mysql> CREATE USER 'wordpress_user'@'localhost' identified by 'password';
Query OK, 0 rows affected (0.00 sec)


# CREATE A DATABASE
mysql> CREATE DATABASE LINUXAVID;
Query OK, 1 row affected (0.00 sec)


# DROP/DELETE A DATABASE

mysql> drop database LINUXAVID;
Query OK, 0 rows affected (0.00 sec)



#CHECK THE PROCESS LIST

root@cpanel [~]# mysqladmin proc
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
| Id    | User      | Host      | db        | Command | Time | State | Info             |
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
| 46457 | eximstats | localhost | eximstats | Sleep   | 8564 |       |                  |
| 47181 | root      | localhost |           | Query   | 0    |       | show processlist |
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
#CHECK PROCESS LIST STATUS AND EXTENDED STATUS
 mysqladmin processlist extended-status
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
| Id    | User      | Host      | db        | Command | Time | State | Info             |
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
| 46457 | eximstats | localhost | eximstats | Sleep   | 8657 |       |                  |
| 47186 | root      | localhost |           | Query   | 0    |       | show processlist |
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
+-----------------------------------+----------+
| Variable_name                     | Value    |
+-----------------------------------+----------+
| Aborted_clients                   | 10       |
| Aborted_connects                  | 27418    |
| Binlog_cache_disk_use             | 0        |
| Binlog_cache_use                  | 0        |
| Bytes_received                    | 6750461  |
| Bytes_sent                        | 14672974 |
| Com_admin_commands                | 3073     |
| Com_assign_to_keycache            | 0        |
| Com_alter_db                      | 0        |
| Com_alter_db_upgrade              | 0        |
| Com_alter_event                   | 0        |
| Com_alter_function                | 0        |
| Com_alter_procedure               | 0        |
| Com_alter_server                  | 0        |
| Com_alter_table                   | 0        |

# CHECK THE STATUS AT 5 SECONDS INTERVAL

root@cpanel [~]# mysqladmin -i 5 proc status
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
| Id    | User      | Host      | db        | Command | Time | State | Info             |
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
| 46457 | eximstats | localhost | eximstats | Sleep   | 8791 |       |                  |
| 47195 | root      | localhost |           | Query   | 0    |       | show processlist |
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
Uptime: 900686  Threads: 2  Questions: 128791  Slow queries: 0  Opens: 59  Flush tables: 1  Open tables: 52  Queries per second avg: 0.142

+-------+-----------+-----------+-----------+---------+------+-------+------------------+
| Id    | User      | Host      | db        | Command | Time | State | Info             |
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
| 46457 | eximstats | localhost | eximstats | Sleep   | 8796 |       |                  |
| 47195 | root      | localhost |           | Query   | 0    |       | show processlist |
+-------+-----------+-----------+-----------+---------+------+-------+------------------+
Uptime: 900691  Threads: 2  Questions: 128887  Slow queries: 0  Opens: 59  Flush tables: 1  Open tables: 52  Queries per second avg: 0.143

+-------+-----------+-----------+-----------+---------+------+-------+------------------+
| Id    | User      | Host      | db        | Command | Time | State | Info             |
+-------+-----------+-----------+-----------+---------+------+-------+-------------

# DUMP OR TAKE THE BACKUP OF A DATABASE AS .sql

root@cpanel [~]# mysqldump wordpress > word.sql
root@cpanel [~]# ll word.sql
-rw-r--r-- 1 root root 385228 Jul 24 02:46 word.sql


# IMPORT DATABASE 

$ mysql -h <host> -u <username> <nameOfDatabase> -p < /path/to/file

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON 
<dbname>.* TO <dbuser@localhost> [IDENTIFIED BY '<password>'];

REVOKE ALL ON <dbname> FROM <dbuser@localhost>;

CREATE DATABASE <dbname>;a

DROP DATABASE <dbname>;

DROP TABLE <tablename1[, table2, table3...]>;

#TO ACTIVATE THE PERMISSION

FLUSH PRIVILEGES; 

USE <nameOfDatabase>;

SHOW DATABASES;

#SHOW TABLES BEGINNING WITH PREFIX
mysql> show tables like 'wp_use%';( where wp_use is the prefix I have searched)
+-------------------------------+
| Tables_in_wordpress (wp_use%) |
+-------------------------------+
| wp_usermeta                   |
| wp_users                      |
+-------------------------------+
2 rows in set (0.00 sec)
 

SELECT * FROM <nameOfTable>;

DESCRIBE <nameOfTable>;

INSERT INTO <table> <username, password, name1, name2, ...>
            VALUES ('user', password('pass'), 'value1', 'value2' ...);

CREATE TABLE <newtable> AS SELECT DISTINCT <field> FROM <oldtable>;

INSERT INTO <database.table> SELECT * FROM <database.table> WHERE <field> = <value>;

ALTER TABLE <tableOldName> RENAME <tableNewName>;

UPDATE <tableName> SET <field1> = <newValue> [WHERE <field2> = <currentValue>];







HOW TO SEE THE CONNECTION STATUS TO MYSQL
============================================

mysql> show status like '%onn%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| Aborted_connects         | 27418 |
| Connections              | 47167 |
| Max_used_connections     | 6     |
| Ssl_client_connects      | 0     |
| Ssl_connect_renegotiates | 0     |
| Ssl_finished_connects    | 0     |
| Threads_connected        | 2     |
+--------------------------+-------+
7 rows in set (0.00 sec)



Comments

Popular posts from this blog

Modsecurity block rule for XMLRPC and wp-login attack

SecAction phase:1,nolog,pass,initcol:ip=%{REMOTE_ADDR},initcol:user=%{REMOTE_ADDR},id:5000134  <Locationmatch "/wp-login.php">  SecRule user:bf_block "@gt 0" "deny,status:401,log,id:5000135,msg:'ip address blocked for 5 minutes, more than 10 login attempts in 3 minutes.'"  SecRule RESPONSE_STATUS "^302" "phase:5,t:none,nolog,pass,setvar:ip.bf_counter=0,id:5000136"  SecRule RESPONSE_STATUS "^200" "phase:5,chain,t:none,nolog,pass,setvar:ip.bf_counter=+1,deprecatevar:ip.bf_counter=1/180,id:5000137"  SecRule ip:bf_counter "@gt 10" "t:none,setvar:user.bf_block=1,expirevar:user.bf_block=300,setvar:ip.bf_counter=0"  </Locationmatch>  SecAction phase:1,nolog,pass,initcol:ip=%{REMOTE_ADDR},initcol:user=%{REMOTE_ADDR},id:5000234  <Locationmatch "/xmlrpc.php">  SecRule user:bf_block "@gt 0" "deny,status:401,log,id:5000235,msg:'ip address blocked for 5 m...

How to tweak linux server harddisk using hdparm

hdparm switches explained http://manpages.ubuntu.com/manpages/intrepid/man8/hdparm.8.html   First of all you have to install hdparm in linux. apt-get install hdparm #hdparm /dev/sda /dev/sda: readonly = 0 (off) readahead = 120 (on) geometry = 8850/255/63, sectors = 142182912, start = 0 Hard disk Performance Information # hdparm -tT /dev/hda /dev/hdd: Timing cached reads: 496 MB in 2.00 seconds = 247.42 MB/sec Timing buffered disk reads: 60 MB in 3.03 seconds = 19.81 MB/sec Hard drive set to low, slow settings # hdparm -cuda /dev/hda /dev/hda: IO_support = 0 (default 16-bit) unmaskirq = 0 (off) using_dma = 0 (off) readahead = 256 (on) Use below tweaks to increase disk read write performance. For sda drive ~]# hdparm -a 2048 /dev/sda /dev/sda: setting fs readahead to 2048 readahead = 2048 (on) For sdb drive [root@439298a ~]# hdparm -a 2048 /dev/sdb /dev/sdb: setting fs readahead to 2048 readahead = 2048 (on) ]# echo “anticipatory” >...

Apache tuning documentation

Apache bench marking ======================= Things or checklist to be considered before the test     CPU: avoid power-saving mode. cpufreq-set -g performance.     File descriptors: raise the limit to (at least) the number of concurrent connections you wish to handle, using ulimit -n in your shell, or setrlimit(RLIMIT_NOFILE) in your server. Beware, some systems forbid you to raise the limit, you might need to investigate a bit to find how to unlock it.     Disable the logs of your server (you do not want to lose time logging thousands of requests instead of answering them).     Raise /proc/sys/net/somaxconn to the number of concurrent connections you want to handle. To understand why this is necessary, read the technical report or the excellent paper Measuring the Capacity of a Web Server (Banga and Druschel, Usenix 97). More on the fascinating topic of the accept() queue can be found in accept()able Strategies for Im...