标签:linux
十三周四次课(3月22日)13.1 设置更改root密码
/usr/local/mysql/bin/mysql -uroot
更改环境变量PATH,增加mysql绝对路径
mysqladmin -uroot password '123456'
mysql -uroot -p123456
密码重置
vi /etc/my.cnf//增加skip-grant
重启mysql服务 /etc/init.d/mysqld restart
mysql -uroot
use mysql;
update user set password=password('aminglinux') where user='root';
root用户是mysql的超级管理员用户,和linux系统的root用户类似,不过和Linux的不一样
默认mysql的 root 用户密码是空的,直接就可以连接上去,不需要输入密码,但是不安全,所以就需要设置一个密码
为了方便使用mysql服务,将mysql目录加入到环境变量里
1.打开系统,查看mysql是否启动
[root@tianqi-01 vhost]# ps aux |grep mysql
root 838 0.0 0.1 115388 1680 ? S 20:05 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/tianqi-01.pid
mysql 998 0.0 46.0 981248 460236 ? Sl 20:05 0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/tianqi-01.err --pid-file=/data/mysql/tianqi-01.pid --socket=/tmp/mysql.sock
root 4175 0.0 0.0 112664 984 pts/0 S+ 21:58 0:00 grep --color=auto mysql
[root@tianqi-01 vhost]#
2.若是没有启动mysql的话,将mysql启动起来
[root@tianqi-01 ~]# /etc/init.d/mysqld start
Starting MySQL [ OK ]
[root@tianqi-01 ~]# 180321 22:06:42 mysqld_safe A mysqld process already exists
[root@tianqi-01 ~]#
[root@tianqi-01 ~]# !ps
ps aux |grep mysql
root 838 0.0 0.1 115388 1680 ? S 20:05 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/tianqi-01.pid
mysql 998 0.0 46.0 981248 460236 ? Sl 20:05 0:06 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/tianqi-01.err --pid-file=/data/mysql/tianqi-01.pid --socket=/tmp/mysql.sock
root 4418 0.0 0.0 112660 984 pts/1 R+ 22:08 0:00 grep --color=auto mysql
[root@tianqi-01 ~]#
3.在启动mysql后,使用mysql -uroot命令,但是mysql命令会提示不存在,因为安装的mysql是在/usr/local/mysql/bin/mysql,而这个目录并不在环境变量PATH里面,所以它会报错
[root@tianqi-01 ~]# mysql -uroot
-bash: mysql: command not found
[root@tianqi-01 ~]# ls /usr/local/mysql/bin/mysql
/usr/local/mysql/bin/mysql
[root@tianqi-01 ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@tianqi-01 ~]#
4.若想要这个命令直接运行,需要把PATH做一个更改
[root@tianqi-01 ~]# export PATH=$PATH:/usr/local/mysql/bin/
[root@tianqi-01 ~]#
5.这时再来使用mysql -uroot命令就会发现可以使用了
退出mysql输入 quit 即可
[root@tianqi-01 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> quit
Bye
[root@tianqi-01 ~]#
6.若想要变量永久生效,还需要将export PATH=$PATH:/usr/local/mysql/bin/ 放入到 /etc/profile
//将内容放到配置文件的最后面,使命令永久生效
export PATH=$PATH:/usr/local/mysql/bin/
保存退出
7.假设若是没有运行 export PATH=$PATH:/usr/local/mysql/bin/ 命令,那也不能运行mysql,因为变量还没有生效,想要这个变量生效,在配置文件中加入命令后,还需要执行source /etc/profile 命令,重新加载
[root@tianqi-01 ~]# source /etc/profile
[root@tianqi-01 ~]#
8.一般是使用mysql -uroot -p命令
-p,表示指定密码
9.密码为空的时候,直接回车就可进入到mysql,并可以在其中操作一些mysql的一些行为
[root@tianqi-01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> quit
Bye
[root@tianqi-01 ~]#
10.退出mysql,输入 quit 即可
11.设置mysql密码,命令为mysqladmin -uroot passwd 'tianqi.1' 在 ' ' 为密码
[root@tianqi-01 ~]# mysqladmin -uroot password 'tianqi.1'
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]#
12.在设置密码的时候,会看到有输出信息,但这不是报错信息,这是告诉你你现在密码在当前命令行显示出来了,这样不×××全
13.这时在想直接登录mysql,就会提示需要输入密码了
[root@tianqi-01 ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
14.再使用-p,并输入密码就可以正常的进入到mysql命令行了
[root@tianqi-01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> quit
Bye
[root@tianqi-01 ~]#
知道mysql的root密码,去更改密码
1.若是这时知道mysql密码,去修改mysql密码,看到输出的提示信息不用去理会
格式
mysqladmin -uroot -p'hanfeng.1' password 'hanfeng'
[root@tianqi-01 ~]# mysqladmin -uroot -p'tianqi.1' password 'tianqi.2'
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]#
2.指定新密码去登录,当然也可以不明文指定密码,直接-p回车,输入密码登录也行
[root@tianqi-01 ~]# mysql -uroot -p'tianqi.2'
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.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> quit
Bye
[root@tianqi-01 ~]#
[root@tianqi-01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>
3.在明文指定密码的时候,密码可以加单引号,也可以不加单引号,建议加上单引号,防止密码有特殊符号的存在——>(若是不加单引号,而密码中又有特殊符号,就有可能会不识别)
不知道mysql的root密码,去更改密码
1.在不知道mysql的root用户密码的时候,先去更改 /etc/my.cnf 下配置文件中加入skip-grant
skip-grant ,表示忽略授权,也就是说操作mysql的时候不需要用户名和密码了,能直接登录
[root@tianqi-01 ~]# vim /etc/my.cnf
//在[mysqld]下面
加入一行
skip-grant
保存退出
2.在更改配置文件后,重启mysql服务
[root@tianqi-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL... [ OK ]
Starting MySQL....... [ OK ]
[root@tianqi-01 ~]#
3.这时候再输入mysql -uroot ,会发现直接进入mysql,而不需要密码了
[root@tianqi-01 ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> quit
Bye
[root@tianqi-01 ~]#
4.在登录进mysql后,还需要更改一个表,因为用户名和密码是存在于一个mysql库里面的,使用 use mysql; 切换库,在切换到mysql库里面,然后去更改一个存用户名密码的user表
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
mysql>
5.查看user表,输入select * from user; 命令,会看到输出乱七八糟的乱码,里面存放的就是用户名和密码,还有权限和授权等信息
mysql> select * from user;
………………
| Host | User | Password
………………
//省略指出都是乱码
6.查看password表,会看到密码都是加密的
mysql> select password from user where user='root';
+-------------------------------------------+
| password |
+-------------------------------------------+
| *1D6E44B005B8029764C395EE17EBA018CB2DD9A2 |
| |
| |
| |
+-------------------------------------------+
4 rows in set (0.05 sec)
mysql>
7.更改user表,使用update user set password=password('aminglinux') where user='root'; 命令
update user set password=password('aminglinux') where user='root';
密码字段 函数 //用于加密密码 高亮部分:为条件语句
mysql> update user set password=password('aminglinux') where user='root';
Query OK, 4 rows affected (0.10 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql>
8.提示说4行修改完毕,即使有些行是空的
9.这样密码就更改成功了,输入quit退出数据库即可
mysql> quit
Bye
[root@tianqi-01 ~]#
10.再去 /etc/my.cnf 配置文件中删除免授权配置,即删除skip-grant——>若是不删除,那么之后所有的用户都不需要输入密码,就可以登录进去,这样安全性太低
[root@tianqi-01 ~]# vim /etc/my.cnf
//在[mysqld]下面删除刚添加的
skip-grant
保存退出
11.重启mysql服务
[root@tianqi-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. [ OK ]
Starting MySQL....... [ OK ]
[root@tianqi-01 ~]#
12.重启完之后,再用新的密码测试下,会看到新的密码可以登录
[root@tianqi-01 ~]# mysql -uroot -paminglinux
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.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> quit
Bye
[root@tianqi-01 ~]#
13.这样就是成功更改mysql密码
13.2 连接mysql
连接mysql
本地连接——>即使没有指定,但默认使用sock连接,使用/tmp/mysql.sock连接
mysql -uroot -p123456 //输入用户名和密码连接本机
使用ip端口连接远程机器
mysql -uroot -p123456 -h[远程mysql主机IP] -P[端口]
[root@tianqi-01 ~]# mysql -uroot -paminglinux -h127.0.0.1 -P3306
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.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> quit
Bye
[root@tianqi-01 ~]#
使用sock方式连接(只适合在本机使用)如为指定IP就用sock访问
mysql -uroot -p123456 -S/tmp/mysql.sock
[root@tianqi-01 ~]# mysql -uroot -paminglinux -S/tmp/mysql.sock
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 10
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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> quit
Bye
[root@tianqi-01 ~]#
连接mysql之后执行命令(常用于shell脚本)
mysql -uroot -p123456 -e “show databases”
show databases //列出所有数据库
[root@tianqi-01 ~]# mysql -uroot -paminglinux -e “show databases”
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
[root@tianqi-01 ~]#
13.3 mysql常用命令
mysql常用命令目录概要
查询库 show databases;
切换库 use mysql;
查看库里的表 show tables;
查看表里的字段 desc tb_name;
查看建表语句 show create table tb_name\G;
查看当前用户 select user();
查看当前使用的数据库 select database();
创建库 create database db1;
创建表 use db1; create table t1(id
int(4), name
char(40));
查看当前数据库版本 select version();
查看数据库状态 show status;
查看各参数 show variables; show variables like 'max_connect%';
修改参数 set global max_connect_errors=1000;
查看队列 show processlist; show full processlist;
mysql内部命令和linux系统命令不通用
库是由表组成,表是由字段组成
在mysql 中使用的命令需要使用分号 ; 结尾,否则会出问题
1.登录到mysql
[root@tianqi-01 ~]# mysql -uroot -paminglinux
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.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>
2.查看数据库 show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>
3.切换库 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
mysql>
4.列出所有的表 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.01 sec)
mysql>
5.查看表里的字段 desc tb_name;
6.例子
desc user; //查看user表里的所有字段;需要进入相关的库以后才能查看对应的表
//库是由表组成的,表是由字段组成的
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.03 sec)
mysql>
//Field就是代表了字段
7.查看建表语句 show create table tb_name\G;——>G 表示竖排显示
8.例子
show create table user\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)
mysql>
//这样的结果比上面的表示还要详细
9.查看当前用户 select user();
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.01 sec)
mysql>
这里的localhost 表示主机名,会反解析你的IP所属主机名
10.在mysql里面使用 方向键 可以查看之前使用的命令,和shell的命令类似,并且也有记录命令历史的功能,默认的缓存文件在 / 下,名字为“.mysql_history”
支持ctrl+l 清屏
[root@tianqi-01 ~]# ls -la
total 68
dr-xr-x---. 6 root root 238 Mar 22 21:36 .
dr-xr-xr-x. 18 root root 256 Feb 27 08:31 ..
-rw-------. 1 root root 1420 Jan 26 09:23 anaconda-ks.cfg
-rw-------. 1 root root 27003 Mar 21 22:36 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw------- 1 root root 716 Mar 22 21:36 .mysql_history
drwxr----- 3 root root 19 Feb 27 20:16 .pki
drwxr-xr-x 2 root root 6 Mar 5 08:43 practice
-rw------- 1 root root 1024 Mar 21 20:44 .rnd
drwxr-xr-x. 2 root root 157 Jan 26 10:23 shell
drwx------ 2 root root 29 Mar 19 22:52 .ssh
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw------- 1 root root 7460 Mar 22 19:52 .viminfo
[root@tianqi-01 ~]#
[root@tianqi-01 ~]# less .mysql_history
_HiStOrY_V2_
use\040mysql
select\040*\040from\040user;
show\040databases;
use\040mysql;
show\040tables;
desc\040users;
desc\040user;
show\040create\040table\040user\134G
show\040create\040table\040user
show\040create\040table\040user;
show\040create\040table\040user\040show\040create\040table\040user;
show\040create\040table\040user
show\040create\040user;
show\040create\040table\040user;
select\040*from\040user;
show\040create\040table\040user;
show\040create\040table\040user\134G;
use\040user;
show\040databases;
use\040mysql;
show\040tables;
desc\040user;
show\040create\040table\040user;
show\040create\040table\040user\134G;
select\040*\040from\040user;
select\040*\040from\040user\134G;
select\040user();
11.查看当前使用的数据库 select database();
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql>
//NULL代表为空
mysql> use mysql; //切换到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 database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
mysql>
1.创建库 create database db1;
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql>
2.查看db1库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql>
3.切换到db1库下面去
mysql> use db1;
Database changed
mysql>
4.创建表 create table t1(id
int(4), name
char(40));——>定义 id 和 name ,并用反引好括起来
mysql> create table t1(id int(4), name char(40));
Query OK, 0 rows affected (0.17 sec)
mysql>
5.查看创建的表t1,默认使用的是InnoDB 引擎
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.12 sec)
ERROR:
No query specified
mysql>
6.在数据库里面取消命令,即在命令的最前面加一个 # 号就不会生效了
7.删除表 drop table t1;
mysql> drop table t1;
Query OK, 0 rows affected (0.08 sec)
mysql>
8.创建t1表
mysql> create table t1(`id` int(4), `name` char(40)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.03 sec)
mysql>
9.查看表,会看到变成了utf8
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
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
10.查看当前数据库版本 select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35 |
+-----------+
1 row in set (0.05 sec)
mysql>
11.查看数据库状态 show status; 它会把常用的数据都列出来
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name | Value |
+-----------------------------------------------+-------------+
| Aborted_clients | 0 |
| Aborted_connects | 10 |
| Binlog_cache_disk_use | 0 |
| Binlog_cache_use | 0 |
| Binlog_stmt_cache_disk_use | 0 |
| Binlog_stmt_cache_use | 0 |
| Bytes_received | 1235 |
| Bytes_sent | 22520 |
| Com_admin_commands | 0 |
| Com_assign_to_keycache | 0 |
| Com_alter_db | 0 |
| Com_alter_db_upgrade | 0 |
…………………………………………//下面省略很多
12.查看各参数
show variables;
show variables like 'max_connect%'; //mysql下 % 为通配符
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/tianqi-01-slow.log |
+---------------------+--------------------------------+
3 rows in set (0.00 sec)
mysql>
13.修改参数 set global max_connect_errors=1000; ——>仅在内存中生效
mysql> set global max_connect_errors=1000;
Query OK, 0 rows affected (0.04 sec)
mysql> show variables like 'max_connect%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| max_connect_errors | 1000 |
| max_connections | 151 |
+--------------------+-------+
2 rows in set (0.00 sec)
mysql>
若想重启依然生效,那需要修改配置文件/etc/my.cnf
在[mysqld]里面定义
14.查看队列
show processlist; //查看库的状况,比如,那些用户在连,做了些什么操作,是否锁表
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 19 | root | localhost | db1 | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 19 | root | localhost | db1 | Query | 0 | init | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
mysql>
标签:linux
原文地址:http://blog.51cto.com/13184900/2090047