目录
一、设置更改root密码
二、连接mysql
三、mysql常用命令
一、设置更改root密码
- 检查mysql服务是否启动
[root@minglinux-01 ~] ps aux |grep mysql
root 828 0.0 0.0 115640 1824 ? S 03:15 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/minglinux-01.pid
mysql 1199 0.0 24.7 1320112 462216 ? Sl 03:15 0:59 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=minglinux-01.err --pid-file=/data/mysql/minglinux-01.pid --socket=/tmp/mysql.sock --port=3306
root 6529 0.0 0.0 112724 984 pts/0 S+ 21:34 0:00 grep --color=auto mysql
- mysql启动
[root@minglinux-01 ~] mysql
-bash: mysql: 未找到命令 //只单独输入一个mysql命令是不行的,因为Mysql相关命令在/usr/local/mysql/bin路径下,但该路径不在环境变量PATH中
[root@minglinux-01 ~] echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
[root@minglinux-01 ~] export PATH=$PATH:/usr/local/mysql/bin/ //临时生效,系统重启后失效
[root@minglinux-01 ~] echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin/
[root@minglinux-01 ~] echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile //永久生效
[root@minglinux-01 ~] source /etc/profile
[root@minglinux-01 ~] mysql -uroot -p //指定使用root用户登录mysql,-p参数用于指定密码。root用户是MySQL自带的管理员账户,默认是没有密码的。
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
mysql> quit //quit退出mysql
Bye
- 给root用户设定密码
[root@minglinux-01 ~] mysqladmin -uroot password '123456'
Warning: Using a password on the command line interface can be insecure. //警告明文密码不安全
[root@minglinux-01 ~] mysql -uroot //不指定密码无法登录,
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@minglinux-01 ~] mysql -uroot -p'123456'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
输入密码时需要加-p选项,后面可以直接跟密码。-p选项后面不可以有空格,密码可以不加单引号(但是密码中有特殊字符时就会出问题,所以最好还是加上单引号)。当然,-p选项后面也可以不加密码,以和用户交互的方式输入密码。
- 再次修改root用户密码
[root@minglinux-01 ~] mysqladmin -uroot -p'123456' password 'toor'
Warning: Using a password on the command line interface can be insecure.
[root@minglinux-01 ~] mysql -uroot -p'toor'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
- 在不知道root密码的情况下修改root密码
修改/etc/my.cnf配置文件,增加一行skip-grant
[root@minglinux-01 ~] vim /etc/my.cnf
7 [mysqld]
8 skip-grant //忽略授权,即用户登录mysql时不需要密码
[root@minglinux-01 ~] /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
···
[root@minglinux-01 ~] mysql -uroot //无需密码直接登录
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
- 通过修改mysql库中的user表来修改root密码
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from user; //user表这记录了用户名、密码、权限等信息
mysql> select password from user; //密码信息是加密的字符串,这些加密的字符串由password函数生成,所以修改密码时也需要password函数对密码进行加密
+-------------------------------------------+
| password |
+-------------------------------------------+
| *9CFBBC772F3F6C106020035386DA5BBBF1249A11 |
| |
| |
| |
| |
| |
+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> update user set password=password('123456') where user='root'; //修改密码为123456
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
- 测试
删除/etc/my.cnf中的skip-grant后重启mysql再进行测试
[root@minglinux-01 ~] vim /etc/my.cnf
[root@minglinux-01 ~] /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@minglinux-01 ~] mysql -uroot -p'123456'
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
二、连接mysql
mysql -uroot -p123456 //连接本机
mysql -uroot -p123456 -h127.0.0.1 -P3306 //指定IP和端口号远程连接
mysql -uroot -p123456 -S/tmp/mysql.sock //Socket连接
mysql -uroot -p123456 -e ''show databases'' //连接后执行操作,常用于shell脚本
[root@minglinux-01 ~] mysql -uroot -p123456 -e 'show databases'
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
三、mysql常用命令
- 查询库 show databases
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
- 切换库 use mysql
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
- 查看库里的表 show tables
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)
- 查看表里的字段 desc tb_name
mysql> desc user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| plugin | char(64) | YES | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum('N','Y') | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
43 rows in set (0.00 sec)
- 查看建表语句 show create table tb_name\G
mysql> show create table user\G;
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',
`ssl_cipher` blob NOT NULL,
`x509_issuer` blob NOT NULL,
`x509_subject` blob NOT NULL,
`max_questions` int(11) unsigned NOT NULL DEFAULT '0',
`max_updates` int(11) unsigned NOT NULL DEFAULT '0',
`max_connections` int(11) unsigned NOT NULL DEFAULT '0',
`max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
`plugin` char(64) COLLATE utf8_bin DEFAULT 'mysql_native_password',
`authentication_string` text COLLATE utf8_bin,
`password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
PRIMARY KEY (`Host`,`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Users and global privileges'
1 row in set (0.00 sec)
ERROR:
No query specified
命令加\G的目的是让列出来的结果竖排显示,这样看起来更清晰。
- 查看当前用户 select user()
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
[root@minglinux-01 ~] mysql -uroot -p123456 -h192.168.162.130 //
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.6.39-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> select user();
+-------------------+
| user() |
+-------------------+
| root@minglinux-01 | //这里ip地址192.168.162.130被反解析到主机名minglinux-01
+-------------------+
1 row in set (0.00 sec)
- 查看当前使用的数据库 select databsase()
mysql> select database();
+------------+
| database() |
+------------+
| NULL | //当前为NULL
+------------+
1 row in set (0.00 sec)
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> use mysql;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
- 创建库 create database db1
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
- 创建表 use db1; create table t1(
id
int(4),name
char(40))
mysql> use db1;
Database changed
mysql> create table t1(`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1 |
+---------------+
1 row in set (0.00 sec)
mysql> show create table t1\G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> drop table t1;
Query OK, 0 rows affected (0.00 sec)
mysql> create table t1(`id` int(4),`name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> show create table t1\G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(40) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 //字符集已改成utf8
1 row in set (0.00 sec)
ERROR:
No query specified
- 查看当前数据库版本 select version()
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.39-log |
+------------+
1 row in set (0.00 sec)
- 查看数据库状态 show status
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name | Value |
+-----------------------------------------------+-------------+
| Aborted_clients | 0 |
| Aborted_connects | 2 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 4 |
| Bytes_received | 1246 |
| Bytes_sent | 22400 |
| Com_admin_commands | 0 |
| 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 |
| Com_alter_user | 0 |
| Com_analyze | 0 |
| Com_begin | 0 |
| Com_binlog | 0 |
| Com_call_procedure | 0 |
| Com_change_db | 3 |
···
···
+-----------------------------------------------+-------------+
341 rows in set (0.00 sec)
- 查看各参数 show variables; show variables like 'max_connect%'
mysql> show variables; //这些参数均可以在my.cnf中定义
mysql> show variables like 'max_connect%'; //指定查看某个或某些参数
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 100 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
mysql> show variables like 'slow%';
+---------------------+-----------------------------------+
| Variable_name | Value |
+---------------------+-----------------------------------+
| slow_launch_time | 2 |
| slow_query_log | OFF |
| slow_query_log_file | /data/mysql/minglinux-01-slow.log |
+---------------------+-----------------------------------+
3 rows in set (0.00 sec)
- 修改参数 set global max_connect_errors=1000
mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
修改参数的值在内存中生效,重启失效。在/etc/my.cnf文件中修改则永久有效
- 查看队列 show processlist; show full processlist
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 11 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 11 | root | localhost | NULL | Query | 0 | init | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
使用show processlist显示的最后一列(info)可能不完整,使用show full processlist会显示完整
- Mysql历史命令记录文件
[root@minglinux-01 ~] ls -a|grep mysql_history
.mysql_history
[root@minglinux-01 ~] cat .mysql_history
_HiStOrY_V2_
show\040database;
show\040databases;
use\040mysql;
select\040*\040from\040user;
show\040databases;
use\040mysql;
show\040tables;
desc\040user;
show\040create\040table\040user\134G;
select\040user();
select\040database();
use\040mysql
use\040mysql;
select\040database();
create\040database\040db1;
show\040databases;
use\040db1;
create\040table\040t1(`id`\040int(4),\040`name`\040char(40));
show\040tables;
show\040create\040table\040t1\134G;
drop\040table\040t1;
create\040table\040t1(`id`\040int(4),`name`\040char(40))\040ENGINE=InnoDB\040DEFAULT\040CHARSET=utf8;
show\040create\040table\040t1\134G;
select\040version();
show\040status;
show\040variables;
show\040variables\040like\040'max_connect%'
show\040variables\040like\040'max_connect%';
show\040variables\040like\040'slow%';
set\040global\040max_connect_errors=1000;\040
show\040variables\040like\040'max_connect%';
show\040processlist;
show\040full\040processlist;
扩展
mysql5.7 root密码更改 http://www.apelearn.com/bbs/thread-7289-1-1.html
myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/
知乎上的答案 https://www.zhihu.com/question/20596402
mysql 配置详解:https://www.jb51.net/article/48082.htm
mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html
同学分享的亲身mysql调优经历: http://www.apelearn.com/bbs/thread-11281-1-1.html
网友评论