suninf 's blog

Enjoy From Programming And Technique

CentOS7 安装Mysql以及与Spring boot对接

Catalog

本文整理了下CentOS7下安装mysql以及与Spring boot对接的基本配置。

配置mysql

安装

wget https://dev.mysql.com/get/Downloads/MySQL-4.7/mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar

tar -xvf mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar

rpm -ivh mysql-community-common-5.7.22-1.el7.x86_64.rpm \
  mysql-community-libs-5.7.22-1.el7.x86_64.rpm  \
  mysql-community-client-5.7.22-1.el7.x86_64.rpm \
  mysql-community-server-5.7.22-1.el7.x86_64.rpm \
  mysql-community-devel-5.7.22-1.el7.x86_64.rpm

注意:

  1. 如果已经安装mariadb,需要删除 yum -y remove mariadb mariadb-libs mariadb-server
  2. 需要安装perl yum -y install perl perl-JSON

使用mysql

启动与结束命令

service mysqld start
service mysqld stop

# 使用密码登录mysql
mysql -u root -p

找到并修改初始密码

参考:

  1. 通过cat /var/log/mysqld.log | grep password找到临时密码
  2. mysql -u root -p输入临时密码,连接到mysql
  3. 修改初始密码 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MysqlPasswordXXX');

如果找不到临时的初始密码,可以新建密码

参考:https://www.jianshu.com/p/707190d13e5c

  1. 修改/etc/my.cnf文件,在[mysqld]下添加 skip-grant-tables, 再重新启动mysql
  2. 然后用空密码方式使用root用户登录MySQL: mysql -u root
  3. 修改root用户的密码
    mysql> update mysql.user set password=PASSWORD('new_password') where User='root';
    mysql> flush privileges;
    mysql> quit

Spring boot与Mybatis结合使用

pom.xml引入依赖

  • 引入连接mysql的必要依赖 mysql-connector-java
  • 引入整合MyBatis的核心依赖 mybatis-spring-boot-starter
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.21</version>
</dependency>

搜索mapper DAO的指定 @MapperScan

@SpringBootApplication
@MapperScan("net.suninf.dao")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

application.yml关于mybatis的配置

mybatis:
    mapper-locations: classpath:mapper/*.xml
    type-aliases-package: net.suninf.dao
    configuration:
        map-underscore-to-camel-case: true
        default-fetch-size: 100

根据mapper配置,需要在src/main/resource/mapper目录下添加数据库操作的xml文件

如果遇到 mysql Table 'performance_schema.session_variables' doesn't exist 问题

参考:https://blog.csdn.net/whereismatrix/article/details/54018732

  1. 输入:mysql_upgrade -u root -p
  2. 重启mysql服务:service mysqld restart

mybatis配置参考

mysql常用操作

参考mysql教程: http://www.runoob.com/mysql/mysql-create-tables.html

show databases;
use db_name;
show tables;
exit

其他参考

Comments