一、介绍
1.1 技术介绍
MySQL是一个开源的轻量级的数据库,具有体积小,速度快,跨平台等特点,可以和开发语言结合,适合学习及小型项目的数据存储。
1.2 官网地址
下载地址(windows版):https://downloads.mysql.com/archives/community/
下载地址(linux版):https://downloads.mysql.com/archives/community/
二、环境搭建
2.1 安装
2.1.1 下载安装包并解压到指定文件夹
以5.7.29版本,64位解压版为例,在下拉框中选中需要的版本,点击Download进行下载
解压在想要存放的文件夹,本例存放的文件夹
D:\work\software\mysql-5.7.29-winx64
2.1.2 配置环境变量
以Windows10为例,根据系统具体步骤可能不同,具体位置为我的电脑->属性->高级系统设置->高级->环境变量,在环境变量的Path里添加数据库文件夹里的bin目录
2.1.3 配置my.ini
在数据库根目录新建一个my.ini
文件,以下可供参考
-
basedir
和datadir
一定要添加自己的数据库路径,不能直接粘贴这段my.ini
的内容 - 安装目录
\
需要两个\\
进行转义,如果不使用安装时候会找不到文件夹
[mysql]
# 设置 mysql 客户端默认字符集
default-character-set=utf8mb4
[mysqld]
#设置 3306 端口,不配置默认3306
port = 3306
# 设置 mysql 的安装目录
basedir=D:\\work\\software\\mysql-5.7.29-winx64
# 设置 mysql 数据库的数据的存放目录
datadir=D:\\work\\software\\mysql-5.7.29-winx64\\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为 8 比特编码的 latin1 字符集
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
在bin目录下输入mysql --initialize --console
开始安装
PS D:\work\software\mysql-5.7.29-winx64\bin> mysqld --initialize --console
2020-07-10T07:34:05.592633Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-10T07:34:05.805421Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-07-10T07:34:05.845299Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-07-10T07:34:05.922923Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: bc03d044-c27f-11ea-ac53-00155dd2bbb6.
2020-07-10T07:34:05.933676Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-07-10T07:34:06.899806Z 0 [Warning] CA certificate ca.pem is self signed.
2020-07-10T07:34:07.784070Z 1 [Note] A temporary password is generated for root@localhost: vqqNu&7BahFy
vqqNu&7BahFy
此段为默认密码,需要记录,接着使用mysqld install
命令进行安装
PS D:\work\software\mysql-5.7.29-winx64\bin> mysqld install
Service successfully installed.
PS D:\work\software\mysql-5.7.29-winx64\bin>
安装成功
2.1.4启动mysql
使用管理员权限在命令行模式输入net start mysql
启动mysql
PS D:\work\software\mysql-5.7.29-winx64> net start mysql
The MySQL service is starting.
The MySQL service was started successfully.
启动成功
2.1.5 本地命令行链接MySQL
mysql -u root -p
2.1.6 使用工具连接MySQL
以Navicat为例,输入账号密码后可进行测试
输入之前记录的密码,登录mysql
2.1.6更改root账号密码
mysql>use mysql;
mysql>update user set authentication_string=password('your password') where user='root';
mysql>flush privileges;
your password
更改为新密码
2.1.7 设置远程连接
MySQL默认只能进行本地连接,通过以下命令,设置允许远程登录
GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password' WITH GRANT OPTION;
使root账号远程登陆只需要把your username 换成root ,your passoword换成你的root密码
三、Demo开发演示
写作说明:demo简要概述,通过demo能达到什么程度等。
3.1 Demo概述
整个项目的架构
pom文件导入数据库连接驱动
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.toj</groupId>
<artifactId>mysqldemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
</dependencies>
</project>
数据库内的数据
对应的实体类
package cn.toj.mysqldemo.domain;
/**
* @author Carlos
* @description 用户实体类
* @Date 2020/7/10
*/
public class User {
private Integer id;
private String username;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String adderess) {
this.address = adderess;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
", adderess='" + address + '\'' +
'}';
}
}
用户查询接口
package cn.toj.mysqldemo.dao;
import cn.toj.mysqldemo.domain.User;
import java.util.List;
public interface IUserDao {
List<User> findAll();
}
用户查询实现类
package cn.toj.mysqldemo.dao.impl;
import cn.toj.mysqldemo.dao.IUserDao;
import cn.toj.mysqldemo.domain.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
/**
* @author Carlos
* @description
* @Date 2020/7/10
*/
public class UserDaoImpl implements IUserDao {
public List<User> findAll() {
List<User> users = new LinkedList<User>();
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功");
String url = "jdbc:mysql://localhost:3306/user?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("数据库连结成功");
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM user";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
User userResult = new User();
userResult.setId(rs.getInt("id"));
userResult.setSex(rs.getString("sex"));
userResult.setAddress(rs.getString("address"));
userResult.setUsername(rs.getString("username"));
users.add(userResult);
}
rs.close();
stmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
return users;
}
}
测试类
package cn.toj.mysqldemo;
import cn.toj.mysqldemo.dao.IUserDao;
import cn.toj.mysqldemo.dao.impl.UserDaoImpl;
import cn.toj.mysqldemo.domain.User;
import java.util.List;
/**
* @author Carlos
* @description 测试类
* @Date 2020/7/10
*/
public class MyqlTest {
public static void main(String[] args) {
IUserDao test = new UserDaoImpl();
List<User> users = test.findAll();
for (User user : users) {
System.out.println(user);
}
}
}
运行测试类获得结果
数据库驱动加载成功
数据库连结成功
User{id=1, username='李明', sex='男', adderess='北京'}
User{id=2, username='王刚', sex='男', adderess='上海'}
User{id=3, username='张磊', sex='男', adderess='广州'}
Process finished with exit code 0
3.2 Demo下载地址
- GitHub项目地址:
https://github.com/diyzhang/42j102-mysqldemo - 使用Git下载项目的命令
git clone https://github.com/diyzhang/42j102-mysqldemo.git