2018-07-11(mysql经常使用操做)

13.1 设置更改root密码

设置更改root密码目录概要html

/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';mysql

设置更改root密码linux

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

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

[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

二、设置mysql的root密码
安装完mysql后,默认root用户是没有密码的,咱们能够经过mysql自带的命令mysqladmin给root设置一个密码
格式:mysqladmin -uroot passwd '123456' sql

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

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

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

四、忘记密码的状况修改
第一步:修改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

一、本地链接,默认使用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>

二、使用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>

三、指定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>

四、不登录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;

一、建立库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)

二、使用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)

三、查看表的字段

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)

四、查看建表语句

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是为了竖型显示,更清晰
五、查看当前用户和库

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)

六、查询数据库版本

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

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

八、修改参数 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)

九、查看队列
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用户管理

一、建立一个用户并受权全部库和表的全部权限
格式: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)

二、针对指定的条件受权
格式: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)

三、针对指定的条件受权
格式: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)

四、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)

五、查看刚才受权的用户user2@'192.168.133.132'

mysql> show grants for 'user2'@'192.168.133.132';
+--------------------------------------------------------------------------------------------------------------------+
| Grants for user2@192.168.133.132                                                                                   |
+--------------------------------------------------------------------------------------------------------------------+
| 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

相关文章
相关标签/搜索