Friday, 27 April 2018

20 MySQL (Mysqladmin) Commands for Database Administration in Linux

mysqladmin is a command-line utility the comes with MySQL server and it is used by Database Administratorsto perform some basic MySQL tasks easily such as setting root password, changing root password, monitoring mysql processes, reloading privileges, checking server status etc.
In this article we’ve compiled some very useful ‘mysqladmin‘ commands that are used by system/database administrators in their day-to-day work. You must have MySQL server installed on your system to perform these tasks.
mysqladmin commands
20 mysqladmin commands examples
If you don’t have MySQL server installed or you are using older version of MySQL server, then we recommend you all to install or update your version by following our below article.
  1. Installation of MySQL 5.5.28 Server on RHEL/CentOS/Fedora

1. How to set MySQL Root password?

If you have fresh installation of MySQL server, then it doesn’t required any password to connect it as root user. To set MySQL password for root user, use the following command.
# mysqladmin -u root password YOURNEWPASSWORD

2. How to Change MySQL Root password?

If you would like to change or update MySQL root password, then you need to type the following command. For example, say your old password is 123456 and you want to change it with new password say xyz123.
mysqladmin -u root -p123456 password 'xyz123'

3. How to check MySQL Server is running?

To find out whether MySQL server is up and running, use the following command.
# mysqladmin -u root -p ping
Enter password:
mysqld is alive

4. How to Check which MySQL version I am running?

The following command shows MySQL version along with the current running status .
# mysqladmin -u root -p version
Enter password:
mysqladmin  Ver 8.42 Distrib 5.5.28, for Linux on i686
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Server version          5.5.28
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 7 days 14 min 45 sec
Threads: 2  Questions: 36002  Slow queries: 0  Opens: 15  Flush tables: 1  Open tables: 8  Queries per second avg: 0.059

5. How to Find out current Status of MySQL server?

To find out current status of MySQL server, use the following command. The mysqladmin command shows the status of uptime with running threads and queries.
# mysqladmin -u root -ptmppassword status
Enter password:
Uptime: 606704  Threads: 2  Questions: 36003  Slow queries: 0  Opens: 15  Flush tables: 1  Open tables: 8  Queries per second avg: 0.059

6. How to check status of all MySQL Server Variable’s and value’s?

To check all the running status of MySQL server variables and values, type the following command. The output would be similar to below.
# mysqladmin -u root -p extended-status
Enter password:
+------------------------------------------+-------------+
| Variable_name                            | Value       |
+------------------------------------------+-------------+
| Aborted_clients                          | 3           |
| Aborted_connects                         | 3           |
| Binlog_cache_disk_use                    | 0           |
| Binlog_cache_use                         | 0           |
| Binlog_stmt_cache_disk_use               | 0           |
| Binlog_stmt_cache_use                    | 0           |
| Bytes_received                           | 6400357     |
| Bytes_sent                               | 2610105     |
| Com_admin_commands                       | 3           |
| 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           |
| Com_alter_tablespace                     | 0           |
+------------------------------------------+-------------+

7. How to see all MySQL server Variables and Values?

To see all the running variables and values of MySQL server, use the command as follows.
# mysqladmin  -u root -p variables
Enter password:
+---------------------------------------------------+----------------------------------------------+
| Variable_name                                     | Value                                        |
+---------------------------------------------------+----------------------------------------------+
| auto_increment_increment                          | 1                                            |
| auto_increment_offset                             | 1                                            |
| autocommit                                        | ON                                           |
| automatic_sp_privileges                           | ON                                           |
| back_log                                          | 50                                           |
| basedir                                           | /usr                                         |
| big_tables                                        | OFF                                          |
| binlog_cache_size                                 | 32768                                        |
| binlog_direct_non_transactional_updates           | OFF                                          |
| binlog_format                                     | STATEMENT                                    |
| binlog_stmt_cache_size                            | 32768                                        |
| bulk_insert_buffer_size                           | 8388608                                      |
| character_set_client                              | latin1                                       |
| character_set_connection                          | latin1                                       |
| character_set_database                            | latin1                                       |
| character_set_filesystem                          | binary                                       |
| character_set_results                             | latin1                                       |
| character_set_server                              | latin1                                       |
| character_set_system                              | utf8                                         |
| character_sets_dir                                | /usr/share/mysql/charsets/                   |
| collation_connection                              | latin1_swedish_ci                            |
+---------------------------------------------------+----------------------------------------------+

8. How to check all the running Process of MySQL server?

The following command will display all the running process of MySQL database queries.
# mysqladmin -u root -p processlist
Enter password:
+-------+---------+-----------------+---------+---------+------+-------+------------------+
| Id    | User    | Host            | db      | Command | Time | State | Info             |
+-------+---------+-----------------+---------+---------+------+-------+------------------+
| 18001 | rsyslog | localhost:38307 | rsyslog | Sleep   | 5590 |       |                  |
| 18020 | root    | localhost       |         | Query   | 0    |       | show processlist |
+-------+---------+-----------------+---------+---------+------+-------+------------------+

9. How to create a Database in MySQL server?

To create a new database in MySQL server, use the command as shown below.
# mysqladmin -u root -p create databasename
Enter password:
# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18027
Server version: 5.5.28 MySQL Community Server (GPL) by Remi
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| databasename       |
| mysql              |
| test               |
+--------------------+
8 rows in set (0.01 sec)
mysql>

10. How to drop a Database in MySQL server?

To drop a Database in MySQL server, use the following command. You will be asked to confirm press ‘y‘.
# mysqladmin -u root -p drop databasename
Enter password:
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'databasename' database [y/N] y
Database "databasename" dropped

11. How to reload/refresh MySQL Privileges?

The reload command tells the server to reload the grant tables. The refresh command flushes all tables and reopens the log files.
# mysqladmin -u root -p reload;
# mysqladmin -u root -p refresh

12. How to shutdown MySQL server Safely?

To shutdown MySQL server safely, type the following command.
mysqladmin -u root -p shutdown
Enter password:
You can also use the following commands to start/stop MySQL server.
# /etc/init.d/mysqld stop
# /etc/init.d/mysqld start

13. Some useful MySQL Flush commands

Following are some useful flush commands with their description.
  1. flush-hosts: Flush all host information from host cache.
  2. flush-tables: Flush all tables.
  3. flush-threads: Flush all threads cache.
  4. flush-logs: Flush all information logs.
  5. flush-privileges: Reload the grant tables (same as reload).
  6. flush-status: Clear status variables.
# mysqladmin -u root -p flush-hosts
# mysqladmin -u root -p flush-tables
# mysqladmin -u root -p flush-threads
# mysqladmin -u root -p flush-logs
# mysqladmin -u root -p flush-privileges
# mysqladmin -u root -p flush-status

14. How to kill Sleeping MySQL Client Process?

Use the following command to identify sleeping MySQL client process.
# mysqladmin -u root -p processlist
Enter password:
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host      | db | Command | Time | State | Info             |
+----+------+-----------+----+---------+------+-------+------------------+
| 5  | root | localhost |    | Sleep   | 14   |       |      |
| 8  | root | localhost |    | Query   | 0    |       | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+
Now, run the following command with kill and process ID as shown below.
# mysqladmin -u root -p kill 5
Enter password:
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host      | db | Command | Time | State | Info             |
+----+------+-----------+----+---------+------+-------+------------------+
| 12 | root | localhost |    | Query   | 0    |       | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+
If you like to kill multiple process, then pass the process ID‘s with comma separated as shown below.
# mysqladmin -u root -p kill 5,10

15. How to run multiple mysqladmin commands together?

If you would like to execute multiple ‘mysqladmin‘ commands together, then the command would be like this.
# mysqladmin  -u root -p processlist status version
Enter password:
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host      | db | Command | Time | State | Info             |
+----+------+-----------+----+---------+------+-------+------------------+
| 8  | root | localhost |    | Query   | 0    |       | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+
Uptime: 3801  Threads: 1  Questions: 15  Slow queries: 0  Opens: 15  Flush tables: 1  Open tables: 8  Queries per second avg: 0.003
mysqladmin  Ver 8.42 Distrib 5.5.28, for Linux on i686
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Server version          5.5.28
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 1 hour 3 min 21 sec
Threads: 1  Questions: 15  Slow queries: 0  Opens: 15  Flush tables: 1  Open tables: 8  Queries per second avg: 0.003

16. How to Connect remote mysql server

To connect remote MySQL server, use the -h (host)  with IP Address of remote machine.
# mysqladmin  -h 172.16.25.126 -u root -p

17. How to execute command on remote MySQL server

Let’s say you would like to see the status of remote MySQL server, then the command would be.
# mysqladmin  -h 172.16.25.126 -u root -p status

18. How to start/stop MySQL replication on a slave server?

To start/stop MySQL replication on salve server, use the following commands.
# mysqladmin  -u root -p start-slave
# mysqladmin  -u root -p stop-slave

19. How to store MySQL server Debug Information to logs?

It tells the server to write debug information about locks in use, used memory and query usage to the MySQL log file including information about event scheduler.
# mysqladmin  -u root -p debug
Enter password:

20. How to view mysqladmin options and usage

To find out more options and usage of myslqadmin command use the help command as shown below. It will display a list of available options.
# mysqladmin --help
We have tried our best to include almost all of ‘mysqladmin‘ commands with their examples in this article, If still, we’ve missed anything, please do let us know via comments and don’t forget to share with your friends.

Red Hat Linux Essentials Interview Questions & Answers

  1. Question1. What Is A Linux Server Used For?

    Answer :
    A Linux server is a high-powered variant of the Linux open source operating system that's designed to handle the more demanding needs of business applications such as network and system administration, database management and Web services.
  2. Question2. What Is The Meaning Of Red Hat?

    Answer :
    Red Hat Enterprise Linux (RHEL) is a distribution of the Linux operating system developed for the business market. RHEL was formerly known as Red Hat Linux Advanced Server.
  3. Question3. What Can You Type At A Command Line To Determine Which Shell You Are Using?

    Answer :
    echo $SHELL.
  4. Question4. Explain Lvm Snapshot?

    Answer :
    LVM snapshots allow the administrator to create a new block device which presents an exact copy of a logical volume, frozen at some point in time.
  5. Question5. What Is Volume Group (vg)?

    Answer :
    The Volume Group is the highest level abstraction used within the LVM. It gathers together a collection of Logical Volumes and Physical Volumes into one administrative unit.
  6. Question6. What Command Is Used To Remove The Password Assigned To A Group?

    Answer :
    gpasswd -r.
  7. Question7. What Is Logical Extent (le)?

    Answer :
    Each logical volume is split into chunks of data, known as logical extents. The extent size is the same for all logical volumes in the volume group.
  8. Question8. What Is Physical Extent (pe)?

    Answer :
    Each physical volume is divided chunks of data, known as physical extents; these extents have the same size as the logical extents for the volume group.
  9. Question9. What Are Lvm1 And Lvm2?

    Answer :
    1. LVM1 and LVM2 are the versions of LVM.
    2. LVM2 uses device mapper driver contained in 2.6 kernel version.
    3. LVM 1 was included in the 2.4 series kernels.
  10. Question10. What Is The Difference Between Lvm And Raid?

    Answer :
    RAID provides redundancy but LVM doesn’t provide Redundancy.
  11. Question11. What Command Should You Use To Check The Number Of Files And Disk Space Used And Each User's Defined Quotas?

    Answer :
    repquota.
  12. Question12. What Are The Process States In Unix?

    Answer :
    As a process executes it changes state according to its circumstances. Unix processes have the following states:
    1. Running : The process is either running or it is ready to run .
    2. Waiting : The process is waiting for an event or for a resource.
    3. Stopped : The process has been stopped, usually by receiving a signal.
    4. Zombie : The process is dead but have not been removed from the process table.
  13. Question13. How We Will Check Free Space On Drive /dev/sda With Parted Command?

    Answer :
    #parted /dev/sda
    print.
  14. Question14. What Are The Steps To Create Lvm?

    Answer :
    Create physical volumes by “pvcreate” command :
    #pvcreate /dev/sda2
    Add physical volume to volume group by “vgcreate” command :
    #vgcreate VLG0 /dev/sda2
    Create logical volume from volume group by “lvcreate” command :
    #lvcreate -L 1G -n LVM1 VLG0
    Now create file system on /dev/sda2 partition by “mke2fs” command :
    #mke2fs -j /dev/VLG0/LVM1
  15. Question15. What Is Lvm?

    Answer :
    LVM stands for Logical Volume Manager. LVM, is a storage management solution that allows administrators to divide hard drive space into physical volumes (PV), which can then be combined into logical volume groups (VG), which are then divided into logical volumes (LV) on which the filesystem and mount point are created.
  16. Question16. What Is 'inode'?

    Answer :
    All UNIX files have its description stored in a structure called 'inode'. The inode contains info about the file-size, its location, time of last access, time of last modification, permission and so on. Directories are also represented as files and have an associated inode.
  17. Question17. What Can We Do With “parted” Command Or Utility?

    Answer :
    • View the existing partition table.
    • Add partitions from free space or additional hard drives.
    • Change the size of existing partitions.
  18. Question18. Tell Me The Steps To Remove The Swap File?

    Answer :
    1. Firstly disable the swap file by “swapoff” command.
    2. Remove Swap file entry from /etc/fstab file.
    3. Now remove the swap file by “rm” command.
  19. Question19. How Do You Identify The Ip Address Assigned To The Box?

    Answer :
    The ipconfig command tells you the IP for the computer.
  20. Question20. How Do You Identify Which Version Of Red Hat Is Installed On The Server?

    Answer :
    Open a Red Had shell command line and type /etc/redhat-release to identify the release version for Red Hat.
  21. Question21. Which Layer In The Osi Model Is Responsible For The User’s Application When Working With An Application Over The Network?

    Answer :
    Layer 7 is the layer responsible for application support including support for user passwords and file sharing.
  22. Question22. How Can You Rename A File In Red Hat Linux?

    Answer :
    Open the Red Hat Linux shell command line and use the mv command. The mv command takes the path to the original file and the path to the newly named file in the parameters. If you don’t specify the parameters, the mv command will ask you for them.
  23. Question23. How Do You Access Mysql On Red Hat?

    Answer :
    Go to the shell prompt and type mysql. Linux looks up the information contained in /var/mysql/mysql.sock and connects to the port listed in the sock file. If the port is blocked or mysql isn’t installed on the system, Red Hat Linux returns an error.
  24. Question24. What Is The Standard Directory Used To Store User Html Website Files?

    Answer :
    First, you need to install Apache, and Apache sets a default directory for public HTML files. The standard directory is /var/www/html.
  25. Question25. What Type Of Remote Software Can You Use To Encrypt Communication But Control Your Server?

    Answer :
    SSH is a remote desktop software that lets you remotely control the Red Hat server using a command line but also encrypts the information across the Internet. SSH uses port 22, so you must open this port when you set up Red Hat.
  26. Question26. When You Install Apache, What Is The Default Web Port Used To Serve Web Pages To Public Viewers?

    Answer :
    Most web servers run on port 80, and Apache defaults to port 80 when you install the software. You can use an alternative port, but this forces the user to type a port into the browser when they access the website. The other popular port for web applications is port 8080.
  27. Question27. What Is The Total Number Of Primary Partitions You Can Have On One Drive In Linux Red Hat?

    Answer :
    you can have four primary partitions in Linux. This includes Red Hat and any other operating system.
  28. Question28. What Are Some Commands That You Can Use To Create A New User To Your System?

    Answer :
    There are three commands you can use to add a user. The three commands are useradd, adduser, and linuxconf.
  29. Question29. What Is Red Hat Network?

    Answer :
    Red Hat Network : A systems management platform providing lifecycle management of the operating system and applications.
    1. Installing and provisioning new systems.
    2. Updating systems.
    3. Managing configuration files.
    4. Monitoring performance.
    Redeploying systems for a new purpose "Hosted" and "Satellite" deployment architectures.
  30. Question30. What Are The Objectives Of Red Hat Linux?

    Answer :
    A user who can use effectively employ Red HatEnterprise Linux to customize his or her operating environment as well as accomplish common commandline tasks and desktop productivity roles.