Mysql5.7安装及操作

一、mysql5.7采用二进制包安装(CentOS7.3)

官方安装文档:

http://dev.mysql.com/doc/refman/5.7/en/linux-installation.html

二进制 rpm: 

Yum Repository      mysql57-community-release-el7-9.noarch.rpm

CentOS7.3机器环境部署:

[root@mysql1 ~]# rpm -q mariadb-server
package mariadb-server is not installed
[root@mysql1 ~]# sed -ri '/^SELINUX=/c\SELINUX=disabled' /etc/selinux/config
[root@mysql1 ~]# setenforce 0

方法:二进制 rpm

http://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html
[root@mysql1 ~]# md5sum mysql57-community-release-el7-9.noarch.rpm
[root@mysql1 ~]# yum -y install mysql57-community-release-el7-9.noarch.rpm
[root@mysql1 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.neusoft.edu.cn
 * extras: mirrors.neusoft.edu.cn
 * updates: mirrors.neusoft.edu.cn
repo id                                 repo name                         status
base/7/x86_64                           CentOS-7 - Base                   9,591
extras/7/x86_64                         CentOS-7 - Extras                   392
mysql-connectors-community/x86_64       MySQL Connectors Community           45
mysql-tools-community/x86_64            MySQL Tools Community                59
mysql57-community/x86_64                MySQL 5.7 Community Server          247
updates/7/x86_64                        CentOS-7 - Updates                1,962
repolist: 12,296

过滤数据仓库

[root@mysql1 ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community    disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community -  disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community    disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community -  disabled
mysql-connectors-community/x86_64  MySQL Connectors Community     enabled:    45
mysql-connectors-community-source  MySQL Connectors Community - S disabled
mysql-tools-community/x86_64       MySQL Tools Community          enabled:    59
mysql-tools-community-source       MySQL Tools Community - Source disabled
mysql-tools-preview/x86_64         MySQL Tools Preview            disabled
mysql-tools-preview-source         MySQL Tools Preview - Source   disabled
mysql55-community/x86_64           MySQL 5.5 Community Server     disabled
mysql55-community-source           MySQL 5.5 Community Server - S disabled
mysql56-community/x86_64           MySQL 5.6 Community Server     disabled
mysql56-community-source           MySQL 5.6 Community Server - S disabled
mysql57-community/x86_64           MySQL 5.7 Community Server     enabled:   247
mysql57-community-source           MySQL 5.7 Community Server - S disabled
mysql80-community/x86_64           MySQL 8.0 Community Server     disabled
mysql80-community-source           MySQL 8.0 Community Server - S disabled

查看已经开启数据仓库

[root@mysql1 ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64       MySQL Connectors Community           45
mysql-tools-community/x86_64            MySQL Tools Community                59
mysql57-community/x86_64                MySQL 5.7 Community Server          247

安装mysql数据库服务器及启动服务并设置为开机自启动

[root@mysql1 ~]# yum -y install mysql-community-server
[root@mysql1 ~]# systemctl start mysqld                                 //第一次启动先初始数据库
[root@mysql1 ~]# systemctl enable mysqld
[root@mysql1 ~]# ls /var/lib/mysql
auto.cnf         ib_buffer_pool  mysql               public_key.pem   test
ca-key.pem       ibdata1         mysql.sock          school
ca.pem           ib_logfile0     mysql.sock.lock     server-cert.pem
client-cert.pem  ib_logfile1     performance_schema  server-key.pem
client-key.pem   ibtmp1          private_key.pem     sys

查看mysql默认密码并使用默认密码登陆数据库

[root@mysql1 ~]# grep 'temporary password' /var/log/mysqld.log 
2018-03-08T01:07:46.185157Z 1 [Note] A temporary password is generated for root@localhost: 7WZU6OMbBS*(
[root@mysql1 ~]# mysql -uroot -p'7WZU6OMbBS*('
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21

Copyright (c) 2000, 2018, 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> set password=password('Zaq12wsx');
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set password=password('Qwe123??');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

使用新密码登陆数据库

[root@mysql ~]# mysql -uroot -pQwe123??
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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>

查看databases

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

二、数据库基本操作

创建数据库并进入数据库

mysql> create database test;
Query OK, 1 row affected (0.08 sec)

mysql> use test;
Database changed

创建表

mysql> create table test1( tinyint_test tinyint, int_test int );
Query OK, 0 rows affected (0.09 sec

查看当前数据库所包含的表

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test1          |
+----------------+
1 row in set (0.00 sec)

查看表结构

mysql> desc test1;
+--------------+------------+------+-----+---------+-------+
| Field        | Type       | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+-------+
| tinyint_test | tinyint(4) | YES  |     | NULL    |       |
| int_test     | int(11)    | YES  |     | NULL    |       |
+--------------+------------+------+-----+---------+-------+
2 rows in set (0.11 sec)

插入数据

mysql> insert into test1 values(111,111);
Query OK, 1 row affected (0.00 sec)

数据库语法

CREATE DATABASE 数据库名;
数据库命名规则:
区分大小写
唯一性
不能使用关键字如 create select
不能单独使用数字


查看数据库
SHOW DATABASES;


选择数据库
SELECT database();
USE 数据库名

示例(表基本操作)

1、student1

表school.student1
字段		    字段		    字段
id              name	    sex		        age
1               tom		    male	        23		记录
2               jack		    male	        21		记录
3               alice		female	        19		记录
语法:
create table 表名(
		字段名1  类型[(宽度) 约束条件],
		字段名2  类型[(宽度) 约束条件],
		字段名3  类型[(宽度) 约束条件]
)[存储引擎 字符集];
==在同一张表中,字段名是不能相同
==宽度和约束条件可选
==字段名和类型是必须的
mysql> CREATE  DATABASE school;				        //创建数据库school
mysql> use school;
mysql> create table student1(
    -> id int,
    -> name varchar(50),                 
    -> sex enum('m','f'),
    -> age int
    -> );
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;			                                    //查看表(当前所在库)
+------------------+
| Tables_in_school |
+------------------+
| student1         |
+------------------+
1 row in set (0.00 sec)
mysql> select * from student1;						        //查询表中所有字段的值
Empty set (0.00 sec)

mysql> select name,age from student1;		        //查询表中指定字段的值
Empty set (0.00 sec)

向表中插入内容
语法:
insert into 表名(字段1,字段2...)  values(字段值列表...);

查看表结构

mysql> desc student1;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| id    | int(11)       | YES  |     | NULL    |       |
| name  | varchar(100)  | YES  |     | NULL    |       |
| sex   | enum('m','f') | YES  |     | NULL    |       |
| age   | int(11)       | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

顺序插入

mysql> insert into student1 values
    -> (1,'abel','m',33),
    -> (2,'alice','f',20),
    -> (3,'jack','m',40);
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

只向指定的字段插入值

mysql> insert into student1(name,age) values ('zhuzhu',10), ('gougou',20);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

查询表内容

mysql> select * from student1;
+------+--------+------+------+
| id   | name   | sex  | age  |
+------+--------+------+------+
|    1 | abel   | m    |   33 |
|    2 | alice  | f    |   20 |
|    3 | jack   | m    |   40 |
| NULL | zhuzhu | NULL |   10 |
| NULL | gougou | NULL |   20 |
+------+--------+------+------+
5 rows in set (0.00 sec)

2、表school.student2

id               id                   int
姓名			  name			    varchar(50)
出生年份	  born_year	    year
生日			  birthday		    date
上课时间	  class_time	    time
注册时间	  reg_time		datetime

创建student2表

mysql> create table student2(
    -> id int,
    -> name varchar(50),
    -> born_year year,
    -> birthday date,
    -> class_time time,
    -> reg_time datetime
    -> );

查询student2表结构

mysql> desc student2;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | YES  |     | NULL    |       |
| name       | varchar(50) | YES  |     | NULL    |       |
| born_year  | year(4)     | YES  |     | NULL    |       |
| birthday   | date        | YES  |     | NULL    |       |
| class_time | time        | YES  |     | NULL    |       |
| reg_time   | datetime    | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
插入数据

mysql> insert into student2 values(1,'tom',now(),now(),now(),now());
Query OK, 1 row affected, 1 warning (0.01 sec)

查询数据

mysql> select * from student2;
+------+------+-----------+------------+------------+---------------------+
| id   | name | born_year | birthday   | class_time | reg_time            |
+------+------+-----------+------------+------------+---------------------+
|    1 | tom  |      2018 | 2018-03-08 | 14:24:25   | 2018-03-08 14:24:25 |
+------+------+-----------+------------+------------+---------------------+
1 row in set (0.00 sec)
mysql> insert into student2 values(2,'jack',1982,19821120,123000,20140415162545);
Query OK, 1 row affected (0.01 sec)

mysql> select * from student2;
+------+------+-----------+------------+------------+---------------------+
| id   | name | born_year | birthday   | class_time | reg_time            |
+------+------+-----------+------------+------------+---------------------+
|    1 | tom  |      2018 | 2018-03-08 | 14:24:25   | 2018-03-08 14:24:25 |
|    2 | jack |      1982 | 1982-11-20 | 12:30:00   | 2014-04-15 16:25:45 |
+------+------+-----------+------------+------------+---------------------+
2 rows in set (0.00 sec)

3、student3











猜你喜欢

转载自blog.csdn.net/abel_dwh/article/details/79480637