MySQL 8 安装与使用

下载

https://dev.mysql.com/downloads/mysql/ 压缩版(免安装)
https://dev.mysql.com/downloads/installer/ 安装版 MySQL 8 安装与使用_第1张图片

解压

解压到任意目录 如 D:\software\mysql\mysql-8.0.15 并在目录下新建data文件夹MySQL 8 安装与使用_第2张图片

初始化和启动

打开bin 目录 在地址栏输入cmd回车进入命令行
初始化mysql,初始化时会在命令行生成初始随机密码,此处为ro*cdj(iZ9t5
mysqld --initialize --console
–console表示输入日志
初始化完成后会在data目录生成一堆文件
然后输入mysqld启动服务(或者mysqld --console)

Microsoft Windows [版本 10.0.15063]
(c) 2017 Microsoft Corporation。保留所有权利。

D:\software\mysql\mysql-8.0.15\bin>mysqld --initialize --console
2019-06-28T06:05:34.609135Z 0 [System] [MY-013169] [Server] D:\software\mysql\mysql-8.0.15\bin\mysqld.exe (mysqld 8.0.15) initializing of server in progress as process 5668
2019-06-28T06:06:27.426336Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ro*cdj(iZ9t52019-06-28T06:06:50.996143Z 0 [System] [MY-013170] [Server] D:\software\mysql\mysql-8.0.15\bin\mysqld.exe (mysqld 8.0.15) initializing of server has completed

D:\software\mysql\mysql-8.0.15\bin>mysqld

MySQL 8 安装与使用_第3张图片MySQL 8 安装与使用_第4张图片
新开一个命令行窗口

Microsoft Windows [版本 10.0.15063]
(c) 2017 Microsoft Corporation。保留所有权利。

D:\software\mysql\mysql-8.0.15\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.15

Copyright (c) 2000, 2019, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by 'password';
Query OK, 0 rows affected (0.17 sec)

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

mysql>

MySQL 8 安装与使用_第5张图片
可以看到,此处会让你修改初始密码,我们修改密码为password,然后就可以正常使用了
如果要关闭服务,只需要关闭前面打开的mysqld命令行窗口就行了

如果你觉得不方便可以添加为系统服务,后续就可以像系统服务一样管理了

不过此命令需要以管理员身份运行cmd窗口执行
MySQL 8 安装与使用_第6张图片

# 需要在mysql bin目录下执行
mysqld --install

# 开启
net start mysql
# 关闭
net stop mysql

基本操作

 create user 'luodeng'@'%' identified by '123456';
 create database spring;
 grant all privileges on spring.* to 'luodeng'@'%' with grant option;
 flush privileges;
 use spring;
create table study
(
    id   bigint auto_increment comment 'id' primary key,
    create_time      datetime   null comment '创建时间',
    name     varchar(16)    null comment '名称'
);
 insert into study (name,create_time)values ('test',now());
 select * from study;
Microsoft Windows [版本 10.0.15063]
(c) 2017 Microsoft Corporation。保留所有权利。

D:\software\mysql\mysql-8.0.15\bin>mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 84
Server version: 8.0.15 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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> create user 'luodeng'@'%' identified by '123456';
Query OK, 0 rows affected (0.15 sec)

mysql> create database spring;
Query OK, 1 row affected (0.13 sec)

mysql> grant all privileges on spring.* to 'luodeng'@'%' with grant option;
Query OK, 0 rows affected (0.07 sec)

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

mysql> use spring;
Database changed
mysql> create table study
    -> (
    ->     id   bigint auto_increment comment 'id' primary key,
    ->     create_time      datetime   null comment '创建时间',
    ->     name     varchar(16)    null comment '名称'
    ->
    -> );
Query OK, 0 rows affected (1.60 sec)

mysql> insert into study (name,create_time)values ('test',now());
Query OK, 1 row affected (0.20 sec)

mysql> select * from study;
+----+---------------------+------+
| id | create_time         | name |
+----+---------------------+------+
|  1 | 2019-06-28 17:04:48 | test |
+----+---------------------+------+
1 row in set (0.00 sec)

mysql>

MySQL 8 安装与使用_第7张图片

JDBC连接

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MySQLTest {
	public static void main(String []args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/spring?serverTimezone=UTC","luodeng","123456");
			ps = conn.prepareStatement("select id,name,create_time from study;");
			rs = ps.executeQuery();
			while(rs.next()) {
				int num = rs.getInt("id");
				String name = rs.getString("name");
				System.out.print(num+"\t"+name);
			}
		} catch (Exception e) {
		  e.printStackTrace();
		}
	}
}

注意MySQL版本,要使用8开头的

        
        
            mysql
            mysql-connector-java
            8.0.16
        

MySQL 8 安装与使用_第8张图片

想了解更详细安装说明可以阅读官方文档

https://dev.mysql.com/doc/refman/8.0/en/windows-install-archive.html

https://dev.mysql.com/doc/mysql-installation-excerpt/8.0/en/windows-install-archive.html

你可能感兴趣的:(数据库)