MYSQL ADMINISTRATIVE COMMANDS

From PheonixSolutions
Jump to navigation Jump to search

MYSQL ADMINISTRATIVE COMMANDS

Introduction

MySQL is a popular tool for storing and managing data on websites. It's free and easy to work with, helping websites run smoothly by organizing information effectively. Using a language called SQL, MySQL makes it simple to add, retrieve, and modify data in databases.

Installation

Check whether MySQL has been installed before or not if it is not installed install MySQL using the below link

To install MySQL in Centos7:

Reference:https://www.linode.com/docs/databases/mysql/how-to-install-mysql-on-centos-7/

To Create a Database:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image9.png

To list all the databases:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image18.png

To use the desired Database:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image2.png

To Create Table use:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image15.png

How to insert data into the table:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image6.png

To describe the table :

The description about the author table will be displayed.

To display all the rows i.e., data stored in the table:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image7.png

To insert a Column in the table:

Example:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image17.png

ALTER TABLE testtable ADD last_name varchar (20) AFTER first_name;

Modify the Column in Table using Alter Table command statement in mysql:

Syntax:

ALTER TABLE table_name ALTER COLUMN column_name column_type;

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image11.png

To drop a row by using a id value:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image14.png

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image1.png

To drop a column in a Table:

Syntax:

ALTER TABLE table_name DROP COLUMN column_name

Practicing outside the mysql in terminal command line:

To check the my sql server is running:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image5.png

To check the mysql version:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image4.png

mysqladmin -u root -ptmppassword status

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image12.png

To check the status of all mysql server variable’s and values’s:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image10.png

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image16.png

To check all the processlist in the mysql:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image3.png

Multiple process at the same time:

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image13.png

To connect Mysql remote server:

mysqladmin -h <ipaddress> -u root -p status

To start and stop the mysql replication on a slave server.

mysqladmin -u root -p start-slave

mysqladmin -u root -p stop-slave

To Store a mysql server debug information to logs:

mysqladmin -u root -p debug

Enter password:

Able to grant privileges, flush previleges etc are in it,

And also kill the processlist unwanted in it.

BACKUP COMMANDS IN MYSQL:

TO Backup MySQL db

Mysqldump [options] [database_name] > [backup_name]

Practice:

database named Rakesh I have created and mount in the external hard drive /mnt/backups/ .then the basic command to run that backup is,

Syntax :

Sudo mysqldump –p Rakesh > /mnt/backups/Rakesh.sql

-p: is the password for MySQL

If necessary, Back up all the databases:

mysqldump –u USER –p --all-databases > /mnt/backups/all_databases.sql

Compressing your Backup:

mysqldump -u USER -p -C Rakesh > /mnt/backups/Rakesh.sql.tgz

For compressed file :

Dumping:

mysqldump db_name table_name | gzip > table_name.sql.gz

Restore it

gunzip < table_name.sql.gz | mysql -u username -p db_name

Restore it.

MYSQL flush commands:

  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

To view mysql admin options use

Mysqladmin --help

https://wiki.pheonixsolutions.com/index.php/File:Vertopal_c73e5ef820ac4e159404bc761ad8f6bf-media-image8.png