进入并修改提示符,临时生效
mysql propmt="(\u@\h) \D [\d)]>_"
需要写入文件/etc/
登陆数据库
mysql -uroot -pcentos
进入mysql这个数据库
use mysql
查看当前数据库系统二进制程序的主目录
show variables like "basedir"
查看当前系统数据库对外提供服务的服务端口
show variables like "port"
查看当前数据库使用的字符集
show variables like "%character_set%"
刷新权限配置
修改完权限以后 一定要刷新服务,或者重启服务,刷新服务用的
flush privileges
查看数据库引擎
show engines;
SHOW VARIABLES LIKE 'have%' ;
查询默认存储引擎:
SHOW VARIABLES LIKE 'storage_engine' ;
清除mysql管理账号root密码
修改主配置文件
vim /etc/my.cnf
在[mysqld]的段下添加上一句
skip_grant_tables
重启服务
systemctl restart mariadb
直接进入mysql
>update user set password=password("new_pass") where user="root";
>flush privileges;
进入数据库,修改密码的指令
update mysql.user set password=password("centos");
防止在没写where条件下误操作删除,为了不让删除,如下操作
在/etc/my.cnf.d/client.cnf 文件
[client]下面添加如下行
safe_updates
更改默认的存储引擎,
可以在my.ini中进行修改。将“default-storage-engine=INNODB”更改为“default-storage-engine= MyISAM”。然后重启服务,修改生效。
查看支持所有字符集:
SHOW CHARACTER SET;
查看支持所有排序规则:
SHOW COLLATION;
创建数据库
create schema [数据库名称] default character set utf8 collate utf8_general_ci;--创建数据库
采用create schema和create database创建数据库的效果一样。
mysql> CREATE DATABASE 库名;
CREATE DATABASE IF NOT EXISTS 库名 DEFAULT CHARSET utf8;
删除数据库:
mysql> DROP DATABASE 库名;
查询所有数据库
show databases;
创建表
mysql> USE 库名;
mysql> CREATE TABLE 表名 (字段名 VARCHAR(20), 字段名 CHAR(1));
修饰符
所有类型:
NULL 数据列可包含NULL值
NOT NULL 数据列不允许包含NULL值
DEFAULT 默认值
PRIMARY KEY 主键
UNIQUE KEY 唯一键
CHARACTER SET name 指定一个字符集 数值型
AUTO_INCREMENT 自动递增,适用于整数类型
UNSIGNED 无符号 表示0到原本范围的最大值
举例:
use dongwu;
create table pet(
name varchar(20), #名字
owner varchar(20), #主人
species varchar(20), #种类
sex char(1), #性别
birth date, #出生日期
death date #死亡日期
)
查询所有表
show tables;
查询表中所有的列
show columns from 表名
-- 或
describe 表名;
查看表的状态,可看使用的是什么存储引擎
SHOW TABLE STATUS LIKE 'people'\G
查询表结构
DESC 表名;
MariaDB [testdb]> desc people;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(20) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
查看表上的索引
SHOW INDEXES FROM [数据库名.]表名;
删除数据表:
mysql> DROP TABLE 表名;
将表中记录清空:
mysql> DELETE FROM 表名;
添加一个字段
alter table 表名 add 字段名 VARCHAR(20) null;
重命名表:
rename table 原表名 to 新表名;
ALTER TABLE 原表名 RENAME 新表名;
添加id字段
alter table 表名 add id int not null primary key auto_increment first;
修改字段并修改字段名
ALETR TABLE 表名 CHANGE id nid int UNSIGNED NOT NULL PRIMARY KEY;
设置某个字段为唯一键
ALTER TABLE 表名 ADD UNIQUE KEY(字段名);
删除某个字段
ALTER TABLE 表名 DROP COLUMN 字段名;
修改字段的类型
ALTER TABLE 表名 MODIFY 字段 int;
设置索引:
ALTER TABLE A ADD INDEX index_B(B);
CREATE INDEX index_B ON A (B);
这两句话的意思一样,为 A 表增加一个索引,索引建立在 B 字段上,给这个索引起个名字叫index_B。前者是修改,后者是创建
删除索引:
DROP INDEX index_name ON tbl_name;
创建用户
create user '[用户名称]'@'%' identified by '[用户密码]';
--创建用户
密码8位以上,包括:大写字母、小写字母、数字、特殊字符
%:匹配所有主机,该地方还可以设置成‘localhost’,代表只能本地访问,例如root账户默认为‘localhost‘
user表中host列的值的意义
% 匹配所有主机
localhost localhost不会被解析成IP地址,直接通过UNIXsocket连接
127.0.0.1 会通过TCP/IP协议连接,并且只能在本机访问;
::1 ::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1
-- 用户密码30天过期
CREATE USER IF NOT EXISTS 'test' IDENTIFIED BY 'test' PASSWORD EXPIRE INTERVAL 30 DAY
-- 用户密码永不过期
CREATE USER IF NOT EXISTS 'test' IDENTIFIED BY 'test' PASSWORD EXPIRE NEVER
修改密码
三种方法:
1)SET PASSWORD FOR 'user'@'host' = PASSWORD(‘password');
- UPDATE user SET password=PASSWORD('password') WHERE User='test' ;
注意:上面修改表的命令不会马上生效,需执行FLUSH PRIVILEGES生效
- mysqladmin -u root –poldpassword password ‘newpassword‘
重命名用户
RENAME USER test TO new_test;
查看当前用户
select user();
查看数据库系统所有用户
SELECT User,Host,Password FROM user; 进入具体数据库
SELECT User,Host,Password FROM mysql.user; 没进入数据库
删除用户
delete from mysql.user where user='tester';--删除用户
DROP USER IF EXISTS test;
DROP USER ''@'localhost'; --删除默认的空用户
授权用户
GRANT priv_type,... ON [object_type] db_name.tb_name TO 'user'@'host' [IDENTIFIED BY 'password'] [WITH GRANT OPTION];
priv_type: ALL [PRIVILEGES]
db_name.tb_name:
*.*: 所有库的所表
db_name.*: 指定库的所有表
db_name.tb_name: 指定库的指定表
db_name.routine_name:指定库的存储过程和函数
WITH GRANT OPTION:表示是否有权限可授权
grant select,insert,update,delete,create on [数据库名称].* to [用户名称];
-- 授权testdb库的所有权限给用户test
GRANT ALL ON testdb.* TO 'test'@'localhost';
-- 只把testdb库的select、insert、update授权给用户test
GRANT SELECT, INSERT,UPDATE ON testdb.* TO 'test';
--还可以只授权某个表,甚至某个列给用户
GRANT ALL ON testdb.user.* TO 'test';
查看哪些用户获得了什么授权
SHOW GRANTS ;
查看指定用户获得的授权
SHOW GRANTS FOR 'user'@'host';
撤销授权
REVOKE priv_type, ... ON db_name.tb_name FROM 'user'@'host
revoke all on . from tester;--取消用户所有数据库(表)的所有权限
示例:
REVOKE DELETE ON testdb.* FROM 'testuser'@'%‘
注意:MariaDB服务进程启动时会读取mysql库中所有授权表至内存
(1)GRANT或REVOKE等执行权限操作会保存于系统表中,MariaDB的服务进程通常会自动重读授权表,使之生效
(2)对于不能够或不能及时重读授权表的命令,可手动让MariaDB的服务进程重读授权表:mysql> FLUSH PRIVILEGES;
在Ubuntu服务器下,MySQL默认是只允许本地登录,因此需要修改配置文件将地址绑定给注释掉:
[plain]
1. # Instead of skip-networking the default is now to listen only on
2. # localhost which is more compatible and is not less secure.
3. #bind-address = 127.0.0.1 #注释掉这一行就可以远程登录了
不然会报如下错误:
ERROR 2003 (HY000): Can't connect to MySQL server on 'host' (111)
查询数据
SELECT col1,col2,... FROM tbl_name [WHERE clause] [ORDER BY 'col_name' [DESC]] [LIMIT [m,]n];
Limit m,n 跳过m个,要n个
字段表示法:
*: 所有字段
as:字段别名,col1 AS alias1
MariaDB [testdb]> select * from people;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | 12 | 1 |
+----+------+------+
1 row in set (0.00 sec)
MariaDB [testdb]>
插入数据
INSERT [INTO] tbl_name [(col_name,...)] {VALUES | VALUE} (val1,...),(...),...
MariaDB [testdb]> select * from people;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | 12 | 1 |
+----+------+------+
1 row in set (0.00 sec)
MariaDB [testdb]> desc people;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(20) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [testdb]> select * from people;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | 12 | 1 |
+----+------+------+
1 row in set (0.00 sec)
MariaDB [testdb]> insert into people (id,name,sex)value(2,13,2);
Query OK, 1 row affected (0.01 sec)
MariaDB [testdb]> select * from people;
+----+------+------+
id | name | sex |
+----+------+------+
| 1 | 12 | 1 |
| 2 | 13 | 2 |
+----+------+------+
2 rows in set (0.00 sec)
MariaDB [testdb]>
修改数据
UPDATE tbl_name SET col1=val1, col2=val2, ... [WHERE clause] [ORDER BY 'col_name' [DESC]] [LIMIT [m,]n];
MariaDB [testdb]> select * from people;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | 12 | 1 |
| 2 | 13 | 2 |
+----+------+------+
2 rows in set (0.00 sec)
MariaDB [testdb]> update people set name=11 where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [testdb]> select * from people;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | 11 | 1 |
| 2 | 13 | 2 |
+----+------+------+
2 rows in set (0.00 sec)
MariaDB [testdb]>
删除数据
DELETE FROM tbl_name [WHERE clause] [ORDER BY 'col_name' [DESC]] [LIMIT [m,]n]; 可先排序再指定删除的行数
MariaDB [testdb]> select * from people;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | 11 | 1 |
| 2 | 13 | 2 |
+----+------+------+
2 rows in set (0.00 sec)
MariaDB [testdb]> delete from people where id>=2;
Query OK, 1 row affected (0.00 sec)
MariaDB [testdb]> select * from people;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | 11 | 1 |
+----+------+------+
1 row in set (0.00 sec)
MariaDB [testdb]>
清空表数据
TRUNCATE TABLE tbl_name;
MariaDB [testdb]> select * from people;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | 11 | 1 |
| 2 | 12 | 2 |
| 3 | 13 | 1 |
+----+------+------+
3 rows in set (0.00 sec)
MariaDB [testdb]> truncate table people;
Query OK, 0 rows affected (0.01 sec)
MariaDB [testdb]> select * from people;
Empty set (0.00 sec)
MariaDB [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| people |
+------------------+
1 row in set (0.00 sec)
MariaDB [testdb]> desc people;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(20) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [testdb]>
示例
DESC students;
INSERT INTO students VALUES(1,'tom','m'),(2,'alice','f');
INSERT INTO students(id,name) VALUES(3,'jack'),(4,'allen');
SELECT * FROM students WHERE id < 3;
SELECT * FROM students WHERE gender='m';
SELECT * FROM students WHERE gender IS NULL;
SELECT * FROM students WHERE gender IS NOT NULL;
SELECT * FROM students ORDER BY name DESC LIMIT 2;
SELECT * FROM students ORDER BY name DESC LIMIT 1,2;
SELECT * FROM students WHERE id >=2 and id <=4
SELECT * FROM students WHERE BETWEEN 2 AND 4
SELECT * FROM students WHERE name LIKE ‘t%’
SELECT * FROM students WHERE name RLIKE '.*[lo].*';
SELECT id stuid,name as stuname FROM students