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

Enter password: 

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)显示数据库

mysql> show databases;

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

| Database           |

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

| information_schema |

| cmsdb              |

| livey              |

| mysql              |

| performance_schema |

| sys                |

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

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;

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

| id | name | sex | degree |

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

|  1 | tony |   1 |  98.65 |

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

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

Enter password: 

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

猜你喜欢

转载自blog.51cto.com/sky9896/2623061