MySQL讲解

设置更改root密码
首先,直接使用mysql命令是会有提示“命令不存在”,原因是该命令没有加入系统环境变量。
暂时将其加入环境变量中:
[root@ma-1 ~]# export PATH=$PATH:/usr/local/mysql/bin/
这时,只是暂时将其加入环境变量中,关机后就使用不了了。要想使其永久生效,要将这条命令写入/etc/profile中
[root@ma-1 ~]# vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin/
保存退出后,重启
[root@ma-1 ~]# source /etc/profile
这时就可以使用mysql命令直接登陆了。
[root@ma-1 ~]# mysql -uroot -p
Enter password:
设置mysql密码为000000
[root@ma-1 ~]# mysqladmin -uroot password '000000'
登录
[root@ma-1 ~]# mysql -uroot -p000000
修改和重置密码
修改密码:
将root密码修改为shuai
[root@localhost ~]# mysqladmin -uroot -p000000 password 'shuai'

新密码登录:

[root@ma-1 ~]# mysql -uroot -pshuai
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 MySQL Community Server (GPL)

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> quit
Bye
---------------------------------------------
重置密码

1. 编辑my.cnf配置文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]下添加 skip-grant
重启mysql服务
[root@localhost ~]# /etc/init.d/mysqld restart
这时可以直接登陆mysql,不用密码。

2. 利用mysql库中的user表修改密码

[root@localhost ~]# mysql -uroot
use mysql
select * from user;
update user set password=password('重置后的密码') where user='root';
将root密码改为111111
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;
mysql> update user set password=password('111111') where user='root';
Query OK, 4 rows affected (0.04 sec)
Rows matched: 4 Changed: 4 Warnings: 0
退出mysql
进入/etc/profile中取消skip-grant
重启mysql服务。
[root@localhost ~]# /etc/init.d/mysqld restart
mysql5.7 root密码更改   http://www.apelearn.com/bbs/thread-7289-1-1.html

连接Mysql

mysql的几种连接方式
1. 直接输入用户名和密码。适用于本机用户连接本机mysql

mysql -uroot -p123456

2. 通过ip和端口连接mysql,适用于本机用户连接本机mysql或者本机用户连接远端mysql
mysql -uroot -p123456 -h127.0.0.1 -P3306

[root@ma-1 ~]# mysql -uroot -p111111 -h127.0.0.1 -P3306

3. 通过sock套接字文件连接mysql,适用于本机用户连接本机mysql(只适合本机)
mysql -uroot -p123456 -S/tmp/mysql.sock
[root@ma-1 ~]# mysql -uroot -p111111 -S/tmp/mysql.sock


4. 连接mysql的同时执行mysql语句
mysql -uroot -p123456 -e “show databases”
显示所有的数据库
[root@ma-1 ~]# mysql -uroot -p111111 -e "show databases"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
使用与shell脚本

Mysql常用命令

mysql命令语句完了一定要加’;’ 
mysql常用命令
查询库 show databases;
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.10 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; 
查看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.14 sec)
查看建表语句 show create table tb_name\G; 
\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.08 sec)
查看当前用户 select user();
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.30 sec)
记录mysql命令历史:
[root@ma-1 ~]# less .mysql_history
use\040mysql
select\040*\040from\040user
;
show\040databases
;
use\040mysql
use\040mysql;
show\040tables;
desc\040user;
show\040create\040table\040user\134G;
select\040user();
.mysql_history (END)
查看当前使用的数据库 select databsase();
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
创建库 create database db1;
mysql> create database db1;
Query OK, 1 row affected (0.15 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)); 
在数据库中,表在库中。
在db1库中创建t1表;
mysql> use db1
Database changed
mysql> create table t1(`id` int(4),`name` char(20));
Query OK, 0 rows affected (0.84 sec)

mysql> show create table t1\G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.06 sec)

ERROR:
No query specified
或者
mysql> create table t1(`id` int(4),`name` char(20)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.12 sec)
删除一个表(t1)
mysql> drop table t1;
Query OK, 0 rows affected (0.17 sec)
mysql> show tables;
Empty set (0.01 sec)
查看当前数据库版本 select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.36 |
+-----------+
1 row in set (0.07 sec)
查看数据库状态 show status;列出常用的数据
查看各参数 
show variables; 
show variables like ‘max_connect%’; 
%可以通配
修改参数 set global max_connect_errors=1000;
查看队列,查看mysql正在执行的操作
show processlist;
show full processlist;
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

mysql用户管理

创建用户
创建一个user1,受所有权限,密码是123456,只能在127.0.0.1机器下登录。
mysql> grant all on *.* to 'user1'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.45 sec)

创建user2用户,密码为000000,并授予其针对db1库SELECT,UPDATE,INSERT权限
MySQL > grant SELECT,UPDATE,INSERT on db1.* to'user2'@'192.168.1.11' identified by '000000';
创建user3,密码为000000,并针对所有IP授予其db1库所有权限
MySQL > grant all on db1.* to 'user3'@'%' identified by '000000';
当然,也可以使用create user ‘test’@’%’ identified by ‘123456’; 创建用户并授权。
grant all 代表所有的权限
*.* 第一个*代表库名。后面.*代表库中的表名
% 通配所有的IP

[root@localhost ~]# mysql -uuser1 -p000000
ERROR 1045 (28000): Access denied for user 'user1'@'localhost' (using password: YES)
[root@localhost ~]# mysql -uuser1 -p000000 -h127.0.0.1
因为默认使用sock通信,所以使用-h指定主机
查看用户的授权信息
1. 查看当前用户的授权信息
MySQL > show grants;
2. 查看指定用户的授权信息。root用户可用
MySQL > show grants for [email protected];

mysql> show grants for user1@'127.0.0.1';
+-----------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'user1'@'127.0.0.1' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+-----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)

常用sql语句

sql语句,对数据库操作无外乎增,删,改,查。
查找mysql库user表中的行数
mysql> select count(*) from mysql.user;
+----------+
| count(*) |
+----------+
| 7 |
+----------+
1 row in set (0.07 sec)
查询mysql库db表中的所有内容
select * from mysql.db\G;
查询mysql库db表中的db字段内容
mysql> select db from mysql.db;
+---------+
| db |
+---------+
| test |
| test\_% |
+---------+
2 rows in set (0.10 sec)
查询mysql库db表中的db和user字段内容
mysql> select db,user from mysql.db;
+---------+------+
| db | user |
+---------+------+
| test | |
| test\_% | |
+---------+------+
2 rows in set (0.00 sec)
来自ip模糊查询mysql库中的db表的所有的内容
select * from mysql.db where host like '192.168.%';
db1库t1表中插入数据(1 , ‘abc’)
mysql> insert into db1.t1 values (1, 'abc');
Query OK, 1 row affected (0.17 sec)

mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | abc |
+------+------+
1 row in set (0.04 sec)
db1库t1表中更新数据
mysql> update db1.t1 set name='aaa' where id=1;
Query OK, 1 row affected (0.15 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
+------+------+
1 row in set (0.00 sec)
删除表中某条数据delete from db1.t1 where id=1;
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 2 | abc |
+------+------+
2 rows in set (0.00 sec)

mysql> delete from db1.t1 where id=1;
Query OK, 1 row affected (0.03 sec)

mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 2 | abc |
+------+------+
1 row in set (0.00 sec)
清空表中的所有内容,表结构保留truncate table db1.t1;
mysql> select * from db1.t1;
+------+------+
| id | name |
+------+------+
| 2 | abc |
+------+------+
1 row in set (0.00 sec)

mysql> truncate db1.t1;
Query OK, 0 rows affected (0.24 sec)

mysql> select * from db1.t1;
Empty set (0.00 sec)
mysql> desc db1.t1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
删除库表,表结构同时删除
drop table db1.t1;
drop database db1;
对运维来讲,使用删除(drop, truncate)命令,一定要慎重,在小心都不过分。一旦删除错了某表,字段,那就损失惨重!

Mysql数据库备份和恢复

是在linux下操作的
备份库 
mysqldump -uroot -p000000 库名 > /tmp/mysql.sql
将mysql库备份为/tmp/mysql.sql
[root@shuai-01 ~]# mysqldump -uroot -p111111 mysql > /tmp/mysql.sql
恢复库 mysql -uroot -p000000 mysql < /tmp/mysql.sql
[root@shuai-01 ~]# mysql -uroot -p111111 mysql < /tmp/mysql.sql
备份表 mysqldump -uroot -p000000 mysql user > /tmp/user.sql 库后面空格加上表名
恢复表 mysql -uroot -p000000 mysql < /tmp/user.sql 
只用写库名就行了
备份所有库 mysqldump -uroot -p000000 -A >/tmp/123.sql
只备份表结构 mysqldump -uroot -p000000 -d mysql > /tmp/mysql.sql
备份用mysqldump , 恢复用mysql 
mysqldump针对比较小的数据进行备份比较合适。
扩展 
SQL语句教程  http://blog.51cto.com/zt/206 
什么是事务?事务的特性有哪些?  http://blog.csdn.net/yenange/article/details/7556094 
根据binlog恢复指定时间段的数据  http://www.centoscn.com/mysql/2015/0204/4630.html 
mysql字符集调整  http://xjsunjie.blog.51cto.com/999372/1355013
使用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 


你可能感兴趣的:(linux)