天天看點

MySQL指令操作

作業系統:centos 7.9

資料庫系統:mysql 5.7.23

遠端mysql伺服器:172.16.100.60

mysql用戶端:172.16.100.61

實戰任務:本地連伺服器端

1.連接配接mysql

連接配接mysql指令的格式:

mysql -h主機位址 -u使用者名  -p密碼

[root@linux root]# mysql -h 172.16.100.61 -uroot -p

[11:17:01]enter password: 

[11:17:01]error 1130 (hy000): host '172.16.100.60' is not allowed to connect to this mysql server

遠端資料庫伺服器授權該終端ip(172.16.100.61)能遠端登入資料庫

grant all on 資料庫.* to 使用者名@登入主機 identified by "密碼"

#root使用者授予通路所有資料庫的所有權限,并且僅允許172.16.100.61用戶端登入通路

mysql> grant all on  *.* to [email protected] identified by "********";

query ok, 0 rows affected, 1 warning (0.00 sec)

#db使用者授予通路所有資料庫的查詢、插入、修改、删除權限,并且允許任何用戶端登入通路

mysql> grant select,insert,update,delete on *.* to db@"%" identified by "db123";

#dba使用者,隻可以在localhost上登入,并可對資料庫查詢、插入、修改、删除的操作,但任何用戶端都無法登入通路,

mysql> grant select,insert,update,delete on livey.*   to dba@localhost  identified by "***";  

#下面的終端是無法遠端通路172.16.100.60,因為dba使用者隻有localhost才能登入。

[root@localhost ~]# mysql -h172.16.100.60 -udba   -p

enter password: 

error 1045 (28000): access denied for user 'dba'@'172.16.100.61' (using password: yes)

用戶端遠端連接配接mysql資料庫伺服器

[root@localhost etc]# mysql -h172.16.100.60 -uroot -p

mysql> show databases;

+--------------------+

| database           |

| information_schema |

| cmsdb              |

| mysql              |

| performance_schema |

| sys                |

5 rows in set (0.01 sec)

2.資料庫的基本操作:

1)建立資料庫

mysql> create database livey;

2)顯示資料庫

| livey              |

6 rows in set (0.00 sec)

3)删除資料庫

mysql>drop database livey; 

4)切換資料庫

mysql> use livey;

database changed

5)顯示目前資料庫

mysql> select database();

+------------+

| database() |

| livey      |

6)建立資料表

mysql> create table mytables(

    -> id int(4) not null primary key auto_increment,

    -> name char(20) not null,

    -> sex int(4) not null default '0',

    -> degree double(16,2));

query ok, 0 rows affected (0.06 sec)

7)删除表

mysql> drop table mytables;

mysql> insert into mytables values(1,'tony','1',98.65),(2,'jasm','0',88.9);    

query ok, 2 rows affected (0.02 sec)

records: 2  duplicates: 0  warnings: 0

8)查詢表

mysql> select * from mytables;

+----+------+-----+--------+

| id | name | sex | degree |

|  1 | tony |   1 |  98.65 |

|  2 | jasm |   0 |  88.90 |

2 rows in set (0.01 sec)

9)檢視表中第一行的資料

mysql> select * from mytables order by id limit 0,1;

1 row in set (0.00 sec)

10)增加字段

mysql> alter table mytables  add apptest int(4) default '0';

11)修改表中的資料

mysql> update mytables set name='sky9890' where id=1;    

query ok, 1 row affected (0.02 sec)

rows matched: 1  changed: 1  warnings: 0

12)删除表中的資料

mysql> delete from mytables where id=1;

13)修改表名

mysql> rename table mytables to yourtables;

3.備份遠端資料庫

1)備份資料庫

格式:

mysqldump -h  主機位址  -u 使用者名 -p  資料庫名 > 導出的檔案名

檔案預設是存放目前用戶端的目錄下。

[root@localhost ~]# mysqldump -h172.16.100.60 -uroot -p livey>livey.sql

2)導出一個表

[root@localhost ~]# mysqldump -h172.16.100.60 -uroot -p livey yourtables> yourtables.sql

enter password:

3)導出一個資料庫結構

[root@localhost ~]# mysqldump -h172.16.100.60 -u root -p --no-data --databases  livey> liveydb.sql

繼續閱讀