Skip to content
Bots!
Bots!
  • About
    • Myself
    • আমার দোয়া
  • Bookmarks
    • Bookmarks
    • My OCI Bookmarks
    • Useful Proxmox Commands & Links
    • Learning Nano
    • Useful Sites
    • Useful Virtualbox Command
    • Useful MySQL Command
    • Useful Linux Command
    • BTT-CAS
  • Resources
    • Webinar on Cloud Adoption for Project Managers
  • Photos
  • Videos
  • Downloads
Bots!

Useful MySQL Command

Allow MySQL client to connect Remote Host

$ mysql -u root -p
Enter password:
mysql> use mysql
mysql> GRANT ALL ON *.* to root@'192.168.1.4' IDENTIFIED BY 'your-root-password';
mysql> FLUSH PRIVILEGES;

MySQL Database Dump

mysqldump -u user -p db-name > db-name.out

Delete MySQL Data from Table

There are two ways to delete all the data in a MySQL database table.

TRUNCATE TABLE tablename;

This will delete all data in the table very quickly. In MySQL the table is actually dropped and recreated, hence the speed of the query. The number of deleted rows for MyISAM tables returned is zero; for INNODB it returns the actual number deleted.

DELETE FROM tablename;

This also deletes all the data in the table, but is not as quick as using the “TRUNCATE TABLE” method. In MySQL >= 4.0 the number of rows deleted is returned; in MySQL 3.23 the number returned is always zero.


Exporting DB Schema/Structure (no data) only 

mysqldump -u root -p --no-data dbname > schema.sql

Backup Multiple Database

mysqldump –u[user name] –p[password] [database name 1] [database name 2] .. > [dump file]

Backup ALL Database

shell> mysqldump –u[user name] –p[password] –all-databases > [dump file]

Backup Specific Table in a Database

shell> mysqldump --user [username] --password=[password] [database name] [table name] > /tmp/sugarcrm_accounts_contacts.sql

Restoring MySQL Database

shell> mysql --u [username] --password=[password] [database name] < [dump file]

Export Database in Zipped (.gz) format

mysqldump -hlocalhost -uUSERNAME -pPA$$W0RD DATABASE | gzip > /home/USERNAME/backups-mysql/BACKUP.gz

Optimize Single Database:

./mysqlcheck -o database_name

Optimize All Databases:

./mysqlcheck -o -A
 ./mysqlcheck -o --all-databases

Analyze Single Database:

./mysqlcheck -a database_name

Analyze All Databases:

./mysqlcheck -a -A
 ./mysqlcheck -a --all-databases

Repair Single Database:

./mysqlcheck -r database_name

Repair All Databases:

./mysqlcheck -r -A
 ./mysqlcheck -r --all-databases

Importing & Exprting a Table from Database

Export- mysqldump -p - -user=username dbname tableName > tableName.sq
Import- mysql -u username -p -D dbname < tableName.sql

MySQL Drop Table

mysql> use database 'yourdbname';
mysql> drop table 'yourtablename';

View MySQL users and privileges

mysql> select user,host from mysql.user;
mysql> show grants for 'root'@'%';
mysql> select * from mysql.user;
mysql> desc mysql.user;
select host, user, password from mysql.user;

How to Create a New User

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

The first thing to do is to provide the user with access to the information they will need.

GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';

Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.

FLUSH PRIVILEGES;

How To Grant Different User Permissions
 
Here is a short list of other common possible permissions that users can enjoy.
ALL PRIVILEGES- as we saw previously, this would allow a MySQL user all access to a designated database (or if no database is selected, across the system)
CREATE- allows them to create new tables or databases
DROP- allows them to them to delete tables or databases
DELETE- allows them to delete rows from tables
INSERT- allows them to insert rows into tables
SELECT- allows them to use the Select command to read through databases
UPDATE- allow them to update table rows
GRANT OPTION- allows them to grant or remove other users' privileges
To provide a specific user with a permission, you can use this framework:
GRANT [type of permission] ON [database name].[table name] TO ‘[username]’@'localhost’;

If you want to give them access to any database or to any table, make sure to put an asterisk (*) in the place of the database name or table name. Each time you update or change a permission be sure to use the Flush Privileges command.

If you need to revoke a permission, the structure is almost identical to granting it:

REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’;

Just as you can delete databases with DROP, you can use DROP to delete a user altogether:

DROP USER ‘demo’@‘localhost’;

Run OPTIMIZE TABLE to defragment tables for better performance

mysqlcheck -u username -p --auto-repair --optimize --all-databases

Obtain MySQL Database size in MB

SELECT table_schema AS "Database", ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" FROM information_schema.TABLES GROUP BY table_schema;

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Myself…

Hi, I am Hasan T. Emdad Rumi, an IT Project Manager & Consultant, Virtualization & Cloud Savvyfrom Dhaka, Bangladesh. I have prior experience in managing numerous local and international projects in the area of Telco VAS & NMC, National Data Center & PKI Naitonal Root and CA Infrastructure. Also engaged with several Offshore Software Development Team.

Worked with Orascom Telecom-Banglalink, Network Elites as VAS partner, BTRC, BTT (Turkey) , Mango Teleservices Limited and Access to Informaiton (A2I-UNDP)

Currently working at Oracle Corporation as Principal Technology Solution and Cloud Architect.

You can reach me [h.t.emdad at gmail.com] and I will be delighted to exchange my views.

Tags

Apache Bind Cacti CentOS CentOS 6 CentOS 7 Debain Debian Debian 10 Debian 11 Debian 12 DKIM Docker endian icinga iptables Jitsi LAMP Letsencrypt Linux Munin MySQL Nagios Nextcloud NFS nginx pfsense php Postfix powerdns Proxmox RDP squid SSH SSL Ubuntu Ubuntu 16 Ubuntu 18 Ubuntu 20 Varnish virtualbox vpn Webmin XCP-NG zimbra

Topics

Recent Posts

  • Install Jitsi on Ubuntu 22.04 / 22.10 April 30, 2025
  • Key Lessons in life April 26, 2025
  • Create Proxmox Backup Server (PBS) on Debian 12 April 19, 2025
  • Add Physical Drive in Proxmox VM Guest April 19, 2025
  • Mount a drive permanently with fstab in Linux April 16, 2025
  • Proxmox 1:1 NAT routing March 30, 2025
  • Installation steps of WSL – Windows Subsystem for Linux March 8, 2025
  • Enabling Nested Virtualization In Proxmox March 8, 2025
  • How to Modify/Change console/SSH login banner for Proxmox Virtual Environment (Proxmox VE / PVE) March 3, 2025
  • Install Proxmox Backup Server on Debian 12 February 12, 2025

Archives

Top Posts & Pages

  • Install Jitsi on Ubuntu 22.04 / 22.10
©2025 Bots! | WordPress Theme by SuperbThemes