憨豆说安全 · 2020年08月10日

云芯一号教程 - MySQL安装指导及基础使用

  MySQL是最流行的关系型数据库管理系统之一,也是服务器系统的必备组件之一。
  下面介绍MySQL数据的安装和基础使用方法。

1.安装MySQL
  首先,查看一下安装源上的MySQL版本。

  jishu@Jishu:~$ sudo apt-cache search mysql
  ......
  mysql-client - MySQL database client (metapackage depending on the latest version)
  mysql-client-5.7 - MySQL database client binaries
  mysql-client-core-5.7 - MySQL database core client binaries
  mysql-common - MySQL database common files, e.g. /etc/mysql/my.cnf
  mysql-server - MySQL database server (metapackage depending on the latest version)
  mysql-server-5.7 - MySQL database server binaries and system database setup
  mysql-server-core-5.7 - MySQL database server binaries
  ......

  直接安装MySQL 5.7版本。

  sudo apt-get install mysql-server-5.7

  安装完成后,确认一下MySQL数据库是否正常。

  jishu@Jishu:~$ sudo mysql
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 2
  Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)
  
  Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  
  Oracle is a registered trademark of Oracle Corporation and/or its
  affiliates. Other names may be trademarks of their respective
  owners.
   
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  
  mysql>  

  出现上面的提示表示已经连接到MySQL数据库,数据库工作正常。

  MySQL服务的启动关闭命令如下:

  service mysql start
  service mysql stop
  service mysql restart

2.MySQL数据库基本操作
  2.1 连接数据库

  mysql -h host -u 用户名 -p 输入密码
  -h参数  默认为localhost

  2.2 用户操作
  2.2.1 新建用户

  create user 'test'@'localhost' identified by '1234';

  上述语句创建一个只能在本地登录的test用户,无法远程登录。创建远程登录用户的语句为:

  create user 'test'@'%' identified by '1234';

  2.2.2 查询用户

  select user,host from mysql.user;

  如果想看到用户的所有信息,也可以用:

  select * from mysql.user;

  2.2.3 删除用户

  drop user test@'localhost';
  或
  drop user test@'%';

  2.2.4 修改用户密码

  update mysql.user set authentication_string=password('123456') where user='test';
  flush privileges;

  在MySQL 5.7版本中,user表中用户密码字段已经改为authentication_string,不是password。
  用户密码修改之后,一定要刷新权限,否则修改不起效。

  2.2.4 用户分配权限

  grant all privileges on testdb.* to test@'%' identified by '123456';
  
  grant create,alter,drop,select,insert,update,delete on testdb.* to test@'%' identified by '123456';
  flush privileges; 
  

  2.3 数据库操作
  2.3.1 创建数据库
  简单的创建命令:

  create database testdb;

  约定默认字符类型,判断是否重名的创建命令:

  
  create database if not exists testdb default charset utf8 collate utf8_general_ci;

  2.3.2 选择数据库

  use testdb;

  2.3.3 删除数据库

  drop database testdb;

  2.4 表操作
  2.4.1 创建表
  表的创建是数据库中非常重要的功能,基本格式如下:

  create table 表名
  (
     列名 数据类型 [约束],
     ...
  );

  约束的类型主要有主键约束、外键约束、非空约束、唯一约束、默认约束

  1)主键约束:要求主键列的数据是唯一,且不允许为空。
  格式为:字段名 数据类型 primary key

  2)外键约束:用来在两个表之间建立连接。
  格式为:constraint 外键名 foreign key 字段名 references 主表名(主键列)

  3)非空约束:指字段值不能为空。
  格式为:字段名 数据类型 not null

  4)唯一约束:要求该列值唯一,允许为空,但只能出现一个空值。
  格式为:字段名 数据类型 unique

  5)默认约束:指定某列的默认值。
  格式为:字段名 数据类型 default 默认值

  6)设置表的属性值自增:一个表只能有一个字段使用自增约束,且该字段必须为主键的一部分。
  格式为:字段名 数据类型 auto_increment

  2.4.2 查看表结构

  describe 表名 / desc 表名

  2.4.3 修改表名

  alter table 旧表名 rename 新表名;

  2.4.4 修改表字段/结构

  alter table 表名 modify/add/change/drop ..

  2.4.5 删除表

  drop table 表名; 

  2.4.6 查询表数据

  select 字段, 字段  from  表名 where  条件

  2.4.7 插入数据

  insert  into  表名  (列字段列表)  values(值内容列表);

  2.4.8 更新数据

  update 表名 set 列名 = 值 , ....列名 = 值  where 条件;

  2.4.9 删除数据

  delete from 表名 where 条件; 
推荐阅读
关注数
4271
内容数
71
低成本Arm微服务器开发平台“云芯1号”教程及应用,欢迎关注
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息