MySQL数据库基础操作

 

创建一个字符集为utf8的数据库

mysql> create database lisi DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

 

查看创建的数据库

mysql> show create database lisi;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| lisi | CREATE DATABASE `lisi` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)

删除数据库lisi

mysql> drop database lisi;
Query OK, 0 rows affected (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> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)

创建用户

mysql> create user 'zhangsan'@'host' identified by 'zhangsan';
Query OK, 0 rows affected (0.01 sec)

也可以用如下方法创建用户
mysql> grant all on lisi.* to 'lisi'@'localhost' identified by 'root@123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

删除用户

mysql> drop user 'zhangsan'@'host';
Query OK, 0 rows affected (0.00 sec)

mysql> drop user 'lisi'@'localhost';
Query OK, 0 rows affected (0.00 sec)

 

mysql> select user,host from mysql.user\G
*************************** 1. row ***************************
user: mysql.session
host: localhost
*************************** 2. row ***************************
user: mysql.sys
host: localhost
*************************** 3. row ***************************
user: root
host: localhost
3 rows in set (0.00 sec)

授权只允许testuser用户在IP为10.0.0.100的主机上远程连接MySQL数据库,并且testuser用户对所有数据库中的所有库和表都拥有全部权限

mysql> grant all on *.* to 'testuser'@'10.0.0.100' identified by 'TestUser';
Query OK, 0 rows affected, 1 warning (0.00 sec)

授权只允许testuser用户在IP地址段为172.16.100.x这些主机上远程连接MySQL数据库,并且testuser用户对所有数据库中的所有库和表都拥有全部权限

mysql>
mysql> grant all on *.* to 'testuser'@'172.16.100.%' identified by 'TestUser';
Query OK, 0 rows affected, 1 warning (0.01 sec)

 授权允许testuser用户在IP地址段为172.16.100.x这些主机上远程连接MySQL数据库,且testuser对mysql数据库中的zhangsan数据库的所有表都有查询,更新,插入,删除权限

mysql> grant select,update,insert,delete on zhangsan.* to 'testuser'@'172.16.100.%' identified by 'TestUser';
Query OK, 0 rows affected, 1 warning (0.00 sec)

 

posted @ 2022-06-12 21:16  北极之光的博客  阅读(21)  评论(0编辑  收藏  举报