2018-07-11(mysql常用操作)

13.1 设置更改root密码

设置更改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密码

root用户是mysql的超级管理员用户,和linux系统的root用户类似,不过和Linux的不一样
默认mysql的 root 用户密码是空的,直接使用mysql -uroot就可以连接上去,不需要输入密码,但是不安全,所以就需要设置一个密码
为了方便使用mysql服务,将mysql目录加入到环境变量里"export PATH=/usr/local/mysql/bin:$PATH" 永久生效加入到"/etc/profile"里面,执行source /etc/profile 命令

1、查看mysql是否启动 ps aux|grep mysql,如果没有启动,执行"/etc/init.d/mysqld start"

[root@lnmp-server ~]# ps aux |grep mysql
root        841  0.0  0.1 115432  1728 ?        S    10:33   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/ --pid-file=/data/mysql//lnmp-server.pid
mysql      1057  6.4 45.1 1296356 451644 ?      Sl   10:33   0:04 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/data/mysql//lnmp-server.pid --socket=/tmp/mysql.sock --port=3306
root       1231  0.0  0.0 112720   972 pts/0    R+   10:34   0:00 grep --color=auto mysql

2、设置mysql的root密码
安装完mysql后,默认root用户是没有密码的,我们可以通过mysql自带的命令mysqladmin给root设置一个密码
格式:mysqladmin -uroot passwd '123456'

[root@lnmp-server ~]# mysqladmin -uroot password '123456'
Warning: Using a password on the command line interface can be insecure.

在设置密码的时候,会看到有输出信息,但这不是报错信息,这是告诉你 你现在密码在当前命令行显示出来了,这样不×××全
3、知道密码的情况下更改密码
格式:mysqladmin -uroot -p'123456' password '654321'

[root@lnmp-server ~]# mysqladmin -uroot -p'123456' password '654321'
Warning: Using a password on the command line interface can be insecure.

4、忘记密码的情况修改
第一步:修改mysql的配置文件 /etc/my.cnf 在mysqld模块下加入一行skip-grant,表示忽略授权

vi /etc/my.cnf   #在mysqld模块下新增一行
skip-gant
[root@lnmp-server ~]# cat /etc/my.cnf
[mysqld]
port = 3306
basedir=/usr/local/mysql
datadir=/data/mysql/
socket=/tmp/mysql.sock
user=mysql
default-time-zone=system
default-storage-engine=InnoDB
log-error=/var/log/mysqld.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
skip-grant                          #增加这一行

第二步:在更改配置文件后,重启mysql服务 /etc/init.d/mysqld restart

[root@lnmp-server ~]# /etc/init.d/mysqld restart
Shutting down MySQL. SUCCESS! 
Starting MySQL. SUCCESS!

第三步:连接mysql,这时候在输入mysql -uroot ,会发现直接进入mysql,而不需要密码了

[root@lnmp-server ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 Source distribution

Copyright (c) 2000, 2017, 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后,输入:update mysql.user set password=password('123456') where user='root';

mysql> update mysql.user set password=password('123456') where user='root';
Query OK, 0 rows affected (0.06 sec)
Rows matched: 4  Changed: 4  Warnings: 0
mysql> flush privileges;                                  #刷新权限,让它生效
Query OK, 0 rows affected (0.00 sec)
mysql> quit                                                    #输入quit,退出mysql
Bye

注:提示说4行修改完毕,即使有些行是空的
第五步:更新完后修改mysql的配置文件 /etc/my.cnf 删除增加的那一行skip-grant,去掉忽略授权,并重启mysql服务 /etc/init.d/mysqld restart

[root@lnmp-server ~]# cat /etc/my.cnf
[mysqld]
port = 3306
basedir=/usr/local/mysql
datadir=/data/mysql/
socket=/tmp/mysql.sock
user=mysql
default-time-zone=system
default-storage-engine=InnoDB
log-error=/var/log/mysqld.log
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#skip-grant                                           #这一行删掉或者注释掉

第六步:用更新完后密码登陆mysql -uroot -p'123456'

[root@lnmp-server ~]# 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 3
Server version: 5.6.36 Source distribution

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

13.2 连接mysql

1、本地连接,默认使用sock连接
格式:mysql -uroot -p‘123456’

[root@lnmp-server ~]# 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 3
Server version: 5.6.36 Source distribution

Copyright (c) 2000, 2017, 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、使用ip端口连接远程机器
格式:mysql -uroot -p'111111' -h[远程mysql主机IP] -P[端口],mysql默认端口3306

[root@lnmp-server ~]# mysql -uroot -p'123456' -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 4
Server version: 5.6.36 Source distribution

Copyright (c) 2000, 2017, 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、指定sock文件(只适合本机登陆)
格式:mysql -uroot -p'123456' -S/tmp/mysql.sock

[root@lnmp-server ~]# mysql -uroot -p'123456' -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 5
Server version: 5.6.36 Source distribution

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

4、不登陆mysql执行sql语句(常用于shell脚本)
格式:mysql -uroot -p123456 -e 'show databases;'

[root@lnmp-server ~]# mysql -uroot -p'123456' -e 'show databases;'
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

13.3 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;

1、创建库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)

2、使用db1库,创建表tb1(新建字段id 整数型长度为4 ,字段name 字符型长度为40),并查看表

mysql> use db1;
Database changed
mysql> create table tb1 (id int(4),name char(40));
Query OK, 0 rows affected (0.13 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| tb1           |
+---------------+
1 row in set (0.00 sec)

3、查看表的字段

mysql> desc tb1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(4)   | YES  |     | NULL    |       |
| name  | char(40) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

4、查看建表语句

mysql> show create table tb1\G;
*************************** 1. row ***************************
       Table: tb1
Create Table: CREATE TABLE `tb1` (
  `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

注:\G是为了竖型显示,更清晰
5、查看当前用户和库

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> select database();
+------------+
| database() |
+------------+
| db1        |
+------------+
1 row in set (0.00 sec)

6、查询数据库版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.36    |
+-----------+
1 row in set (0.00 sec)

7、查看各参数 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)

8、修改参数 set global max_connect_errors=1000; ——>仅在内存中生效,若想重启生效修改/etc/my.cnf

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)

9、查看队列
show processlist; //查看库的状况,比如,那些用户在连,做了些什么操作,是否锁表
show full processlist; //查看到的对列,最后一个会非常完成的显示出来

mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 14 | 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                   |
+----+------+-----------+------+---------+------+-------+------------------------+
| 14 | root | localhost | db1  | Query   |    0 | init  | show full  processlist |
+----+------+-----------+------+---------+------+-------+------------------------+
1 row in set (0.00 sec)

说明:在mysql中也支持上下方向键查看执行过的命令,命令历史保存在用户家目录下.mysql_history文件中

13.4 mysql用户管理

1、创建一个用户并授权所有库和表的所有权限
格式:grant all on . to 'user1' identified by 'passwd';

mysql> grant all on *.* to 'luo' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> select user from mysql.user;
+------+
| user |
+------+
| luo  |
| root |
| root |
|      |
| root |
|      |
| root |
+------+
7 rows in set (0.00 sec)

2、针对指定的条件授权
格式:grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.180.1' identified by 'passwd';
语句说明:授权查询 更新 插入 在数据库 db1所有表上 给来源ip为192.168.180.1的用户user2,并设定密码

mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.133.132' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

3、针对指定的条件授权
格式:grant all on db1.* to 'user3'@'%' identified by 'passwd';
语句说明:授权所有权限在数据库 db1所有表上 给来源ip为所有的用户user2,并设定密码 ,%表示通配,即所有的

mysql> grant all on db1.* to 'user2'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

4、show grants;
show grants;看的是root

mysql> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

5、查看刚才授权的用户user2@'192.168.133.132'

mysql> show grants for 'user2'@'192.168.133.132';
+--------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                   |
+--------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user2'@'192.168.133.132' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'192.168.133.132'                                               |
+--------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

13.5 常用sql语句

增删改查,就是mysql和其他关系型数据库常用的select语句操作命令
查询语句

首先登录root下的mysql mysql -uroot -p111111
使用db1库 use db1;
查看当前库的所有表show tables;
查看表的行数 select count(*) from mysql.user;
库和表中间有个分割符,就是用点 . 分割

mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|       10 |
+----------+
1 row in set (0.00 sec)

就是说user表有10行内容
查看所有的内容 select from mysql.db;(这样看起来会很乱) ——>可以在后面加上\G,如select from mysql.db\G;
这里的 * 表示查看所有内容
查看db库的所有内容 select db from mysql.db; 第一个db是字段

mysql> select db from mysql.db;
+---------+
| db      |
+---------+
| test    |
| test\_% |
| db1     |
| db1     |
+---------+
4 rows in set (0.01 sec)

查db字段和user字段 select db,user from mysql.db;

mysql> select db,user from mysql.db;
+---------+-------+
| db      | user  |
+---------+-------+
| test    |       |
| test\_% |       |
| db1     | user2 |
| db1     | user2 |
+---------+-------+
4 rows in set (0.00 sec)

模糊查询 select * from mysql.db where host like '192.168.%'; like 就是模糊匹配
插入语句

查看创建的表

mysql> desc db1.tb1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(4)   | YES  |     | NULL    |       |
| name  | char(40) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

查看db1.tb1表的内容,会发现为空 select * from db1.tb1;
插入数据到

insert into db1.tb1 values (1, 'abc');`

插入1, 'abc'到db1.tb1表
再来查询db1.tb1

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
+------+------+
1 row in set (0.00 sec)

这样就成功了插入了一条数据,在插入的时候 name 这个字段应该是是一个字符串,字符串需要加上一个单引号 ' ' ,数字可以不加单引号

mysql> insert into db1.tb1 values (1, 234);
Query OK, 1 row affected (0.01 sec)

mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
|    1 | 234  |
+------+------+
2 rows in set (0.00 sec)

这里没有做限制,这里id和name都可以是相同的,同一个字段里有相同的数字,相同的值 ,也可以做一些限制,在插入相同的id的时候,就会冲突
update操作

更改db1.t1表 的字符串为name 的数据 和 字符串为id 的数据
update db1.tb1 set name='aaa' where id=1;

mysql> update db1.tb1 set name='aaa' where id=1;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from db1.tb1;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
|    1 | aaa  |
+------+------+
2 rows in set (0.00 sec)

delete操作

删除db1.tb1表 的数据 和 字符串为id 的数据

delete from db1.t1 where id=1;
mysql> delete from db1.tb1 where id=1;
Query OK, 2 rows affected (0.01 sec)

mysql> select * from db1.tb1;
Empty set (0.00 sec)

truncate清空一个表

清空表数据 truncate table db1.tb1;
即使表的数据清空了,但表的字段依旧存在的

mysql> truncate table db1.tb1;
Query OK, 0 rows affected (0.02 sec)

mysql> select * from db1.tb1;
Empty set (0.00 sec)

mysql> desc db1.tb1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(4)   | YES  |     | NULL    |       |
| name  | char(40) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

truncate 只是清空的内容,而drop 会清空表的数据并清除表的框架
drop 会把表的框架也丢掉 drop table db1.tb1;

mysql> drop table db1.tb1;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from db1.tb1;    //因为表的架构已经不存在了
ERROR 1146 (42S02): Table 'db1.t1' doesn't exist
mysql> 

删除库

drop database db1;

总结

在使用mysql的时候,少用 这样的操作,因为若是一个表里面的内容很多,select count()这样操作就会很耗时,浪费资源
数据库中常用引擎是myisam和innodb,默认mysql库里面都是使用的myisam引擎
特点:myisam引擎,能自动去统计有多少行
在select count()查看表的时候会很快
use mysql;
show create table user\G;
特点:innodb引擎,不会自动统计行数,每次去查询,每次去统计行数,就会很耗时
use db1
show create table t1;
所以select count(
)这种操作尽量减少,会耗费太多资源

13.6 mysql数据库备份恢复

备份库
备份mysql库 mysqlbak.sql文件就是mysql的备份库文件

[root@lnmp-server ~]# mysqldump -uroot -p123456 mysql>/tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@lnmp-server ~]# ll /tmp/mysqlbak.sql 
-rw-r--r-- 1 root root 657033 7月  12 13:14 /tmp/mysqlbak.sql

我们可以通过mysqlbak.sql来恢复数据库,还可以恢复到另外一个数据库里面去
恢复库
这里把数据恢复到新的库里,创建一个新的库mysql2

[root@lnmp-server ~]# mysql -uroot -p123456 -e "create database mysql2;"
Warning: Using a password on the command line interface can be insecure.
[root@lnmp-server ~]# mysql -uroot -p123456 mysql2 </tmp/mysqlbak.sql 
Warning: Using a password on the command line interface can be insecure.

进入到数据库里面,在后面加一个mysql2 就会进入到mysql2数据库里面

[root@lnmp-server ~]# mysql -uroot -p123456 mysql2
Warning: Using a password on the command line interface can be insecure.

查看数据库

mysql> select database();
+------------+
| database() |
+------------+
| mysql2     |
+------------+
1 row in set (0.00 sec)

备份表
针对库里面的某一个表去做备份,只需要在 库后面 加上 表名字 即可备份,先库 在表,中间是空格
备份表格式:mysqldump -uroot -p123456 databasename tablename > /tmp/user.sql

[root@lnmp-server ~]# mysqldump -uroot -p123456 mysql user >/tmp/user.sql
Warning: Using a password on the command line interface can be insecure.

恢复表
恢复表的时候,只需要写库的名字,不需要去写表的名字
恢复表格式:mysql -uroot -p123456 mysql < /tmp/user.sql

[root@lnmp-server ~]# mysql -uroot -p123456 mysql </tmp/user.sql 
Warning: Using a password on the command line interface can be insecure.

恢复表到mysql2库

[root@lnmp-server ~]# mysql -uroot -p123456 mysql2 </tmp/user.sql 
Warning: Using a password on the command line interface can be insecure.

备份所有的库
格式:mysqldump -uroot -p123456 -A >/tmp/mysql_all.sql
-A 表示all所有的意思

[root@lnmp-server ~]# mysqldump -uroot -p111111 -A >/tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.

只备份表结构
格式:mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql
不需要表的数据,只需要表的结构
备份mysql2的表结构

[root@lnmp-server ~]#  mysqldump -uroot -p123456 -d mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.

两个机器的库备份,一个库备份到另一台机器上
解决:
首先两台机器能够通信
然后mysqldump -h 远程mysql-ip -uuser-ppassword dbname > /本地backup.sql
这样即可备份

扩展:
使用xtrabackup备份innodb引擎的数据库 innobackupex 备份 Xtrabackup 增量备份 http://zhangguangzhi.top/2017/08/23/innobackex%E5%B7%A5%E5%85%B7%E5%A4%87%E4%BB%BDmysql%E6%95%B0%E6%8D%AE/#%E4%B8%89%E3%80%81%E5%BC%80%E5%A7%8B%E6%81%A2%E5%A4%8Dmysql
mysql5.7 root密码更改 http://www.apelearn.com/bbs/thread-7289-1-1.html
myisam 和innodb引擎对比 http://www.pureweber.com/article/myisam-vs-innodb/
mysql 配置详解: http://blog.linuxeye.com/379.html
mysql调优: http://www.aminglinux.com/bbs/thread-5758-1-1.html
同学分享的亲身mysql调优经历: http://www.apelearn.com/bbs/thread-11281-1-1.html
SQL语句教程 http://www.runoob.com/sql/sql-tutorial.html
什么是事务?事务的特性有哪些? http://blog.csdn.net/yenange/article/details/7556094

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章