如果你有全新安装的
MySQL
服务器,则不需要任何密码即可以 root 用户身份连接它。设置MySQL
root 用户的密码,使用以下命令。
# mysqladmin -u root password `YOURNEWPASSWORD`
如果你想更改或更新
MySQL
root 密码,那么你需要输入以下命令。例如,假设你的旧密码是123456
改成rumenz123
.
# mysqladmin -uroot -p123456 password rumenz123
找出是否
MySQL
服务器已启动并正在运行,请使用以下命令。
# mysqladmin -u root -p ping
Enter password:
mysqld is alive
以下命令显示
MySQL
版本以及当前的运行状态。
# mysqladmin -u root -p version
Enter password:
mysqladmin Ver 42 Distrib `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 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: 059
要了解当前状态
MySQL
服务器,使用以下命令。
# 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: 059
检查所有的运行状态
MySQL
服务器变量和值,键入以下命令。输出将类似于下面。
# 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 |
+------------------------------------------+-------------+
查看所有运行变量和值
MySQL
服务器,使用命令如下。
# 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 |
+---------------------------------------------------+----------------------------------------------+
以下命令将显示所有运行过程
MySQL
数据库查询。
# 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 |
+-------+---------+-----------------+---------+---------+------+-------+------------------+
在中创建新数据库
MySQL
服务器,使用如下所示的命令。
# 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: 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 (01 sec)
mysql>
将数据库放入
MySQL
服务器,使用以下命令。你将被要求确认按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
reload
命令告诉服务器重新加载授权表。refresh
命令刷新所有表并重新打开日志文件。
# mysqladmin -u root -p reload;
# mysqladmin -u root -p refresh
去关机
MySQL
服务器安全,键入以下命令。
mysqladmin -u root -p shutdown
Enter password:
你还可以使用以下命令来启动/停止 MySQL 服务器。
# /etc/init.d/mysqld stop
# /etc/init.d/mysqld start
以下是一些有用的刷新命令及其说明。
flush-hosts
:从主机缓存中刷新所有主机信息。flush-tables
: 刷新所有表。flush-threads
: 刷新所有线程缓存。flush-logs
: 刷新所有信息日志。flush-privileges
:重新加载授权表(与重新加载相同)。flush-status
: 清除状态变量。# 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
使用以下命令打印睡眠
MySQL
客户端进程。
# 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 |
+----+------+-----------+----+---------+------+-------+------------------+
现在,运行以下命令
kill
和process ID
如下所示。
# mysqladmin -u root -p kill 5
Enter password:
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 12 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+
如果你喜欢
kill
多个过程,然后通过process ID
用逗号分隔,如下所示。
# mysqladmin -u root -p kill 5,10
如果你想执行多个
mysqladmin
命令放在一起,那么命令会是这样的。
# 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: 003
mysqladmin Ver 42 Distrib 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 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: 003
连接远程
MySQL
服务器,使用-h
(host
) 和IP Address
远程机器。
# mysqladmin -h 1126 -u root -p
假设你想看
status
远程的MySQL
服务器,那么命令就是。
# mysqladmin -h 1126 -u root -p status
要在从服务器上启动/停止MySQL 复制,请使用以下命令。
# mysqladmin -u root -p start-slave
# mysqladmin -u root -p stop-slave
它告诉服务器将有关使用中的锁、已用内存和查询使用情况的调试信息写入
MySQL
日志文件
# mysqladmin -u root -p debug
Enter password:
了解更多选项和用法
myslqadmin
命令使用 help 命令,如下所示。它将显示可用选项列表。
# mysqladmin --help
相关文章