`
ywencn
  • 浏览: 87358 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

转载//15个mysql常用命令(包括查看mysql状态)

阅读更多

In all the 15 mysqladmin command-line examples below, tmppassword is used as the MySQL root user password. Please change this to your MySQL root password.

1. How to change the MySQL root user password?

# mysqladmin -u root -ptmppassword password 'newpassword'

# mysql -u root -pnewpassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.25-rc-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

 

2. How to check whether MySQL Server is up and running?

# mysqladmin -u root -p ping
Enter password:
mysqld is alive

3. How do I find out what version of MySQL I am running?

Apart from giving the ‘Server version’, this command also displays the current status of the mysql server.

# mysqladmin -u root -ptmppassword version
mysqladmin Ver 8.42 Distrib 5.1.25-rc, for redhat-linux-gnu on i686
Copyright (C) 2000-2006 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version 5.1.25-rc-community
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 107 days 6 hours 11 min 44 sec

Threads: 1 Questions: 231976 Slow queries: 0 Opens: 17067
Flush tables: 1 Open tables: 64 Queries per second avg: 0.25

4. What is the current status of MySQL server?

# mysqladmin -u root -ptmppassword status
Uptime: 9267148
Threads: 1 Questions: 231977 Slow queries: 0 Opens: 17067
Flush tables: 1 Open tables: 64 Queries per second avg: 0.25

The status command displays the following information:

  • Uptime: Uptime of the mysql server in seconds
  • Threads: Total number of clients connected to the server.
  • Questions: Total number of queries the server has executed since the startup.
  • Slow queries: Total number of queries whose execution time waas more than long_query_time variable’s value.
  • Opens: Total number of tables opened by the server.
  • Flush tables: How many times the tables were flushed.
  • Open tables: Total number of open tables in the database.

5. How to view all the MySQL Server status variable and it’s current value?

# mysqladmin -u root -ptmppassword extended-status
+-----------------------------------+-----------+
| Variable_name | Value |
+-----------------------------------+-----------+
| Aborted_clients | 579 |
| Aborted_connects | 8 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Bytes_received | 41387238 |
| Bytes_sent | 308401407 |
| Com_admin_commands | 3524 |
| Com_assign_to_keycache | 0 |
| Com_alter_db | 0 |
| Com_alter_db_upgrade | 0 |

6. How to display all MySQL server system variables and the values?

# mysqladmin  -u root -ptmppassword variables
+---------------------------------+---------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------+
| auto_increment_increment | 1 |
| basedir | / |
| big_tables | OFF |
| binlog_format | MIXED |
| bulk_insert_buffer_size | 8388608 |
| character_set_client | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |

skip.....

| time_format | %H:%i:%s |
| time_zone | SYSTEM |
| timed_mutexes | OFF |
| tmpdir | /tmp |
| tx_isolation | REPEATABLE-READ |
| unique_checks | ON |
| updatable_views_with_limit | YES |
| version | 5.1.25-rc-community |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | i686 |
| version_compile_os | redhat-linux-gnu |
| wait_timeout | 28800 |
+---------------------------------+---------------------------------+

7. How to display all the running process/queries in the mysql database?

# mysqladmin -u root -ptmppassword processlist
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 20 | root | localhost | | Sleep | 36 | | |
| 23 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+

You can use this command effectively to debug any performance issue and identify the query that is causing problems, by running the command automatically every 1 second as shown below.

# mysqladmin -u root -ptmppassword -i 1 processlist
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 20 | root | localhost | | Sleep | 36 | | |
| 23 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+

+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 24 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+

8. How to create a MySQL Database?

# mysqladmin -u root -ptmppassword create testdb

# mysql -u root -ptmppassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 705
Server version: 5.1.25-rc-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| sugarcrm |
| testdb |
+--------------------+
4 rows in set (0.00 sec)



Note: To display all tables in a database, total number of columns, row, column types, indexes etc., use the mysqlshow command that we discussed in our previous articles.

9. How to Delete/Drop an existing MySQL database?

# mysqladmin -u root -ptmppassword drop testdb
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 'testdb' database [y/N] y
Database “testdb” dropped

# mysql -u root -ptmppassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 707
Server version: 5.1.25-rc-community MySQL Community Server (GPL)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| sugarcrm |
+——————–+
3 rows in set (0.00 sec)

10. How to reload/refresh the privilege or the grants tables?

# mysqladmin -u root -ptmppassword reload;

Refresh command will flush all the tables and close/open log files.

# mysqladmin -u root -ptmppassword refresh

11. What is the safe method to shutdown the MySQL server?

# mysqladmin -u root -ptmppassword shutdown

# mysql -u root -ptmppassword
ERROR 2002 (HY000): Can't connect to local MySQL server
through socket '/var/lib/mysql/mysql.sock'

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”

12. List of all mysqladmin flush commands.

# mysqladmin -u root -ptmppassword flush-hosts
# mysqladmin -u root -ptmppassword flush-logs
# mysqladmin -u root -ptmppassword flush-privileges
# mysqladmin -u root -ptmppassword flush-status
# mysqladmin -u root -ptmppassword flush-tables
# mysqladmin -u root -ptmppassword 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.

13. How to kill a hanging MySQL Client Process?

First identify the hanging MySQL client process using the processlist command.

# mysqladmin -u root -ptmppassword processlist
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 20 | root | localhost | | Sleep | 64 | | |
| 24 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+

Now, use the kill command and pass the process_id as shown below. To kill multiple process you can pass comma separated process id’s.

# mysqladmin -u root -ptmppassword kill 20

# mysqladmin -u root -ptmppassword processlist
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 26 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+

14. How to start and stop MySQL replication on a slave server?

# mysqladmin  -u root -ptmppassword stop-slave
Slave stopped

# mysqladmin -u root -ptmppassword start-slave
mysqladmin: Error starting slave: The server is not configured as slave;
fix in config file or with CHANGE MASTER TO

15. How to combine multiple mysqladmin commands together?

In the example below, you can combine process-list, status and version command to get all the output together as shown below.

# mysqladmin  -u root -ptmppassword process status version
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 43 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+

Uptime: 3135
Threads: 1 Questions: 80 Slow queries: 0 Opens: 15 Flush tables: 3
Open tables: 0 Queries per second avg: 0.25

mysqladmin Ver 8.42 Distrib 5.1.25-rc, for redhat-linux-gnu on i686
Copyright (C) 2000-2006 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version 5.1.25-rc-community
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 52 min 15 sec

You can also use the short form as shown below:

# mysqladmin  -u root -ptmppassword pro stat ver

Use the option -h, to connect to a remote MySQL server and execute the mysqladmin commands as shown below.

# mysqladmin  -h 192.168.1.112 -u root -ptmppassword pro stat ver

分享到:
评论

相关推荐

    MYSQL常用命令教程

    这两个命令分别用于在Windows系统中启动和停止MySQL服务。在Linux环境下,命令略有不同,通常使用`systemctl start mysql`和`systemctl stop mysql`。 #### 二、连接与登录MySQL - **基本连接命令**:`mysql -u ...

    MySQL常用查询状态命令

    这个命令返回的信息非常全面,其中包括了服务器的版本、运行平台、连接ID、当前用户、连接方式(如UNIX socket或SSL)、服务端和客户端的字符集、运行时间、线程数、查询次数、慢查询次数等关键指标。例如: - **...

    mysql常用命令集锦--初级DBA

    MySQL常用命令集锦--初级DBA MySQL是当前最流行的开源关系数据库管理系统,本文将总结一些常用的MySQL命令,适合初级DBA学习和工作。 一、MySQL服务的启动和停止 MySQL服务的启动和停止命令如下: * ...

    mysql命令行常用命令

    MySQL 命令行常用命令是 MySQL 数据库管理员和开发者需要掌握的基本技能,本文将介绍 MySQL 命令行常用命令的六大招数,包括 MySQL 服务的启动和停止、登陆 MySQL、增加新用户、操作数据库、导出和导入数据、乱码...

    mysql常用命令总结

    ### MySQL常用命令总结 本文将基于提供的部分内容对MySQL的基本操作命令进行详细解析,这些命令涵盖了数据库及表的基本管理,如创建、查询、更新等。掌握这些命令有助于更好地管理和操作MySQL数据库。 #### 一、...

    mysql中文手册及常用命令

    接下来是"mysql命令大全.chm",这是一份包含了MySQL客户端命令行工具的常用命令集合。在Linux或命令行环境下操作MySQL时,熟悉这些命令能极大地提高工作效率。例如,`mysql -u root -p` 是用来登录MySQL服务器的命令...

    MySQL常用命令 MySQL常用命令

    MySQL 常用命令 MySQL 是一个流行的开源关系数据库管理系统,它提供了许多实用的命令来管理和维护数据库。在本资源中,我们将总结一些常用的 MySQL 命令,涵盖数据库创建、用户管理、数据备份和恢复、查询执行等...

    MySQL常用命令总结.txt

    MySQL常用命令总结 MySQL常用命令总结

    CentOS MySQL启动和关闭命令

    这个命令实际上是调用了`mysql.server`脚本,该脚本负责初始化MySQL服务,并启动MySQL守护进程。 - **命令格式**:`/usr/local/mysql/share/mysql/mysql.server start` - **执行用户**:建议使用root用户或者具有...

    centos源码安装mysql5.6.15或者5.7版本+mysql主从复制+mysql常用命令

    本教程将详细讲解如何通过源码安装MySQL 5.6.15或5.7版本,并实现主从复制,以及掌握一些常用的MySQL命令。 **1. 准备工作** 在开始源码安装之前,确保你的CentOS系统是最新的,可以通过`yum update`来更新。另外,...

    mysql常用操作命令

    本文将对mysql的常用操作命令进行总结,包括连接mysql、查询版本信息、查询当前日期、查询服务器中的所有数据库、使用指定数据库、查询当前所操作的数据库名称、创建新数据库、删除数据库、创建表、显示数据库中的...

    mysql常用命令 .chm

    常用的mysql命令,包括如何登陆mysql,备份等命令。

    MySQL常用命令.doc

    以下是一些关于MySQL常用命令的详细说明: 1. **创建和管理数据库**: - `create database name;` 用于创建一个新的数据库,name是数据库的名称。 - `use databasename;` 用于切换到指定的数据库。 - `drop ...

    linux上安装mysql.pdf

    创建mysql组及用户 Java代码 1. groupadd mysql 2. useradd -g mysql mysql 、解压安装 将mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz文件,复制到/tmp目录。 执行以下命令解压安装: Java代码 1. tar xzvf mysql-...

    ubuntu18.04卸载mysql并重新安装的方法.doc

    这个错误是由于MySQL服务器的post-installation脚本返回了错误状态码127所致。 解决这个错误的方法是,编辑MySQL服务器的配置文件 `/etc/mysql/mysql.conf.d/mysqld.cnf`,并注释掉其中的某一行配置。例如: `user...

    MySQL 常用命令总结

    ### MySQL常用命令精要解析 #### 一、MySQL基础命令概览 MySQL是全球最流行的开源关系型数据库管理系统之一,其高效稳定、功能全面而深受广大开发者喜爱。掌握MySQL的基本命令是进行数据库管理与开发的基础。 ###...

    mysql设置更改root密码、mysql服务器的连接、mysql常用命令的图解

    **MySQL常用命令** - 查看所有数据库: ``` show databases; ``` - 选择数据库: ``` use database_name; ``` - 查看所有表: ``` show tables; ``` - 查看表结构: ``` desc table_name; ``` - 查看...

    mysql-connector-java.zip 两个版本:5.1.47、8.0.28

    java连接mysql数据库的驱动,里边包含两个版本:5.1.47、8.0.28 也可从以下两个地址免费下载: 1. https://repo1.maven.org/maven2/mysql/mysql-connector-java/ 2. ...

    MySQL数据库管理常用命令

    导读:MySQL数据库管理常用命令。 安装利用RPM包安装Mysql,设置TCP 3306端口的iptables。 root密码管理设置root用户的密码mysqladmin -uroot password 'password'。 修改root用户的密码mysqladmin -uroot -p ...

    mysql常用数据库命令

    根据提供的标题、描述以及部分内容,我们可以总结出一系列MySQL数据库中的常用命令及操作。MySQL是一种广泛使用的开源关系型数据库管理系统(RDBMS),因其高效性、可靠性和灵活性而在多种应用场景中受到青睐。下面将...

Global site tag (gtag.js) - Google Analytics