October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Mysql show

mysqlshow is another way to check the database details table and column information, let’s check some of the basic commands

shows available databases
# mysqlshow -pmysql

Display all tables in a DB
# mysqlshow -pmysql databasename

Display tables along with number of columns in a database
# mysqlshow -v -pmysql databasename

Display columns and Rows also
# mysqlshow -v -v -pmysql databasename

Display column name along with column information for table in database

# mysqlshow -pmysql databasename tablename

it displays information about wid column from tablename table.
# mysqlshow -pmysql dbname tablename wid

Show all metadata information about a table.
# mysqlshow -i -pmysql dbname tablename

Display both indexes and columns of a table. Please note that the indexes are listed at the bottom of the display after the column information.
# mysqlshow -k -pmysql dbname tablename

How to Recover a Mysql Root Password

Let’s  check how can we recover mysql root password if we lost it
Step # 1: Stop the MySQL server process.
Step # 2: Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option so that it will not prompt for password.
Step # 3: Connect to mysql server as the root user.
Step # 4: Setup new mysql root account password.
Step # 5: Exit and restart the MySQL server.
Here are commands you need to type for each step (login as the root user)

 

# service mysqld stop
# mysqld_safe –skip-grant-tables &

[1] 13964
root@localhost ~]# Starting mysqld daemon with databases from /var/lib/mysql  ————nothing will show just type mysql
mysql —– typing mysql shows like this
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.0.77 Source distribution
Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the buffer.
Or
ctrl z then

#mysql
mysql> use mysql;
mysql>update user set password=PASSWORD(“password@123#”) WHERE user=”root”;
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

# mysql -u root -p  —— enter the database with new password

Mysqladmin Tips

Here we can see some tips with mysqladmin tool
To check the status of mysqld
# mysqladmin -u root -pmysql ping
# service mysqld status
# mysqladmin -u root -pmysql status

 

To find mysql version
# mysqladmin -u root -pmysql version

 

To view all the MySQL Server status variable and it s current value.
# mysqladmin -u root -pmysql extended-status

 

To display all MySQL server system variables and the values
# mysqladmin -u root -pmysql variables

To display all the running process/queries in the mysql database.

# mysqladmin -u root -pmysql processlist

# mysqladmin -u root -pmysql processlist -i 1  —- run this command automatically every 1 second.

To create a DB
# mysqladmin -pmysql create DBtest

 

To Drop a DB
# mysqladmin -pmysql drop DBtest

 

To reload mysqld demon
# mysqladmin -pmysql reload
# mysqladmin -pmysql refrsh

 

To stop mysql daemon
# mysqladmin -pmysql shutdow

 

Note: You can also use /etc/rc.d/init.d/mysqld stop to shutdown the server. To start the server, execute /etc/rc.d/init.d/mysql start

 

Mysql flush
# mysqladmin -pmysql flush-hosts
# mysqladmin -pmysql flush-logs
# mysqladmin -pmysql flush-privileges
# mysqladmin -pmysql flush-status
# mysqladmin -pmysql flush-tables
# mysqladmin -pmysql flush-threads



flush-hosts: Flush all information in the host cache.

flush-privileges: Reload the grant tables (same as reload).
flush-status: Clear status variables.
flush-threads: Flush the thread cache.
To start and stop MySQL replication on a slave server
# mysqladmin -u root -pmysql stop-slave
# mysqladmin -u root -pmysql start-slave

Mysql grant

 

Some mysql grant queries
Granting permission for a particular  DB only
mysql> grant all privileges on databasename.* to ‘username’@’localhost’;
mysql> flush privileges;

 

Granting Permission for all the DB
mysql> grant all privileges on *.* to ‘username’@’localhost’;

 

Granting Permission from a particular IP
# grant all privileges on *.* to ‘username’@’192.168.0.20’;

 

 # grant usage on *.* to bob@localhost identified by ‘passwd’;
# SHOW GRANTS FOR ‘admin’@’localhost’;

Granting selected privilages for user 

# grant select,insert,update,delete,create,drop on TestDb.* to ‘ctechz’@’localhost’ identified by ‘ctechz’;

 

 After creating the user give permission to him using grant
# create user username identified by ‘password’;
# GRANT ALL PRIVILEGES ON *.* TO ‘monty’@’%’ WITH GRANT OPTION;

— % means from all host.

How to Repair a Mysql Table

Repairing corrupted mysql tables using myisamchk / mysqlcheck / repair
1. myisamchk
 myisam is the default storage engine for mysql database. There is a chance of myisam tables gets corrupted easily. myisamchk helps to identify and fix corrupted tables in myisam.
when table created in mysql it will create different files under mysql some of them are *.MYD (mydata ) file to store data, *.MYI (myindex) to store the index and *.frm to store table format.
you have to shutdown mysqld before performing the repair
Checking all corrupted tables using myisamchk
# myisamchk -c /var/lib/mysql/*.MYI >> /tmp/log.txt
redirect the output to a file then it will display only corrupted table names on the screen. log.txt contains info about all tables including good ones.
Repair the corrupted table using myisamchk
# myisamchk -r /var/lib/mysql/*.MYI
2.mysqlcheck
It is a client performs table maintenance: It checks, repairs, optimizes, or analyzes tables etcIt checks all tables and repair them if necessary.
both mysqlcheck and myisamchk are similar, but works differently. The main difference is that mysqlcheck must be used when the mysqld server is running, whereas myisamchk should be used when it is not. The benefit of usingmysqlcheck is that you do not have to stop the server to perform table maintenance.
Checking tables using mysqlcheck
mysql> mysqlcheck [options] db_name tbl_name 
mysql> mysqlcheck [options] --databases  db_name 
mysql> mysqlcheck [options] --all-databases
some options include  --auto-repair,--check -c
Repair tables with mysqlcheck
# mysqlcheck -uroot -p -r tablename;
3.REPAIR 
# repair table tablename;

How to install Mysql with partition enabled

We can check how can we able to setting up mysql server with partition enabled. Lets see what is mysql partition means.

 

A partition is a division of a logical database or its constituting elements into distinct independent parts. Database partitioning is normally done for manageability, performance or availability reasons.

 

A popular and favourable application of partitioning is in a distributed database management system. Each partition may be spread over multiple nodes, and users at the node can perform local transactions on the partition. This increases performance for sites that have regular transactions involving certain views of data, whilst maintaining availability and security.

 

The partitioning can be done by either building separate smaller databases (each with its own tables, indices, and transaction logs), or by splitting selected elements.

Follow the steps to install mysql server with partition enabled

First get a package

# gunzip mysql-5.5.12.tar.gz
# tar -xvf mysql-5.5.12.tar
# cd mysql-5.5.12
# mysql-5.5.12 > cmake . -LH
# mysql-5.5.12 > cmake .
# mysql-5.5.12 > make
# mysql-5.5.12 > make install
# cd /usr/local/mysql/
# chown mysql:mysql . -R
# scripts/mysql_install_db –datadir=/usr/local/mysql/data/ –user=mysql

—-
Installing MySQL system tables…
OK
Filling help tables…
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password ‘new-password’
./bin/mysqladmin -u root -h systemspfx4 password ‘new-password’

Alternatively you can run:
./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!
——

create a my.cnf file, here i copied from another system and added the needed parameters including the port which the server want to start.
# scp my.cnf  root@IP:/usr/local/mysql/
Start the server
#./bin/mysqld_safe –defaults-file=/usr/local/mysql/my.cnf &
We can login using
# /usr/local/mysql/bin/mysql -uroot -h 127.0.0.1 -P3307
We can start the server in some other ways also
Start the server in background
# nohup sh /usr/local/mysql/bin/mysqld_safe –defaults-file=/usr/local/mysql/my.cnf  &
Enabled the general log and slow queries
# nohup sh /usr/local/mysql/bin/mysqld_safe –general_log=1 –log-slow-queries=/usr/local/mysql/data/RHEL664PFMX-SLOW.log &
And we can see the server is up and running in port 3307

Mysql Performance Tuning

MySQL Performance tuning [my.cnf file parameters + description]
[mysqld]# Maximum allowed number of connections to the MySQL Server
max_connections = 1500

# The key buffer is a variable that is shared amongst all MySQL clients on the server. A large setting is recomended, particularly helpful with tables that have unique keys.
key_buffer = 1024M

#The size of the buffer that is used for plain index scans, range index scans, and joins that do not use indexes and thus perform full table scans. Normally, the best way to get fast joins is to add #indexes. Increase the value of join_buffer_size to get a faster full join when adding indexes is not possible.
join_buffer_size = 100M#Each thread that does a sequential scan allocates a buffer of this size (in bytes) for each table it scans. If you do many sequential scans, you might want to increase this value, which defaults to #131072. The value of this variable should be a multiple of 4KB. If it is set to a value that is not a multiple of 4KB
read_buffer_size = 1M

#Each session that needs to do a sort allocates a buffer of this size
sort_buffer_size = 2M

#This is the old name of table_open_cache. The number of open tables for all threads. Increasing this value increases the number of file descriptors that mysqld requires.
table_cache = 1800

# Number of threads the server should cache for reuse. When a client disconnects, the client’s threads are put in the cache if there are fewer than thread_cache_size threads there.
thread_cache_size = 384

# The number of seconds the server waits for activity on a noninteractive connection before closing it.
wait_timeout = 300

# The number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake.
connect_timeout = 10# The maximum size of internal in-memory temporary tables.
tmp_table_size = 64M

# This variable sets the maximum size to which MEMORY tables are allowed to grow
max_heap_table_size = 64M

# The maximum size of one packet or any generated/intermediate string.
max_allowed_packet = 64M

# If there are more than this number of interrupted connections from a host, that host is blocked from further connections
max_connect_errors = 1000

# When reading rows in sorted order following a key-sorting operation, the rows are read through this buffer to avoid disk seeks.
read_rnd_buffer_size = 524288# Don’t cache results that are larger than this number of bytes.Default value is 1mb
query_cache_limit = 4M

#The size of the persistent buffer used for statement parsing and execution. This buffer is not freed between statements. If you are running complex queries, a larger query_prealloc_size value might be #helpful in improving performance, because it can reduce the need for the server to perform memory allocation during query execution operations.
query_prealloc_size = 65536

# The allocation size of memory blocks that are allocated for objects created during statement parsing and execution.
query_alloc_block_size = 131072

# The amount of memory allocated for caching query results. The default value is 0, which disables the query cache.
query_cache_size = 1024M;

#Set the query cache type
# 0 or OFF : Don’t cache results in or retrieve results from the query cache.
# 1 or ON   : Cache all cacheable query results except for those that begin with SELECT SQL_NO_CACHE.
# 2 or DEMAND : Cache results only for cacheable queries that begin with SELECT SQL_CACHE
query_cache_type = 1
# This variable is set from the value of the –skip-name-resolve option. If it is ON, mysqld resolves host names when checking client connections. If OFF, mysqld uses only IP numbers and all Host # column values in the grant tables must be IP numbers or localhost.
skip-name-resolve
[mysqld_safe]#The number of files that the operating system allows mysqld to open
open_files_limit = 8192

[mysqldump]# The maximum size of one packet or any generated/intermediate string.
max_allowed_packet = 16M

Monitor Mysql DB in linux

We can monitor mysql queries and database using an utility called MonYog. MONyog is a monitoring and advisory tool for MySQL Community as well as Enterprise versions.MONyog is agent-less, and does not require anything to be installed on the MySQL servers. MONyog helps monitor enterprise database environments.

 

MONyog MySQL Monitor and Advisor MySQL DBAs manage more MySQL servers,and tune their MySQL servers.

 

To install monyog get the rpm / tar.gz package

 

# rpm -ivh monyog.version.rpm
# service monyog start / stop

or

# tar -xvf monyog.version.tar.gz
# cd monyog
# cd bin
# ./MoNyog start / stop
After installing, MONyog will be started automatically as a daemon/service. We can immediately start monitoring your MySQL servers by pointing your browser to:

http://<YOUR_HOST>:5555

After getting the GUI interface perform some more steps to get the desired data’s  into the interface.
You have to set the username and password for the interface.
Name for the new Host that need to add the interface.
Mysql host, mysql host username and password.
Mysql connection interval
Enable query logs, error logs

 

Just go through the following screen shoots for more details

http://www.webyog.com/en/screenshots.php

To Enable mysql query logs you need to add some lines to your my.cnf file and restart.

To enable slow Query Log only
log-slow-queries = /var/log/mysql/mysql-slow.log
long_query_time = 1

To enable full Log Query
log=/var/log/mysqldquery.log

The above will log all queries to the log file.
Don’t forgot to restart mysql service after making changes in my.cnf file.

InnoDB vs MyISAM

MyISAM and InnoDB are the two most-common database engines available.  While installing the DB most of them don’t even take the time to select a storage-engine and just accept the database default.
The storage-engine is what which store, handle, and retrieve information for a particular table.
1. MyISAM
 It is the default engine with MySQL. Data in MyISAM tables is split between three different files on the disk.One for the table format, another for the data, and lastly a third for the indexes.
2. InnoDB
 InnoDB is transaction-safe means data-integrity is maintained throughout the entire query process. InnoDB also provides row-locking, as opposed to table-locking, means when one query is busy updating or inserting a row, another query can update a different row at the same time.
InnoDB boasts is the ability to use foreign-key constraints. FK constraints allows developers to ensure that inserted data referencing another table remains valid.
MyISAM v/s InnoDB
   MyISAM in most cases will be faster than InnoDB. It is the default engine chosen by the MySQL development team.
In  InnoDB features like row-level locking, transaction-safe queries, and relational table design are all very fast.
InnoDB recovers from a crash or other unexpected shutdown by replaying its logs. MyISAM must fully scan and repair or rebuild any indexes or possibly tables which had been updated but not fully flushed to disk.

mysqlhotcopy vs mysqldump

mysqlhotcopy vs mysqldump

mysqlhotcopy is a Perl script that was originally written and contributed by Tim Bunce. It uses LOCK TABLES, FLUSH TABLES, and cp or scp to make a database backup quickly. It is the fastest way to make a backup of the database or single tables, but it can be run only on the same machine where the database directories are located. mysqlhotcopy works only for backing up MyISAM and ARCHIVE tables. It runs on Unix and NetWare

The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both.

If you are doing a backup on the server and your tables all are MyISAM tables, consider using the mysqlhotcopy instead because it can accomplish faster backups and faster restores.