一、下载安装
1、安装前环境准备
(1)检查是否已经安装过mysql,执行命令
rpm -qa | grep mysql
-qa q代表query,a代表all
如果已存在,则执行卸载命令 后边为Mysql目录;
rpm -e --nodeps mysql-xxxx
(2)查询所有Mysql对应的文件夹;whereis 将和xxx文件相关的文件都查找出来
find / -name mysql
删除相关目录或文件
rm -rf xxx/mysql
验证是否删除完毕
find / -name mysql
(3)检查mysql用户组和用户是否存在,如果没有,则创建
cat /etc/group | grep mysql
cat /etc/passwd |grep mysql
groupadd mysql
useradd -r -g mysql mysql
2、下载安装mysql
先去官网选择Yum 资源包,地址为:https://dev.mysql.com/downloads/repo/yum/,根据linux版本下载rpm包,如centos6下载mysql80-community-release-el6-4.noarch.rpm
wget http://repo.mysql.com/mysql80-community-release-el6-4.noarch.rpm
rpm -ivh mysql80-community-release-el6-4.noarch.rpm
yum clean all
yum makecache
yum install mysql-community-server
验证mysql是否成功安装:mysqladmin --version
其中一些命令解释:
wget 文件下载命令;
rpm -ivh 安装 -Uvh 升级 -e 卸载 -q 查询是否安装安装(不带.rpm) -qi 查询安装情况;
yum clean all 清除缓存;yum makecache 是将软件包信息缓存,以提高速度;
yum repolist all | grep mysql 显示所有仓库,这里可以通过enable/disable 切换mysql版本 ,如使用mysql80而不使用mysql57
yum-config-manager --disable mysql80-community ;yum-config-manager --enable mysql57-community
验证mysql是否安装:mysqladmin --version
查看安装路径 whereis mysql
二、启动mysql
/etc/init.d/mysqld start
启动时候mysql会输出初始密码,查看log文件(cat /var/log/mysqld.log),能看到
A temporary password is generated for root@localhost: 9/ivBD=toQI9
9/ivBD=toQI9就是你的初始密码
后续启动:service mysqld start
**登录: mysql -u root -p
再粘贴上上面的密码
修改密码:
mysql>ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘Ppp11111111!’;
查看是否能正常使用:
mysql>showdatabases;
mysql>use mysql;
mysql>exit 退出
三、整合springBoot/mybatis
1.创建表结构,并插入测试数据
CREATE TABLE `t00_user` (
`user_id` varchar(64) DEFAULT NULL,
`user_name` varchar(512) DEFAULT NULL
) ;
insert into t00_user values ('3322','马宝国');
insert into t00_user values ('5566','蔡徐坤');
insert into t00_user values ('8899','面筋哥');
2.修改配置文件
(1).在pom.xml文件中添加
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.0version>
dependency>
(2)在application.yml文件中添加
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
#不加allowPublicKeyRetrieval=true会报SQLNonTransientConnectionException
url: jdbc:mysql://192.168.124.128:3306/mysql?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: Ppp11111111!
mybatis:
#扫描mapper文件,Mapper文件一般放到resource下,如果放在java目录下,需要在pom文件的bulid标签下将.xml放到include下
mapper-locations: classpath:mapping/*Mapper.xml
3.项目代码编写
(1)按照图示创建文件夹及文件,项目结构目录如下:
(2)代码编写
①UserController.java
package com.example.springb_web.controller;
import com.example.springb_web.dao.User;
import com.example.springb_web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="/getUser")
@ResponseBody
public List<User> test(){
List<User> user = userService.getUser();
return user;
}
}
②User.java
package com.example.springb_web.dao;
public class User {
private String user_id;
private String user_name;
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
}
③UserDAO.java
package com.example.springb_web.mapper;
import com.example.springb_web.dao.User;
import java.util.List;
public interface UserDAO {
List<User> getUser();
}
④UserService.java、UserServiceImpl.java
package com.example.springb_web.service;
import com.example.springb_web.dao.User;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
public List<User> getUser();
}
package com.example.springb_web.service.impl;
import com.example.springb_web.dao.User;
import com.example.springb_web.mapper.UserDAO;
import com.example.springb_web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
@Override
public List<User> getUser() {
return userDAO.getUser();
}
}
⑤UserMapper.xml,注意namespace和resultType路径不能错
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.springb_web.mapper.UserDAO">
<select id="getUser" resultType="com.example.springb_web.dao.User">
SELECT * FROM t00_user
select>
mapper>
⑥SpringbWebApplication.java,这里主要是加上注解MapperScan
package com.example.springb_web;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.springb_web.mapper")//使用MapperScan批量扫描所有的Mapper接口;
public class SpringbWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbWebApplication.class, args);
}
}
(3)结果验证:右击SpringbWebApplication文件,点击Debug 'SpringbWebApplication’启动。
输入网址http://localhost:8080/getUser
看到结果如下:
(4)权限问题:host is not allowed to connect…
mysql>update user set host = ‘%’ where user = ‘root’;
mysql>flush privileges;