一、设置更改root密码

首次直接使用mysql会提示‘该命令不存在’,原因是还没有将该命令加入环境变量,如果要使用该命令,需要使用其绝对路径:/usr/local/mysql/bin/mysql,为了方便,先将其加入系统环境变量:

[[email protected] ~]# exprt PATH=$PATH:/usr/local/mysql/bin/

mysql命令路径暂时加入环境变量,系统重启后该变量会失效,若要永久生效,需要将其加入环境变量配置文件

[[email protected] ~]# vim /etc/profile
……
export PATH=$PATH:/usr/local/mysql/bin/
 
刷新配置文件(否则不生效):
[[email protected] ~]# source /etc/profile
设置 & 更改密码

首次登陆mysql,root用户没有密码,直接登录

[[email protected] ~]# mysql -uroot
#-u:=user,指定用户名
Welcome to the MySQL monitor.  Commands end with ; or \g.
……
mysql> quit

登录mysql之后可以进行与mysql相关的一些操作,但是设置mysql用户的密码需要执行以下操作
设置密码

[[email protected] ~]# mysqladmin -uroot password '123456'  
 
再次登录:
[[email protected] ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

设置密码后直接登录会报错(ERROR),需要输入密码登录

[[email protected]23 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql>

-p=passwd,使用密码登录,在此可以将密码直接输入在命令行(跟在-p后面,不加空格:-p’123456’<此处单引号可以不加,但是当密码中有特殊符号时必须加,所以在命令行输入密码时养成习惯:加单引号>),也可以不在命令行输入,只跟-p选项,然后根据提示信息:“Enter password”,输入密码进行登录(此方法不会暴露用户密码,安全)

更改密码
[[email protected] ~]# mysqladmin -uroot -p'123456' password '1234567'
 
[[email protected] ~]# mysql -uroot -p'1234567'
Welcome to the MySQL monitor.
mysql>

忘记密码,更改密码

先编辑mysql配置文件:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
skip-grant
#忽略授权!
datadir=/data/mysql
socket=/tmp/mysql.sock
 
重启mysql服务:
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS! 

完成该操作之后就可以任意登录mysql了(无需密码),所以此时mysql安全性很差,平时配置文件中一定不要添加该参数

[[email protected] ~]# mysql -uroot
Welcome to the MySQL monitor.  
mysql> use mysql;
#切换mysql库
Database changed
mysql> select * from user\G;
#查看用户的表信息,该表中存放的是用户相关信息(密码、授权…)
#G选项的作用是使输出信息有序显示,不加该选项,显示内容会很乱  
mysql> select password from user;
#查看用户密码,显示结果Wie加密字符串!  
mysql> update user set password=password('123456') where user='root';
Query OK, 4 rows affected (0.11 sec)
Rows matched: 4  Changed: 4  Warnings: 0
#将密码更改为‘123456’
mysql> quit

更改成功

恢复配置文件:
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
 
重启mysql服务:
[[email protected] ~]# /etc/init.d/mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL........... SUCCESS! 
 
登录:
[[email protected] ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> quit

二、连接mysql

远程连接:
使用IP/port连接

[[email protected] ~]# mysql -uroot -p123456 -h127.0.0.1 -P3306
Welcome to the MySQL monitor.
mysql> quit

-h:=host,指定IP;-P:=port,指定端口

本地连接:
使用socket连接

[[email protected] ~]# mysql -uroot -p123456 -S/tmp/mysql.sock
Welcome to the MySQL monitor.
mysql> quit

-S:=socket,指定socket。此方法只适用于本地连接,等同于“mysql -uroot -p123456”

显示所有数据库

[[email protected] ~]# 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               |
+--------------------+

该方法使用于shell脚本中

三、mysql常用命令

设置更改root密码、连接mysql、mysql常用命令、mysql用户管理、常用sql语句、mysql数据库备份恢复
切换库(use mysql)之后执行
设置更改root密码、连接mysql、mysql常用命令、mysql用户管理、常用sql语句、mysql数据库备份恢复
以上命令均需要在mysql下执行;在mysql中每行命令末尾加上分号,表示该行命令执行结束。 tb_name即table name()表名
如:

[[email protected] mysql]# 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 5
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> 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> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| time_zone                 |
| time_zone_leap_second     |
+---------------------------+
28 rows in set (0.00 sec)
 
mysql> desc time_zone;
+------------------+------------------+------+-----+---------+----------------+
| Field            | Type             | Null | Key | Default | Extra          |
+------------------+------------------+------+-----+---------+----------------+
| Time_zone_id     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| Use_leap_seconds | enum('Y','N')    | NO   |     | N       |                |
+------------------+------------------+------+-----+---------+----------------+
2 rows in set (0.11 sec)
mysql> show create table time_zone\G;
#G=grep筛选文字内容,规律显示出来
*************************** 1. row ***************************
       Table: time_zone
Create Table: CREATE TABLE `time_zone` (
  `Time_zone_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Use_leap_seconds` enum('Y','N') NOT NULL DEFAULT 'N',
  PRIMARY KEY (`Time_zone_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Time zones'
1 row in set (0.03 sec)
ERROR: 
No query specified
mysql> select user();
+----------------+
| user()         |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.07 sec)
mysql> select database();
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)
mysql> select * from user\G;
创建库:
mysql> create database db1;
Query OK, 1 row affected (0.02 sec)
创建表:
mysql> use db1;  
#先切换到指定库下
Database changed
mysql> create table t1(`id` int(4),`name` char(40));
#括号中是定义字段及字段格式,使用反引号引起来
Query OK, 0 rows affected (1.51 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.06 sec)
mysql> show status;
+-----------------------------------------------+-------------+
| Variable_name                                 | Value       |
+-----------------------------------------------+-------------+
| Aborted_clients                               | 0           |
| Aborted_connects                              | 0           |
+-----------------------------------------------+-------------+
 
mysql> show variables\G;
 
mysql> show variables like 'max_connect%'\G;
#like表示匹配;%是通配符
 
更改参数:
mysql> set global max_connect_errors=110;
Query OK, 0 rows affected (0.04 sec)
#在此只是临时更改,如果要永久更改,需要编辑配置文件
 
查看队列:
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  5 | root | localhost | db1  | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.01 sec)
 
mysql> drop table t1;
Query OK, 0 rows affected (0.32 sec)
 
mysql> drop database db1;
Query OK, 0 rows affected (0.10 sec)

MySQL5.7之更改root密码

与MySQL 5.6版本不同,在安装MySQL 5.7过程中(初始化)会自动生成root用户密码(随机),那么在安装完成后如何更改root用户密码?步骤如下:

查看默认密码

[[email protected] mysql]# cat /root/.mysql_secret
# The random password set for the root userat Fri Jan 10 20:00:34 2014 (local time): 3A)2DdJLkcF

已知默认密码,更改root密码

使用默认密码登录:
[[email protected] mysql]# /usr/local/mysql/bin/mysql -uroot -p'3A)2DdJLkcFP'
Welcome to the MySQL monitor.  
Your MySQL connection id is 3
Server version: 5.7.17
 
设置新密码:
方法1:
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
方法2:
mysql> SET PASSWORD FOR 'root'@localhost = PASSWORD('123456');
mysql> qui

不知道默认密码

编辑配置文件:
[[email protected] mysql]# vi /etc/my.cnf
 
[mysqld]
skip-grant-tables
datadir=/data/mysql
socket=/tmp/mysql.sock
#增加参数:skip-grant-tables
 
重启:  
[[email protected] mysql]# /etc/init.d/mysqld restart
 
登录:此时不需要密码
[[email protected] mysql]# /usr/local/mysql/bin/mysql -uroot 
 
更改密码:
mysql> update user set authentication_string=password('12456') where user='root';
mysql>quit
 
[[email protected] mysql]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
 
重启: 
[[email protected] mysql]# /etc/init.d/mysqld restar

四、mysql用户管理

创建用户并授权
指定登录IP

[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456';
#创建user1用户并授予其所有权限“*.*”(通配符)
#第一个*表示db_name;第二个*表示tb_name
#同时指定其来源IP127.0.0.1(即,只可通过此IP登录)
#此处可以使用通配符%,代表所有IP(一般不使用)
#设定密码:identified by
mysql> quit
Bye

指定登录socket

[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.
mysql> grant all on *.* to 'user2'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye

用户登录
使用IP登录

[[email protected] ~]# mysql -uuser1 -p123456 -h127.0.0.1
Welcome to the MySQL monitor.
mysql> quit
Bye

使用socket登录

[[email protected] ~]# mysql -uuser2 -p'123456'
Welcome to the MySQL monitor. 
mysql> exit
Bye

因为指定登录主机为localhost,所以该用户默认使用(监听)本地mysql.socket文件,不需要指定IP即可登录

对具体权限进行授权

[[email protected] ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> create database db1;
Query OK, 1 row affected (0.04 sec)
mysql> grant SELECT,UPDATE,INSERT on db1.* to 'user2'@'192.168.8.132' identified by '123456';
#创建user2用户,并授予其针对db1库SELECT,UPDATE,INSERT权限
mysql> grant all on db1.* to 'user'@'%' identified by '123456';
#创建user3,并针对所有IP授予其db1库所有权限

权限相关命令

[[email protected] ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> show grants;
#查看当前用户的权限
mysql> show grants for [email protected];
#查看指定用户的权限

更改权限

[[email protected] ~]# mysql -uroot -p'123456'
Welcome to the MySQL monitor.
mysql> GRANT USAGE ON *.* TO 'user2'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB743291105EE4568DDA7DC67ED2CA2AD9';
Query OK, 0 rows affected (0.03 sec)
 
mysql> GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for [email protected];
+--------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                   |
+--------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user2'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT SELECT, INSERT, UPDATE ON `db1`.* TO 'user2'@'127.0.0.1'                                               |
+--------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
 
mysql> quit
Bye

更改用户权限时,权限行的内容都要更改

五、常用sql语句

[[email protected] ~]# mysql -uroot -p'123456';
Welcome to the MySQL monitor.
mysql> use db1;
Database changed
#选择库
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
|       12 |
+----------+
1 row in set (0.04 sec)
#查看指定库的内容的行数
mysql> select * from mysql.db\G;
#查看库的所有内容
mysql> select db,user from mysql.db;
#查看库指定内容
mysql> select * from mysql.db where host like '192.168.%'\G;
#查看某些IP对应的库内容,like表示匹配
mysql> create table t1(`id` int(4),`name` char(40));
Query OK, 0 rows affected (0.39 sec)
#在db1库下创建表t1
mysql> select * from db1.t1;
Empty set (0.03 sec)
#查看表中信息:空表
mysql> insert into db1.t1 values(1,'abc');
Query OK, 1 row affected (0.09 sec)
#向表中插入内容
mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | abc  |
+------+------+
1 row in set (0.00 sec)
mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from db1.t1;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
+------+------+
1 row in set (0.00 sec)
#更改表中指定内容
mysql> delete from db1.t1 where id=1;
Query OK, 2 rows affected (0.10 sec)
#删除表中指定内容
mysql> select * from db1.t1;
Empty set (0.00 sec)
mysql> truncate db1.t1;
Query OK, 0 rows affected (0.09 sec)
#清空一个表中内容
mysql> drop table t1;
Query OK, 0 rows affected (0.04 sec)
#删除表
mysql> drop database db1;
Query OK, 0 rows affected (0.13 sec)
#删除库
mysql> use mysql;
mysql> delete from user where User='user1' and Host='127.0.0.1';
Query OK, 1 row affected (0.06 sec)
#删除用户,在删除用户前需要先指定表

# 六、MySQL数据库备份恢复 备份库
备份指定库:
[[email protected] ~]# mysqldump -uroot -p123456 mysql > /tmp/mysqlbak.sql
备份所有库:
[[email protected] ~]# mysqldump -uroot -p123456 -A > /tmp/mysql_all.sql

恢复库

[[email protected] ~]# mysql -uroot -p123456 < /tmp/mysqlbak.sql

备份表

备份指定表:
[[email protected] ~]# mysql -uroot -p123456 mysql user > /tmp/user.sql
 
只备份表结构:
[[email protected] ~]# mysqldump -uroot -p123456 -d mysql > /tmp/mysql_tb.sql

恢复表

[[email protected] ~]# mysql -uroot -p123456 mysql user < /tmp/user.sql

使用xtrabackup备份innodb引擎的数据库 innobackupex 备份 Xtrabackup 增量备份 http://zhangguangzhi.top/2017/08/23/innobackex工具备份mysql数据/#三、开始恢复mysql
相关视频
链接:http://pan.baidu.com/s/1miFpS9M 密码:86dx
链接:http://pan.baidu.com/s/1o7GXBBW 密码:ue2f

扩展
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
根据binlog恢复指定时间段的数据 https://blog.csdn.net/lilongsy/article/details/74726002
mysql字符集调整 http://xjsunjie.blog.51cto.com/999372/1355013

相关文章: