Showing posts with label mySQL. Show all posts
Showing posts with label mySQL. Show all posts

Wednesday, January 7, 2026

MySQL DB administration scripts/commands

 -- List all users present in mysql


mysql> select user,host,account_locked,password_expired from mysql.user;



Create user in mysql:


mysql> CREATE USER 'test_user'@'%' IDENTIFIED BY 'test_user';

Query OK, 0 rows affected (0.02 sec)


mysql> GRANT ALL ON *.* TO 'user'@'localhost';

Query OK, 0 rows affected (0.01 sec)


View create user statements:

MariaDB [(none)]> SHOW CREATE USER 'test_user'@'localhost';


MariaDB [(none)]> SHOW CREATE USER 'test_user'@'%';


Rename a user:


mysql> RENAME USER 'dbatest' TO 'dbaprod';

Query OK, 0 rows affected (0.02 sec)


change password of a user:


mysql> ALTER USER 'dbaprod'@'%' IDENTIFIED BY 'dbaprod';

Query OK, 0 rows affected (0.01 sec)


Change resource option:


mysql> alter user 'dbaprod'@'%' with MAX_USER_CONNECTIONS 10;

Query OK, 0 rows affected (0.01 sec)


Lock/unlock an account:


mysql> alter user 'dbaprod'@'%' account lock;

Query OK, 0 rows affected (0.01 sec)


mysql> alter user 'dbaprod'@'%' account unlock;

Query OK, 0 rows affected (0.01 sec)


Make sure you are connected with root/admin user:


mysql> select current_user();

+----------------+

| current_user() |

+----------------+

| root@localhost |

+----------------+

1 row in set (0.00 sec)


Create normal user if not created.


mysql> CREATE user 'super'@'%' IDENTIFIED BY 'super';

Query OK, 0 rows affected (0.02 sec)


mysql> CREATE user 'super'@'localhost' IDENTIFIED BY 'super';

Query OK, 0 rows affected (0.02 sec)


Grant privileges for making it super user:


mysql> GRANT ALL PRIVILEGES ON *.* TO 'super'@'%' WITH GRANT OPTION;

Query OK, 0 rows affected (0.03 sec)


mysql> GRANT ALL PRIVILEGES ON *.* TO 'super'@'localhost' WITH GRANT OPTION;

Query OK, 0 rows affected (0.03 sec)


Reload all the privileges


mysql> FLUSH PRIVILEGES;


Query OK, 0 rows affected (0.02 sec)


 View the grants:


mysql> show grants for super;


Examples on GRANT command


GRANT ALL PRIVILEGES ON *.* to 'user_name'@'localhost';

GRANT ALL PRIVILEGES ON *.* to 'user_name'@'%';

GRANT ALL PRIVILEGES ON *.* to 'user_name'@'localhost' WITH GRANT OPTION;

GRANT ALL PRIVILEGES ON *.* to 'user_name'@'%' WITH GRANT OPTION;


GRANT ALL PRIVILEGES ON dbname.* to 'user_name'@'localhost';

GRANT ALL PRIVILEGES ON dbname.* to 'user_name'@'%';


GRANT INSERT,UPDATE,DELETE ON TABLE DBNAME.TABLENAME TO 'user_name'@'localhost';

GRANT INSERT,UPDATE,DELETE ON TABLE DBNAME.TABLENAME TO 'user_name'@'%';


GRANT CREATE ON DATABASE.* to 'user_name'@'localhost';

GRANT CREATE ON DATABASE.* to 'user_name'@'%';


GRANT SELECT , EXECUTE ON DATABASE.* to 'user_name'@'%';

GRANT SELECT , EXECUTE ON DATABASE.* to 'user_name'@'localhost';


REVOKE privileges ON <object> FROM user;


REVOKE DELETE, UPDATE ON <table_name> FROM 'user'@'localhost';


REVOKE DELETE, UPDATE ON <table_name> FROM 'user'@'%';


REVOKE ALL ON <table_name> FROM 'user'@'localhost';


REVOKE ALL ON <table_name> FROM 'user'@'%';


REVOKE SELECT ON <object> FROM 'username'@'localhost';


REVOKE SELECT ON <object> FROM 'username'@'localhost';


show engines;

+--------------------+---------+----------------------------------------------------------------+--------------+------+-----

| Engine             | Support | Comment                                                 | Transactions | XA | Savepoints |

+--------------------+---------+----------------------------------------------------------------+--------------+------+----

| ARCHIVE            | YES     | Archive storage engine                                        | NO | NO | NO |


Create a database in MySQL / MariaDB


-- Below commands can be used to create database


MariaDB [(none)]> CREATE DATABASE testdb;

Query OK, 1 row affected (0.009 sec)


MariaDB [(none)]> create database testdb1 character set UTF8mb4 collate utf8mb4_bin;

Query OK, 1 row affected (0.009 sec)


-- View the create database statement used for creating db 


MariaDB [(none)]> SHOW CREATE DATABASE testdb1;

+----------+-----------------------------------------------------------------------------------------+

| Database | Create Database |

+----------+-----------------------------------------------------------------------------------------+

| testdb1 | CREATE DATABASE `testdb1` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin */ |

+----------+-----------------------------------------------------------------------------------------+

1 row in set (0.007 sec)




View database list


MariaDB [(none)]> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| testdb |

+--------------------+

5 rows in set (0.006 sec)


mysql> SELECT * fROM information_schema.schemata;

+--------------+--------------------+----------------------------+------------------------+----------+--------------------+

| CATALOG_NAME | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH | DEFAULT_ENCRYPTION |

+--------------+--------------------+----------------------------+------------------------+----------+--------------------+

| def          | mysql               | utf8mb4           | utf8mb4_0900_ai_ci | NULL | NO |


mysql> show databases like 'test%';

+------------------+

| Database (test%) |

+------------------+

| test |



How to connect to MySQL / MariaDB database


hostname$ export PATH=/usr/local/mysql/bin:$PATH

hostname$ which mysql

/usr/local/mysql/bin


SYNTAX - mysql -u user -p


C:\Program Files\MariaDB 10.4\bin>mysql -u root -p

Enter password: ****

Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 19.Server version: 10.4.18-MariaDB 


Find current connection info:


MariaDB [(none)]> \s

--------------


Below is the alternative command:


MariaDB [(none)]> status;


Get current user and current database:


mariadb> select current_user,database();

+----------------+------------+

| current_user  | database() |

+----------------+------------+

| root@localhost | test |

+----------------+------------+

1 row in set (0.00 sec)


switch to another database:


MariaDB> USE TEST;

Database changed


MariaDB> select database();


Drop database from mysql /MariaDB


C:\Program Files\MariaDB 10.4\bin>mysql -u root -p

Enter password: ****


-- List down databases

MariaDB [(none)]> show databases;

+--------------------+

| Database.          |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

| testdb             |

| testdb1            |

+--------------------+

6 rows in set (0.001 sec)


-- Check the status:

MariaDB [(none)]> status


-- Drop database:

MariaDB [(none)]> DROP DATABASE testdb;



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


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


How to find timezone info


Check whether time_zone table is updated or not.

MariaDB [(none)]> select * from mysql.time_zone_name;

Empty set (0.000 sec)


Find current sessions/process in MYSQL/MARIA DB

Show all processes in mysql cluster.


mysql> show processlist;

+----+-----------------+-----------+------+---------+-------+------------------------+------------------+

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

+----+-----------------+-----------+------+---------+-------+------------------------+------------------+

| 5  | event_scheduler | localhost | NULL | Daemon | 34888 | Waiting on empty queue | NULL |

| 23 | root            | localhost | test | Sleep. | 36    | | NULL                 |

| 24 | root.           | localhost | NULL | Query  | 0      | init                   | show processlist |

+----+-----------------+-----------+------+---------+-------+------------------------+------------------+

3 rows in set (0.00 sec)


Show processes for a specific user:


mysql> select * from information_schema.processlist where user='root'\G


Show processes for a particular database:


mysql> select * from information_schema.processlist where DB='test';

+----+------+-----------+------+---------+------+-------+------+

| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |

+----+------+-----------+------+---------+------+-------+------+

| 23 | root | localhost | test | Sleep | 70 | | NULL |

+----+------+-----------+------+---------+------+-------+------+

1 row in set (0.00 sec)



Get the processid for the session:


mysql> show processlist;

+----+-----------------+-----------+------+---------+-------+------------------------+------------------+

| Id | User            | Host.     | db   | Command | Time  | State                  | Info |

+----+-----------------+-----------+------+---------+-------+------------------------+------------------+

| 5 | event_scheduler  | localhost  | NULL | Daemon  | 35192 | Waiting on empty queue | NULL |

| 24 | root            | localhost  | NULL | Query   | 0     | init                   | show processlist |

| 25 | root            | localhost  | test | Sleep.   | 6    |                        | NULL         |

+----+-----------------+-----------+------+---------+-------+------------------------+------------------+

3 rows in set (0.00 sec)


mysql> kill 25;


Query OK, 0 rows affected (0.00 sec)


mysql> show processlist;

+----+-----------------+-----------+------+---------+-------+------------------------+------------------+

| Id | User            | Host.     | db   | Command | Time  | State                  | Info |

+----+-----------------+-----------+------+---------+-------+------------------------+------------------+

| 5 | event_scheduler  | localhost  | NULL | Daemon  | 35192 | Waiting on empty queue | NULL |

| 24 | root            | localhost  | NULL | Query   | 0     | init 




How to kill all sessions of an user


mysql> show processlist;

+----+-----------------+-----------+--------+---------+-------+------------------------+------------------+

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

+----+-----------------+-----------+--------+---------+-------+------------------------+------------------+

| 5 | event_scheduler  | localhost | NULL  | Daemon   | 36418 | Waiting on empty queue | NULL |

| 24 | root | localhost | NULL     | Query | 0        | init  | show processlist        |

| 26 | root | localhost | test     | Sleep | 376      |       | NULL                    |

| 27 | root | localhost | testdb.  | Sleep | 347      |       | NULL                    |

+----+-----------------+-----------+--------+---------+-------+------------------------+------------------+

4 rows in set (0.00 sec)


Generate kill statements for killing all sessions of user root


mysql> SELECT GROUP_CONCAT(CONCAT('KILL ',id,';') SEPARATOR ' ') 'Run the kill statements ----->>> ' FROM information_schema.processlist WHERE user='root'\G

*************************** 1. row ***************************

Run the kill statements ----->>> : KILL 24; KILL 26; KILL 27;

1 row in set (0.00 sec)


mysql> KILL 24; KILL 26; KILL 27;



Uptime of server


MariaDB [(none)]> status;

--------------

mysql Ver 15.1 Distrib 10.4.18-MariaDB, for Win64 (AMD64), source revision 53123dfa3e365138591fd2f160c6057aca00a3e6


Connection id: 26

Current database:

.............

TCP port: 3306

Uptime: 4 days 19 hours 28 min 14 sec


Alternative command


MariaDB [(none)]> \s


Server startup time:


MariaDB [(none)]> select TIME_FORMAT(SEC_TO_TIME(VARIABLE_VALUE ),'%Hh %im') as Uptime from information_schema.GLOBAL_STATUS where VARIABLE_NAME='Uptime';

+----------+

| Uptime.  |

+----------+m

| 115h 30m |

+----------+

1 row in set (0.007 sec)


C:\Program Files\MariaDB 10.4\bin>mysqladmin version -u root -p

Enter password: ****

mysqladmin Ver 9.1 Distrib 10.4.18-MariaDB, for Win64 on AMD64 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.


Server version 10.4.18-MariaDB

Protocol version 10

Connection localhost via TCP/IP

TCP port 3306

Uptime: 4 days 19 hours 33 min 2 sec



MariaDB [(none)]> show variables like '%data%';

+---------------------------------------------+-------------------------------------+

| Variable_name                               | Value                               |

+---------------------------------------------+-------------------------------------+

| datadir                                     | /usr/local/mysql/data/ |


MariaDB [(none)]> SELECT Variable_Value FROM information_schema.GLOBAL_VARIABLES WHERE Variable_Name = 'datadir';

+-------------------------------------+

| Variable_Value                      |

+-------------------------------------+

| /usr/local/mysql/data/ |

+-------------------------------------+

1 row in set (0.001 sec)


mysql> select @@datadir;

+------------------------+

| @@datadir              |

+------------------------+

| /usr/local/mysql/data/ |

+------------------------+

1 row in set (0.00 sec)


 Find current data/time on MySQL / MariaDB


mysql> select CURRENT_TIMESTAMP,current_date,localtime(0),localtimestamp(0);


Find MySQL / MariaDB configuration values


1 . Get Config values from mysql prompt.


MariaDB [(none)]> SELECT VARIABLE_NAME,VARIABLE_VALUE FROM information_schema.GLOBAL_VARIABLES;


MariaDB [(none)]> SELECT VARIABLE_NAME,VARIABLE_VALUE FROM information_schema.GLOBAL_VARIABLES WHERE Variable_Name = 'port';

+---------------+----------------+

| VARIABLE_NAME | VARIABLE_VALUE |

+---------------+----------------+

| PORT          | 3306           |

+---------------+----------------+

1 row in set (0.001 sec)


MariaDB [(none)]> SELECT VARIABLE_NAME,VARIABLE_VALUE FROM information_schema.GLOBAL_VARIABLES WHERE Variable_Name = 'datadir';

+---------------+-------------------------------------+

| VARIABLE_NAME | VARIABLE_VALUE                      |

+---------------+-------------------------------------+

| DATADIR       | C:\Program Files\MariaDB 10.4\data\ |

+---------------+-------------------------------------+

1 row in set (0.001 sec)


2. Alternatively you can check my.cnf file in /etc folder in Linux


cat /etc/my.cnf


Find the last MySQL / MariaDB Database service restarted / server reboot time


MariaDB [(none)]> status

--------------

mysql Ver 15.1 Distrib 10.4.18-MariaDB, for Win64 (AMD64), source revision 53123dfa3e365138591fd2f160c6057aca00a3e6


Connection id: 33

Current database:

Current user: root@localhost

SSL: Not in use

...........

Uptime: 6 days 2 hours 3 min 3 sec


-- In older version:


MariaDB [(none)]> select TIME_FORMAT(SEC_TO_TIME(VARIABLE_VALUE ),'%Hh %im') as Uptime from information_schema.GLOBAL_STATUS where VARIABLE_NAME='Uptime';

+----------+

| Uptime   |

+----------+

| 146h 03m |

+----------+

1 row in set (0.009 sec)


-- In latest mysql version:


MariaDB [(none)]> SHOW GLOBAL STATUS LIKE 'Uptime';

+---------------+--------+

| Variable_name | Value |

+---------------+--------+

| Uptime        | 525859 |


mysql> show engines;

+--------------------+---------+----------------------------------------------------------------+--------------+------+-----

| Engine             | Support | Comment                                                 | Transactions | XA | Savepoints |

+--------------------+---------+----------------------------------------------------------------+--------------+------+----

| ARCHIVE            | YES     | Archive storage engine                                        | NO | NO | NO |

| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO | NO 


SELECT table_schema AS 'Database Name',  

SUM(data_length + index_length) 'Size in Bytes',  

ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) 'Size in MB'  

FROM information_schema.tables  

-- WHERE table_schema = 'kellogg_xalientkdndb'  

-- WHERE table_schema in ('huedb', 'rangerdb', 'rangerkmsdb', 'hivedb', 'scm')

-- WHERE table_schema in ('metastore','hive','hue','rangerdb','ranger','rangerkmsdb','scm')

GROUP BY table_schema

order by 1; 





owner of the table:


SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, CREATE_USER

FROM INFORMATION_SCHEMA.TABLES

WHERE TABLE_NAME = 'your_table_name';

to change the ownership:


  MYDB.SCHEMA(USER)=> ALTER TABLE weather OWNER TO xyz;

Monday, December 27, 2021

script to check mysql db user locked status

 SELECT 

    user, host, account_locked

FROM

    mysql.user

WHERE

    user = 'mysqluser' ;


mysql> CREATE USER mysqluser

    -> IDENTIFIED BY '5password67'

    -> ACCOUNT LOCK;

Query OK, 0 rows affected (2.10 sec)


mysql> SELECT

    ->     user, host, account_locked

    -> FROM

    ->     mysql.user

    -> WHERE

    ->     user = 'mysqluser' AND

    ->     host='localhost';

Empty set (0.00 sec)


mysql> SELECT

    ->     user, host, account_locked

    -> FROM

    ->     mysql.user

    -> WHERE

    ->     user = 'mysqluser' ;

+-----------+------+----------------+

| user      | host | account_locked |

+-----------+------+----------------+

| mysqluser | %    | Y              |

+-----------+------+----------------+

1 row in set (0.00 sec)

If the mysql server is performing slow than how to find out the process which is causing problem.

 If the mysql server is performing slow than how to find out the process which is causing problem.


Answer: Best way is to check with: ‘SHOW PROCESSLIST / SHOW ENGINE INNODB STATUS

script to check the uptime of a mysql database server?


SELECT
  VARIABLE_VALUE AS Uptime_seconds,
  NOW() AS "Now",
  NOW() - INTERVAL VARIABLE_VALUE SECOND AS "Up since",
  DATEDIFF(NOW(), NOW() - INTERVAL VARIABLE_VALUE SECOND) AS "Uptime_days"
FROM performance_schema.session_status
WHERE VARIABLE_NAME = 'Uptime';


 mysql> SELECT

    ->   VARIABLE_VALUE AS Uptime_seconds,

    ->   NOW() AS "Now",

    ->   NOW() - INTERVAL VARIABLE_VALUE SECOND AS "Up since",

    ->   DATEDIFF(NOW(), NOW() - INTERVAL VARIABLE_VALUE SECOND) AS "Uptime_days"

    -> FROM performance_schema.session_status

    -> WHERE VARIABLE_NAME = 'Uptime';

+----------------+---------------------+----------------------------+-------------+

| Uptime_seconds | Now                 | Up since                   | Uptime_days |

+----------------+---------------------+----------------------------+-------------+

| 100752         | 2021-12-27 10:02:34 | 2021-12-26 06:03:22.000000 |           1 |

+----------------+---------------------+----------------------------+-------------+

1 row in set (0.01 sec)


mysql> SHOW GLOBAL STATUS LIKE '%UPTIME%';

+---------------------------+--------+

| Variable_name             | Value  |

+---------------------------+--------+

| Uptime                    | 100776 |

| Uptime_since_flush_status | 100776 |

+---------------------------+--------+

2 rows in set (0.00 sec)

What are the different database engine present in MySQL?

different database engine present in MySQL

MyISAM

Heap

Merge

INNO DB

ISAM


 What are the advantages of MyISAM over InnoDB?

MyISAM follows a conservative approach to disk space management and stores each MyISAM table in a separate file, which can be further compressed if required. On the other hand, InnoDB stores the tables in the tablespace. Its further optimization is difficult.


What is InnoDB?

InnoDB is a storage database for SQL. The ACID-transactions are also provided in InnoDB and also includes support for the foreign key. Initially owned by InnobaseOY now belongs to Oracle Corporation after it acquired the latter since 2005.

****************************************


What is ISAM?

It is a system for file management developed by IBM, which allows records to access sequentially or even randomly.

How to grant super user privilege to user in MySQL?

 Grant All Privilege's On All Databases

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

FLUSH PRIVILEGES;

Show Grants

SHOW GRANTS FOR 'someuser'@'localhost';

How to list the tables in MySQL ?

SELECT table_name 
FROM information_schema.tables
WHERE table_schema = 'your_db_name';


mysql> use mysql;

Database changed

mysql> show tables;

+----------------------------------------------+

| Tables_in_mysql                              |

+----------------------------------------------+

| columns_priv                                 |

| component                                    |

| db                                           |

| default_roles                                |

| engine_cost                                  |

command to spool query output to another log file in MySQL

 Spooling  SQL query Output in a File in MySQL & MariaDB


mysql> use testdb;

Database changed

mysql> tee testspool.txt;

Logging to file 'testspool.txt;'

mysql> select name from emp;

mysql> notee

Outfile disabled.


commands in MySQL to get the current user and date

 mysql> SELECT CURRENT_DATE();

+----------------+

| CURRENT_DATE() |

+----------------+

| 2021-12-24     |

+----------------+

1 row in set (0.00 sec)


mysql> select current_user();

+----------------+

| current_user() |

+----------------+

| aaadbadmin@%   |

+----------------+

1 row in set (0.00 sec)


mysql> select @@datadir;

--------------

select @@datadir

--------------


+----------------------------------------+

| @@datadir                              |

+----------------------------------------+

| E:\MySQL8\MySQL\MySQL Server 8.0\Data\ |

+----------------------------------------+

1 row in set (0.00 sec)

to know the current database

mysql> SELECT DATABASE()

    -> ;

+--------------------+

| DATABASE()         |

+--------------------+

| information_schema |

+--------------------+

1 row in set (0.00 sec)





How to start/stop MySQL services in windows and Linux servers

 On Windows


If you are using Windows Open the Command Prompt and type


To Stop MySQL Service:


net stop MySQL80 

To Start MySQL Service:


net start MySQL80

On Linux


# /etc/init.d/mysqld start

# /etc/init.d/mysqld stop

# /etc/init.d/mysqld restart

command to get the parameter values in MySQL?

 


mysql> SHOW VARIABLES LIKE '%slow_query_log%';

--------------

SHOW VARIABLES LIKE '%slow_query_log%'

--------------


+---------------------+------------------------+

| Variable_name       | Value                  |

+---------------------+------------------------+

| slow_query_log      | ON                     |

| slow_query_log_file | <server_name>-slow.log |

+---------------------+------------------------+

2 rows in set (0.00 sec)

How to copy a table using CTAS (create Table as Select from ) in MySQL

 

Syntax for CTAS:

create table  <db_name.table_name> as select * from <db_name.table_name> ;


mysql> create table test3.reports_bak as select * from reports;

--------------

create table test3.reports_bak as select * from reports

--------------


Query OK, 55 rows affected (0.04 sec)

Records: 55  Duplicates: 0  Warnings: 0


mysql> use test3;

Database changed

mysql> show tables;

--------------

show tables

--------------


+-----------------+

| Tables_in_test3 |

+-----------------+

| reports_bak     |

+-----------------+

1 row in set (0.00 sec)


create/Drop database in MySQL?

 mysql> create database test1;


when you create DB on ,it will create directory under data with db name and under this Directory  all the tables will be added.

--------------

create database test1

--------------


Query OK, 1 row affected (0.00 sec)


mysql> SELECT schema_name FROM information_schema.schemata;

--------------

SELECT schema_name FROM information_schema.schemata

--------------


+------------------------+

| SCHEMA_NAME            |

+------------------------+

| mysql                  |

| information_schema     |

| performance_schema     |

| sys                    |

| aaabbbb_dba_management |

| aaabbbb_xxxxxxtdb      |

| xxxxxxt                |

| aaabbbb_xxxxxxtkdndb   |

| test3                  |

| test1                  |

+------------------------+

10 rows in set (0.00 sec)



mysql> drop database test1;

--------------

drop database test1

--------------


Query OK, 0 rows affected (0.01 sec)


mysql> SELECT schema_name FROM information_schema.schemata;

--------------

SELECT schema_name FROM information_schema.schemata

--------------


+------------------------+

| SCHEMA_NAME            |

+------------------------+

| mysql                  |

| information_schema     |

| performance_schema     |

| sys                    |

| aaabbbb_dba_management |

| aaabbbb_xxxxxxtdb      |

| xxxxxxt                |

| aaabbbb_xxxxxxtkdndb   |

| test3                  |

+------------------------+

9 rows in set (0.00 sec)


create/drop/Alter user in MySQL database?

 The following is the basic syntax to create a new user in MySQL:


CREATE USER testmysqlusr IDENTIFIED BY 'petcom123';  

***********************************************************



mysql> CREATE USER testmysqlusr IDENTIFIED BY 'petcom123';

--------------

CREATE USER testmysqlusr IDENTIFIED BY 'petcom123'

--------------


Query OK, 0 rows affected (0.04 sec)




ALTER USER 'testmysqlusr'@'localhost' IDENTIFIED BY 'macd123'; 



Query OK, 0 rows affected (0.04 sec)


mysql>SHOW GRANTS FOR 'testmysqlusr'@'localhost';



mysql> SHOW GRANTS FOR 'testmysqlusr'@'localhost';

--------------

SHOW GRANTS FOR 'testmysqlusr'@'localhost'

--------------


ERROR 1141 (42000): There is no such grant defined for user 'testmysqlusr' on host 'localhost'

mysql>


mysql> drop user testmysqlusr;

--------------

drop user testmysqlusr

--------------


Query OK, 0 rows affected (0.01 sec)



How to check the MySQL version?

 How to check the MySQL version?

We can check the MySQL version on Linux using the below command:


mysql -v  



"E:\MySQL8\MySQL\MySQL Server 8.0\bin\mysql.exe" -v -u aaadbadmin -p  -h localhost -P 3430


C:\Users\_SQLAdmin>"E:\MySQL8\MySQL\MySQL Server 8.0\bin\mysql.exe" -v -u aaadbadmin -p  -h localhost -P 3430

Enter password: **********

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 204130

Server version: 8.0.22 MySQL Community Server - GPL


Copyright (c) 2000, 2020, 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>


How to view the database name in MySQL?

mysql>  SELECT schema_name FROM information_schema.schemata;

--------------

SELECT schema_name FROM information_schema.schemata

--------------


+------------------------+

| SCHEMA_NAME            |

+------------------------+

| mysql                  |

| information_schema     |

| performance_schema     |

| sys                    |

| aaabbbb_dba_management |

| aaabbbb_xxxxxxtdb      |

| xxxxxxt                |

| aaabbbb_xxxxxxtkdndb   |

| test3                  |

+------------------------+

9 rows in set (0.00 sec)


mysql> select db,user from mysql.db;

+------------------------+---------------+

| db                     | user          |

+------------------------+---------------+

| aaabbbb_dba_management | aaadbadmin    |

| performance_schema     | mysql.session |

| sys                    | mysql.sys     |

| aaabbbb_xxxxxxtdb      | xxxxxxt_admin |

| aaabbbb_xxxxxxtkdndb   | xxxxxxt_admin |

| xxxxxxt                | xxxxxxt_admin |

+------------------------+---------------+

6 rows in set (0.00 sec)



mysql> show databases;

+------------------------+

| Database               |

+------------------------+

| information_schema     |

| aaabbbb_dba_management |

| aaabbbb_xxxxxxtdb      |

| aaabbbb_xxxxxxtkdndb   |

| mysql                  |

| performance_schema     |

| sys                    |

| test3                  |

| xxxxxxt                |

+------------------------+

9 rows in set (0.00 sec)


Friday, December 24, 2021

How to check user privileges in MySQL database?

 mysql> SHOW GRANTS FOR 'root'@'localhost';

+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| Grants for root@localhost                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |

+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `root`@`localhost` WITH GRANT OPTION                                                                                                                                               |

| GRANT APPLICATION_PASSWORD_ADMIN,AUDIT_ADMIN,BACKUP_ADMIN,BINLOG_ADMIN,BINLOG_ENCRYPTION_ADMIN,CLONE_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,INNODB_REDO_LOG_ARCHIVE,INNODB_REDO_LOG_ENABLE,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_APPLIER,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SERVICE_CONNECTION_ADMIN,SESSION_VARIABLES_ADMIN,SET_USER_ID,SHOW_ROUTINE,SYSTEM_USER,SYSTEM_VARIABLES_ADMIN,TABLE_ENCRYPTION_ADMIN,XA_RECOVER_ADMIN ON *.* TO `root`@`localhost` WITH GRANT OPTION |

| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |

+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

3 rows in set (0.00 sec)


mysql> select super_priv from mysql.user where user='mysqldbadmin';

+------------+

| super_priv |

+------------+

| Y          |

+------------+

1 row in set (0.00 sec)


script to check database size in MySQL?

script to  check database size in MySQL


SELECT table_schema AS 'Database Name',  

SUM(data_length + index_length) 'Size in Bytes',  

ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) 'Size in MB'  

FROM information_schema.tables  

WHERE table_schema = 'mysql'  

GROUP BY table_schema;  

+---------------+---------------+------------+

| Database Name | Size in Bytes | Size in MB |

+---------------+---------------+------------+

| mysql         |       5292032 |       5.05 |

+---------------+---------------+------------+

1 row in set (0.00 sec)