MySQL启动与更改密码
mysql启动基本原理说明:
/etc/init.d/mysqld是一个shell启动脚本,启动后最终会调用,mysqld_safe脚本,最后调用mysqld服务启动mysql,我们编辑/etc/init.d/mysqld,可以看到脚本,启动俩个进程mysqld和mysqld_safe,一般故障的时候我们用mysqld_safe来启动,
关闭mysql
1 mysqladmin - uroot -p密码 shut down
2 /etc/init.d/mysqld stop
3 kill USR2`cat path/pid`
优雅的关闭mysql但是不建议用killall杀掉所有的mysql进程,这样会导致mysql数据库起不来,所以网友遇到这样情况也很多,
我们登陆mysql后想分清出那个是正式环境那个是测试环境,
命令行修改登陆提示符
mysql> prompt\u@king\s->
PROMPT set to '\u@king\s->'
配置文件修改登陆提示符
在my.cnf配置文件中[mysql]模块下添加如下内容,保存后,无需重启myysql,退出当前的session,重新登陆
[mysql]
prompt=\\u@king\s->
登陆mysql
[root @king~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.5. MySQL Community Server (GPL)
Copyright (c) , , 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>
更改root密码
mysql> update user set password=password('') where user='root' and host='localhost'; #password('12345')是指定一个函数
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings:
mysql> flush privileges; #刷新,没刷新前是在内存里面
Query OK, rows affected (0.17 sec)
mysql>
说明,修改密码都需要刷新一下哦,linux找回mysql root用户密码
单实例mysql修改丢失root方法
1,首先停止mysql
/etc/init.d/mysql stop
2,使用--skip-grant-tables启动mysql,忽略授权登陆验证
1 mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
2 mysql -u root -p #说明-p登陆时密码是空,也可以不加-p,亲测哦(#^.^#)
3 update mysql.user set password=password("newpassword") where user='root'and host='localhost';
4 mysql> flush privileges;
5 mysql> quit
6 # /etc/init.d/mysql restart
7 # mysql -uroot -p
8 enter password: <输入新设的密码newpassword>
9 mysql>
多实例mysql修改丢失root方法
1关闭mysql
killall mysqld
2启动时加--skip-grant-tables参数
mysql_safe --defaults-file=/data//my.cnf --skip-grant-tables& mysql -u root -p -S /data//mysql.sock
3修改密码
mysql>update mysql.user set password=password("newpassword") where user='root'and host='localhost';
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)
SQl的分类
SQl结构化查询语言包含6个部分
- 数据查询语言(DQL(data query language)),作用从表中获取数据,关键字select,
- 数据操作语言(DMl(data manipulation language))作用处理表中的数据insert ,update,delete
- 事务处理语言(TPL)关键字begin,commit和 rollback
- 数据控制语言(DCl)grant(授权) 和revoke
- 数据定义语言(DDl)create和drop在数据库中创建新表或删除表 alter
- 指针控制语言(CCl) declare cursor, fetch into 和update where current用于对一个或多个表单独行的操作
查看数据库
show databases;或show database link '';或select database;
1 mysql> show databases;
2 +--------------------+
3 | Database |
4 +--------------------+
5 | information_schema |
6 | mysql |
7 | performance_schema |
8 | student |
9 | test |
10 +--------------------+
11 8 rows in set (0.01 sec)
创建数据库
命令语法:create database <数据库名> 注意库名字不能数字开头
mysql> create database king;
Query OK, row affected (0.01 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| king |
| mysql |
| performance_schema |
| student |
| test |
+--------------------+
6 rows in set (0.01 sec) mysql> show create database king\G
*************************** . row ***************************
Database: king
Create Database: CREATE DATABASE `king` /*!40100 DEFAULT CHARACTER SET utf8 */
row in set (0.00 sec) mysql>
创建一个指定字符集的数据库
mysql> CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; #红色指定编码,蓝色校验规则。
Query OK, row affected (0.01 sec)
删除数据库
drop database <数据库名字>
mysql> drop database test;
Query OK, row affected (0.07 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| king |
| mysql |
| performance_schema |
| student |
+--------------------+
5 rows in set (0.01 sec)
连接数据库
命令:use <数据库名>相当于linux下的cd切换目录的命令,use是切换数据库
mysql> use king;
Database changed
mysql> select database(); #查看当前的数据库,带()相当于函数,
+------------+
| database() |
+------------+
| king |
+------------+
row in set (0.00 sec)
查看数据库
select database ();相当于linux下的pwd
mysql> select version(); #查看当前的版本
+-----------+
| version() |
+-----------+
| 5.5. |
+-----------+
row in set (0.06 sec)
mysql> select user(); #查看当前的用户
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
row in set (0.04 sec)
mysql> select now(); #查看当前的时间
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set (0.02 sec)
mysql>
表操作
创建表并查看
create table <表名>(<字段名1><类型1>);
mysql> create table im(id int() not null, name varchar() not null default'QQ' #创建表id为int类型,name为varchar
);
Query OK, rows affected (0.59 sec)
mysql> show tables from king; #从king数据库中查看表
+----------------+
| Tables_in_king |
+----------------+
| im |
+----------------+
row in set (0.04 sec)
mysql> desc im; #查看表结构
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int() | NO | | NULL | |
| name | varchar() | NO | | QQ | |
+-------+-------------+------+-----+---------+-------+
rows in set (0.22 sec)
mysql>
show colums from im;查看表结构
mysql> show create table im \G; #\G Send command to mysql server,display result vertically向MySQL服务器发送命令,垂直显示结果
*************************** . row ***************************
Table: im
Create Table: CREATE TABLE `im` (
`id` int() NOT NULL,
`name` varchar() NOT NULL DEFAULT 'QQ'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
row in set (0.64 sec)
mysql表的字段类型
列类型 | 需要的存储量 |
tinyint | 1字节 |
smallint | 2个字节 |
mediumint | 3个字节 |
int | 4个字节 |
integer | 4个字节 |
bigint | 8个字节 |
float(X) | 4如果x<=24或8如果25<=X=53 |
float | 4个字节 |
double | 8个字节 |
double precision | 8个字节 |
real | 8个字节 |
decimal(M,D) | M字节(D+2,如果m<D) |
numeric(M,D) | M字节(D+2,如果m<D) |
为表的字段创建索引
创建主键索引,查询数据库,按主键查询是最快的,每一个表只有一个主键列,但是可以有多个普通的索引列,主键列要求列的内容必须唯一,而索引列不要求内容必须唯一。
create table student(
id int (4) not null auto_increment,
name char(20) not null,
age tinyint(2) not null default'0',
dept varchar(16) default null,
primary key (id),
key index_name(name));
mysql> create table student( id int () not null auto_increment, name char() n
ot null, age tinyint() not null default'',dept varchar() default null,primar
y key (id), key index_name(name));
Query OK, rows affected (0.18 sec)
auto_increment自增,primary key (id), 主键,key index_name(name));普通索引,
mysql> desc student; #查看创建的student表
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int() | NO | PRI | NULL | auto_increment |
| name | char() | NO | MUL | NULL | |
| age | tinyint() | NO | | | |
| dept | varchar() | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
rows in set (0.17 sec)
alter table student drop primary key ; 删除主键,测试不行,
mysql> alter table student drop primary key ;
ERROR (): Incorrect table definition; there can be only one auto column and it must be defined as a key
不正确的表定义;只能有一个自动COLUM它必须被定义为一个密钥
alter table student change id id int primary key auto_increment; 如果创建表忘记添加主键了,就执行这个。
创建普通索引分为唯一索引和普通索引
alter table student drop index index_name; # index固定索引,index_name是你创建表的时候的索引
alter table student add index index_name(name);
create index index_name on student (name(8)); # index固定索引(固定写法),index_name 随便写但要见名知意, on student 在哪个表上(student)8就是前8个字符创建索引。
create index index_name_dept on student (name,dept)
按条件列查询数据时,联合索引是有前缀生效特性的 index(a,b,c)仅a,ab,abc三个查询条件列可以走索引
查看表的索引
mysql> show index from student\G;
*************************** . row ***************************
Table: student
Non_unique:
Key_name: PRIMARY
Seq_in_index:
Column_name: id
Collation: A
Cardinality:
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** . row ***************************
Table: student
Non_unique:
Key_name: index_name
Seq_in_index:
Column_name: name
Collation: A
Cardinality:
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
rows in set (0.00 sec)
创建唯一索引
create unique index uni on student(name);
mysql> create unique index uni on student (name);
Query OK, rows affected (0.09 sec)
Records: Duplicates: Warnings:
mysql> desc student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int() | NO | PRI | NULL | auto_increment |
| name | char() | NO | UNI | NULL | |
| age | tinyint() | NO | | | |
| dept | varchar() | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
rows in set (0.02 sec)
- 要在表的列上创建索引
- 索引会加快查询的速度,但是会影响更新的速度
- 索引不是越多越好,要在频繁查询的where后的条件列上创建索引
- 小表或唯一值极少的列上不建索引,要在带包以及不同内容多的列上创建索引
往表中插入数据
create table test(
id int(4) not null auto_increment,
name char(20) not null,
primary key (id)
);
mysql> create table test(id int() not null auto_increment, name char() not n
ll,primary key (id));
Query OK, rows affected (0.01 sec)
mysql> show tables;
+----------------+
| Tables_in_king |
+----------------+
| student |
| test |
+----------------+
rows in set (0.00 sec)
mysql> insert into test (id,name) values(,'boy'); #插入数据,主键自增我们直接插name,insert into test(name) values('new');
Query OK, row affected (0.05 sec)
mysql> select *from test;
+----+------+
| id | name |
+----+------+
| | boy |
+----+------+
row in set (0.04 sec)
mysql> delete from test;
Query OK, row affected (0.05 sec)
mysql> select *from test;
Empty set (0.00 sec)
mysql> insert into test (id,name) values(,'boy'),(,'new');
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings:
查询数据
select *from 表名;
mysql> select *from test;
+----+------+
| id | name |
+----+------+
| | boy |
| | new |
+----+------+
rows in set (0.47 sec)
指定条件查询
mysql> select *from test where id =; #根据id查询
+----+------+
| id | name |
+----+------+
| | boy |
+----+------+
row in set (0.09 sec)
mysql> select *from test where name='new'; #根据name查询,字符串需要带引号
+----+------+
| id | name |
+----+------+
| | new |
+----+------+
row in set (0.03 sec)
多个条件查询and区交集,or取并集。排序 order by id desc(正序),sec(倒序)
mysql> select *from test where name='new'or id=;
+----+------+
| id | name |
+----+------+
| | boy |
| | new |
+----+------+
rows in set (0.08 sec)
explain查看索引、
mysql> explain select *from test where name='new'\G;
*************************** . row ***************************
id:
select_type: SIMPLE
table: test
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL #可以看到索引为空
ref: NULL
rows:
Extra: Using where
row in set (0.04 sec)
help explain;
修改表中指定条件固定列的数据
命令语法:update 表名 set 字段=新值,where 条件
mysql> update test set name='wangxinxia' where id=;
Query OK, row affected (0.90 sec)
Rows matched: Changed: Warnings:
mysql> desc test;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int() | NO | PRI | NULL | auto_increment |
| name | char() | NO | | NULL | |
+-------+----------+------+-----+---------+----------------+
rows in set (0.25 sec)
mysql> select* from test;
+----+------------+
| id | name |
+----+------------+
| | boy |
| | wangxinxia |
+----+------------+
rows in set (0.00 sec)
truncate table test;和delete from test的区别
- truncate 更快,清空物理文件
- delete逻辑清楚,按行删
删除数据
mysql> delete from student where id=3;
Query OK, 1 row affected (0.30 sec)
mysql> show tables;
+----------------+
| Tables_in_king |
+----------------+
| student |
| test |
+----------------+
2 rows in set (0.07 sec)
mysql> select * from student;
+----+----------+-----+-------+
| id | name | age | dept |
+----+----------+-----+-------+
| 1 | zhangsan | 20 | yanfa |
| 2 | zhaoliu | 21 | dba |
+----+----------+-----+-------+
增删改表的字段
语法alter table 表名 add sex char(4);
mysql> alter table student add sex char (4);
Query OK, 2 rows affected (0.54 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | UNI | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
| sex | char(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
5 rows in set (0.20 sec)
alter table 表名 add qq varchar(10)first;
mysql> alter table student add qq varchar(10) first;
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| qq | varchar(10) | YES | | NULL | |
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | UNI | NULL | |
| age | tinyint(2) | NO | | 0 | |
| dept | varchar(16) | YES | | NULL | |
| sex | char(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
6 rows in set (0.02 sec)
rename table 原表to新表
mysql> show tables;
+----------------+
| Tables_in_king |
+----------------+
| student |
| test |
+----------------+
2 rows in set (0.00 sec)
mysql> rename table test to test1;
Query OK, 0 rows affected (0.05 sec) mysql> show tables;
+----------------+
| Tables_in_king |
+----------------+
| student |
| test1 |
+----------------+
2 rows in set (0.00 sec)
alter table test1 rename to test;
mysql插入数据解决乱码问题
- set names 库的字符集,这种方法是临时的每次进来之前都需要执行
- source test.sql在这个文件中插入set names 库字符集
- 对已有数据,需要把数据导出去,重新建库建表,在导进来。
创建/查看/删除mysql系统的用户
语法:
- create user '用户名'@'主机名' identified by '用户名'; 创建用户
- select host,user from mysql.user; 查看用户
- drop user 用户名@'%';注意可以是单或者双引号,但不能不加,删除用户
- delete from mysql.user where user='用户名' and host=‘@后面指定的主机名’;
mysql> create user 'usrabc'@'%' identified by 'usrabc'; #创建usrabc用户
Query OK, rows affected (0.49 sec)
mysql> select host,user from mysql.user; #查看所有用户
+-----------+--------+
| host | user |
+-----------+--------+
| % | usrabc |
| localhost | root |
+-----------+--------+
rows in set (0.00 sec)
mysql> drop user usrabc@'%'; #删除usrabc用户
Query OK, rows affected (0.00 sec)
处理完用户最好刷新一下权限,flush privileges;
创建mysql用户及赋予用户权限
通过help grant 查看帮助,比较常用的创建用户的方法是:CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'
CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass'
GRANT ALL ON db1.* TO 'jeffrey'@'localhost' #这俩命令是先用create创建用户,然后在授权
GRANT SELECT ON db2.invoice TO 'jeffrey'@'localhost'
GRANT USAGE ON *.* TO 'jeffrey'@'localhost' WITH MAX_QUERIES_PER_HOUR
通过grant命令创建用户并授权,命令语法:grant all privileges on dbname.* to username@localhost identified by 'passwd';
grant | all privileges | on dbname.* | to username@localhost | identified by 'passwd' |
授权命令 | 对应权限 | 目标:库和表 | 用户名和客户端主机 | 用户密码 |
mysql> grant all privileges on king.* to liang@localhost identified by '';
#创建liang用户,对king库具备所有权限,允许从localhost主机登陆管理数据库,密码是123456.
Query OK, rows affected (0.01 sec)
mysql> flush privileges;
Query OK, rows affected (0.96 sec)
mysql> show grants for liang@localhost; #查看权限
+-------------------------------------------------------------------------------
-------------------------------+
| Grants for liang@localhost
|
+-------------------------------------------------------------------------------
-------------------------------+
| GRANT USAGE ON *.* TO 'liang'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74
329105EE4568DDA7DC67ED2CA2AD9' |
| GRANT ALL PRIVILEGES ON `king`.* TO 'liang'@'localhost'
|
+-------------------------------------------------------------------------------
-------------------------------+
rows in set (0.07 sec)
create和grant配合
mysql> create user zh@localhost identified by '';
Query OK, rows affected (0.04 sec)
mysql> grant all on king.* to zh@localhost;
Query OK, rows affected (0.00 sec)
mysql> flush privileges;
Query OK, rows affected (0.00 sec)
授权局域网内主机远程连接数据库grant all privileges on king.* to liang@10.0.0.% identified by '123456';把localhost改为ip就可以了。连接,mysql -uliang -p123456 -h10.0.0.%
mysql> show grants for zh@localhost; #查看zh用户权限
+-------------------------------------------------------------------------------
----------------------------+
| Grants for zh@localhost
|
+-------------------------------------------------------------------------------
----------------------------+
| GRANT USAGE ON *.* TO 'zh'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329
105EE4568DDA7DC67ED2CA2AD9' |
| GRANT ALL PRIVILEGES ON `king`.* TO 'zh'@'localhost'
|
+-------------------------------------------------------------------------------
----------------------------+
rows in set (0.16 sec) mysql> revoke insert on king.* from zh@localhost; #更改权限
Query OK, rows affected (0.04 sec) mysql> show grants for zh@localhost;
+-------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------------------------------------+
| Grants for zh@localhost |
+-------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'zh'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329
105EE4568DDA7DC67ED2CA2AD9'
|
| GRANT SELECT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE T
EMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, A
LTER ROUTINE, EVENT, TRIGGER ON `king`.* TO 'zh'@'localhost' |
+-------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-------------------------------------------------------------+
rows in set (0.06 sec)
mysql -uroot -p123456 -e "show grants for zh@localhost;"|grep -i grant #用linux中grep命令过滤需要的权限
Grants for zh@localhost
GRANT USAGE ON *.* TO 'zh'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329
105EE4568DDA7DC67ED2CA2AD9'
GRANT SELECT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE T
EMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, A
LTER ROUTINE, EVENT, TRIGGER ON `king`.* TO 'zh'@'localhost'
查看mysql的ALL PRIVILEGES权限mysql -uroot -p123456 -e "show grants for zh@localhost;"|grep -i grant|tail -1|tr ‘,’ ‘\n’ >a.txt
我们在授权用户最小的 满足业务需求的权限,而不是一味的授权ALl PRIVILEGES。