Become the foundation of MySQL God~

  • [1] Display mysql information:

mysql> status;

  • [2] Exit mysql:

mysql> exit;

  • [3] View the database name:

mysql> show databases;

  • [4] View table name:

mysql> show tables;

  • [5] Create a database:

mysql> create database abc;

  • [6] Connect to the database:

mysql> use abc;
Database changed

  • [7] Create a table:

mysql> create table biao1 (id int primary key ,name char(8),age int)
->; Query OK, 0 rows affected (0.03 sec)
Explanation: query: query, suspect row: row affected: affected sec: Seconds, moments

  • [8] View table structure:

mysql> desc biao1; //Note: In MySQL, the default int type character length is 11

  • [9] Insert data:

mysql> insert into biao1 (id,name) values (1,‘z1’);
mysql> insert into biao1 (id,name) values (2,‘z2’);

Insert data in batches:

mysql> insert into biao1  values (3,'huai',13), 
    -> (4,'xin',18),
    -> (5,'ww',12);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
或
mysql> insert biao1 select 
    -> 8,'qq',18
    -> union select 9,'ww',18
    -> union select 10,'ee',19;
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
  • [10] View data:
mysql> select * from biao1;
mysql> select * from biao1 limit 3;                       //显示出biao1中前三行的数据
mysql> select * from biao1 order by id desc;             //以id为降序排序,显示出所有内容
mysql> select * from biao1 order by id desc limit 3;    //以id为降序排序,显示出前三行的内容
  • [11] Modify data:

mysql> update biao1 set name=‘zzz’ where id=2;

  • 【12】Delete data:

mysql> delete from biao1 where id=2;

  • [13] Delete table:

mysql> drop table biao1;

  • [14] Delete the database:

mysql> drop database abc;

  • [15], enter encrypted data [encrypt a field]

mysql> create table biao2 (name char(10),pass char(48)); //Note: The char(48) here corresponds to the encrypted character length and type! ! !                                                                                                                                           And the minimum character length after encryption is 41

mysql> insert into biao2 values ('zhang1',password('123'));

mysql> insert into biao3 values (haha,password(123));          //中文必须加''号
ERROR 1054 (42S22): Unknown column 'haha' in 'field list'

mysql> insert into biao2 values ('haha',password(123));       //数字加密可以不加引号!!!
Query OK, 1 row affected (0.00 sec)

mysql> insert biao3 values ('qq',123);            //into甚至都可以省略不懈
mysql> select * from biao2;
效果图;
+--------+-------------------------------------------+
| name   | pass                                      |
+--------+-------------------------------------------+
| zhang1 | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| haha   | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| qq     | 123                                       |
+--------+-------------------------------------------+
3 rows in set (0.00 sec)
  • [16], database authorization

Theory: mysql permission management

(1)mysql默认权限是允许root用户本地localhost登录和访问数据库。
(2)数据库用户名如果不存在,授权命令会直接创建此用户,并设置授权密码。

Authorization:

Syntax format: mysql> grant permission on library name. Table name to'database user name'@'allowed access host' identified by'authorized password';
grant: agree, allow identified: confirm, authenticate

For example: add a database user named xixi, and allow it to access from "local", have query permission for biao1 of the abc database, and the verification password is '123456'
mysql> grant select on abc.biao1 to'xixi'@ 'localhost' identified by '123456';
Note: The machine here cannot use 127.0.0.1 or the machine's real IP address (otherwise it will not take effect)

For example:

[root@localhost ~]# mysql -u root -p123.com
mysql> grant select on t325.biao1 to 'xixi'@'localhost' identified by '123.com';
Query OK, 0 rows affected (0.08 sec)
mysql> exit
Bye
[root@localhost ~]# mysql -u xixi -p123.com
mysql> use t325;
mysql> select * from biao1;
mysql> delete from biao1 where id=3;
ERROR 1142 (42000): DELETE command denied to user 'xixi'@'localhost' for table 'biao1'

Special example:
Create a database user named zhangsan, and allow any host in the 192.168.10.0 network segment to log in to the database remotely through this, and have any permissions to [all libraries and tables]!

grant all on * . * to ‘zhang3’@‘192.168.10.%’ identified by ‘123.com’;

Client remote login database: the
client installs the mysql client software. mariadb {MariaDB is developed as open source software. As a relational database, it provides an SQL interface for accessing data. }

Method 1: Install the open source mysql rpm -ivh /media/Packages/mariadb-5.5.52-1.el7.x86_64.rpm

Method 2: yum -y install mariadb

Client access:

mysql -u user -p -h 192.168.10.1 -p authorization password


View authorization:

View the authorization of the current user:
show grants;

View the authorization of the specified database user:
mysql> show grants for'username'@'allowed host'
mysql> show grants for'xixi'@'localhost';

View database user information:
select * from mysql.user;

mysql> use mysql //Switch to the system library
mysql> select * from user\G //Query table permissions, and output in the format to view user authorization


Revocation of authorization:

mysql> revoke permission on library name. Table name from'database user name'@'allowed host'
revoke create on . from'zhang3'@'192.168.10.%';

For example:

mysql> revoke select on t325.biao1 from 'xixi'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@localhost ~]# mysql -u xixi -p123.com
mysql> use t325
ERROR 1044 (42000): Access denied for user 'xixi'@'localhost' to database 't325'
权限列表:

SELECT,	查询
INSERT, 添加数据
UPDATE, 修改数据
DELETE, 删除数据
CREATE, 创建表
DROP,   删除表
all 所有权限
RELOAD, 重新加载(服务配置)
SHUTDOWN, 关闭服务
PROCESS,   进程管理
FILE,     文件管理
REFERENCES,  参照?
INDEX, 	  索引
ALTER,    修改库或表结构
SHOW DATABASES,   查看库
SUPER, 
CREATE TEMPORARY TABLES,  创建临时表
LOCK TABLES,    锁定表
EXECUTE,        执行存储过程,调用存储过程
REPLICATION SLAVE,   允许从服务器复制
REPLICATION CLIENT,  允许客户端复制
CREATE VIEW,    创建视图
SHOW VIEW,      查看视图名
CREATE ROUTINE,   
ALTER ROUTINE, 
CREATE USER,    创建用户
EVENT,          事件处理,事务处理
TRIGGER, 	触发器
CREATE TABLESPACE  创建表空间(库管理结构)
注意:当赋予多个权限时,用,号隔开权限
  • [17], add fields

alter table biao1 add column age int;

  • [18], modify the field name

(1) alter table biao1 change age ages int;

(2) mysql> alter table biao1 change ages age char(100);     //Use to modify the field name can also modify the data type

  • [19], add multiple fields

alter table biao1 add (sex char(4),jg char(10))

  • [20] Move field position

alter table table name change field name new field name field type constraint after field name (after which field to jump to)

Example:
mysql> alter table biao3 change sex xb char(8) default'nan' after pass;         after: after

mysql> desc biao3;
+-------+-----------+------+-----+---------+-------+
| Field | Type      | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-------+
| name  | char(10)  | YES  |     | NULL    |       |
| pass  | char(48)  | YES  |     | NULL    |       |
| xb    | char(8)   | YES  |     | nan     |       |
| age   | char(100) | YES  |     | NULL    |       |
| gz    | int(11)   | YES  |     | NULL    |       |
+-------+-----------+------+-----+---------+-------+
5 rows in set (0.15 sec)

Move your cute little hand to like it!Insert picture description here

Guess you like

Origin blog.csdn.net/qq_50573146/article/details/109895689