2、看当前所有数据库:show databases;
3、进入mysql数据库:use mysql;
4、查看mysql数据库中所有的表:show tables;
5、查看user表中的数据:5以下版本使用select Host, User,Password from user;5及5以上版本使用mysql> select Host,User,authentication_string from user;
6、修改user表中的Host:update user set Host='%' where User='root';
7、刷新:flush privileges;
8、向表中插入数据:insert into 表名(user_name,user_passwd) values('zhangsan',pasword('123'));
9、创建:create table 表名 (字符1 值,字符2 值,primary key(搜索主键));
10、查看数据库服务的基本信息:status;
启动Mysql服务:systemctl start nysqld
检测端口:3306
退出数据库:exit
mysql> exit
取消命令执行:ctrl+c
查看当前连接用户:select user();
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost | #当前用户为root用户
+----------------+
1 row in set (0.00 sec)
查看数据库服务的基本信息:status;
mysql>status;
--------------
mysql Ver 14.14 Distrib 5.7.24, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 2 #连接的ID号
Current database: #目前用的数据库,目前没有进入任何数据库内
Current user: root@localhost #连接的用户为root
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.24 MySQL Community Server (GPL) #用的版本为MYSQL5.7.24版本
Protocol version: 10
Connection: Localhost via UNIX socket #连接方式
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8 #字符集
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock #sock文件存放位置
Uptime: 40 min 37 sec #启动的时间
Threads: 1 Questions: 7 Slow queries: 0 Opens: 105 Flush tables: 1 Open tables: 98 Queries per second avg: 0.002
--------------
查看当前服务器有哪儿些数据库:show databases;
进入数据库:use 数据库名
mysql> use sys
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 #此时已在sys数据库中
查看sys数据库中的数据表:show tables;
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
|user|
+---------------------------+
31 rows in set (0.01 sec)
显示数据表的结构(字段(列)): describe 表名;describe也可以简写为desc
describe [数据库名.]表名:不在数据库内时
describe 表名:在数据库内时
mysql> describe user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null(值是否为空) | Key(主键) | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
char表示字符,no表示不
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.00 sec)
创建新的数据库(先库后表):create database 数据库名
mysql> create database liuxiang;
Query OK, 1 row affected (0.01 sec)
创建新的数据表:create table 表名(字段1名称 类型,字段2名称 类型,primary key(字段名称);
mysql> use liuxiang; #先进入数据库内才能创建数据表
mysql> create table liuxiangs(user_name char(16) not null, user_passwd char(48) default '', primary key(user_name));
Query OK, 0 rows affected (0.01 sec)
mysql> show tables; #查看数据表
+--------------------+
| Tables_in_liuxiang |
+--------------------+
| liuxiangs |
+--------------------+
1 row in set (0.01 sec)
mysql> desc liuxiangs; #查看数据表结构
+-------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| user_name | char(18) | NO | PRI | NULL | |
| user_passwd | char(48) | YES | | | |
+-------------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
删除数据表:drop table 数据库名.表名;
删除数据库:drop database 数据库名;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
向数据表中插入新的数据记录:insert into 表名(字段1,字段2) valuse(字段1的值,字段2的值); #同一个用户不能输入两次mima
mysql> insert into liuxiangs(user_name,user_passwd) values('zhangsan','123456'); #铭文密码
Query OK, 1 row affected (0.00 sec)
mysql> insert into liuxiangs(user_name,user_passwd) values('lisi',password('123456')); #加密密码
Query OK, 1 row affected, 1 warning (0.01 sec)
查看数据表中用户的信息:select * from 数据表名;
mysql> select *from liuxiangs;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | 123456 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
从数据表中查找符合条件的数据记录:select 字段名1,字段2名,from 表名 where 条件表达式;
mysql> select user_name,user_passwd from liuxiangs where user_name='zhangsan';
+-----------+-------------+
| user_name | user_passwd |
+-----------+-------------+
| zhangsan | 123456 |
+-----------+-------------+
1 row in set (0.01 sec)
修改、更新数据表中的数据记录:update 表名 set 字段名1=新值,字段名2=新值2 where 条件表达式; #update更新的意思
mysql> update liuxiangs set user_passwd=password('123456') where user_name='zhangsan';
mysql> select *from liuxiangs;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| zhangsan | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
删除数据表中指定的数据记录:delete from 表名 where user_name='zhangsan';
mysql> delete from liuxiangs where user_name='zhangsan'; #删除用户zhangsan1
Query OK, 1 row affected (0.00 sec)
mysql> select * from liuxiangs; #查看数据表中的信息
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)
[root@localhost ~]# mysql -uroot -p123456
root管理员下操作如下:
给指定用户授权(授权用户不存在时会自动创建):被授权的用户可以管理数据库
mysql>
mysql> create user 'lcx'@'%' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'lcx'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'teacher'@'localhost'; #查看teacher拥有什么权限
+-------------------------------------------------------+
| Grants for teacher@localhost |
+-------------------------------------------------------+
| GRANT USAGE ON *.* TO 'teacher'@'localhost' |
| GRANT SELECT ON `liuxiang`.* TO 'teacher'@'localhost' |
+-------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> select Host,User,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| Host | User | authentication_string |
+-----------+---------------+-------------------------------------------+
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | teacher | *A4B6157319038724E3560894F7F932C8886EBFCF |
+-----------+---------------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> show databases; #root管理员视角
+--------------------+
| Database |
+--------------------+
| information_schema |
| cloud |
| liuxiang |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)
[root@localhost ~]# mysql -uteacher -p1234
teacher普通用户:操作如下
mysql> show databases; #授权用户视角
+--------------------+
| Database |
+--------------------+
| information_schema |
| liuxiang |
+--------------------+
2 rows in set (0.00 sec)
在teacher用户模式下只能查看数据库内容不能进行修改,因为权限不够
撤掉用户权限 :REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@域名或者IP地址
mysql> revoke select on liuxiang.* from 'teacher'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'teacher'@'localhost';
+---------------------------------------------+
| Grants for teacher@localhost |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'teacher'@'localhost' |
+---------------------------------------------+
1 row in set (0.00 sec)
====================================================================================
在数据库中修改root密码:
mysql> use mysql; #进入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> update user set authentication_string=password('123456') where user='root'; #修改root密码为123456
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges; #刷新授权表
Query OK, 0 rows affected (0.00 sec)
mysql> exit #exit退出
此时root密码修改成功
==============================================================================================
跳过数据库密码进行密码重新设置
root@host51 ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables #添加此行,该行的意思是跳过授权
[root@host51 ~]# systemctl restart mysqld
[root@host51 ~]# /etc/init.d/mysqld restart
[root@host51 ~]# mysql
mysql> use mysql;
Database changed
mysql> update user set authentication_string=password('123456') where user='root';
mysql> flush privileges;
7,退出
mysql> quit
在更改密码之后需要恢复之前的设置
root@host51 ~]# vim /etc/my.cnf
[mysqld]
#skip-grant-tables
[root@host51 ~]# systemctl restart mysqld
[root@host51 ~]# mysql -uroot -p'新密码'
mysql>alter user root@"localhost" identified by "123456";
Query OK, 1 row affected, 1 warning (0.06 sec)
6,刷新权限表
mysql> flush privileges;
7,退出
mysql> quit
4、 重启服务:
执行:
killall mysqld (杀死mysql进程)
/etc/init.d/mysql start (启动mysql服务