Remember springboot access to Linux MySQL database

Source: Internet
Author: User
Tags access database

today, using springboot to connect to Linux mysql, the period has been an error, here is a simple record of the process.

Tools: idea-2017,linux-7.x,mysql-5.6.40.

First, let's talk about the effects of these annotations:

(1)Spring Boot @SpringBootApplication,@ Enableautoconfiguration,@ComponentScan annotations, I often foolishly confused on the startup class,

(2)@EnableAutoConfiguration This note is springboot Automatically configured according to the jar package We introduced, let's say we have our own configuration class that overrides

(3)@ComponentScan is to tell springboot to scan the package

(4) @SpringBootApplication annotations are equivalent to using @Configurationwith default properties , @ Enableautoconfiguration and @ComponentScan

since Springboot automatically scans the startup class directory and subdirectories, the startup class is generally placed on the outermost layer of the package, see:cn.buding is the company name or the project name.

start springboot If you want the following error:

This is springboot integrated mybatis when started without discovering mapper, which is generally mapper didn't give Springboot management, there may be quite a few ways to put Mapper Give Springboot Management, use a bar here. See, add @MapperScan annotations on the previous basis , and the parentheses are the classpath of your mapper .

here is the information that Springboot successfully started, pull to the bottom to see, you can see the port number.

Here is my control layer:

after launching, the browser accesses http://localhost:8080/getUserInfo error:

Then look backstage:

The connection timed out, look at the database configuration file:

Log on to the virtual machine that installed MySQL:

(add the Linux install mysql command start:

execution:#yum install-y Mysql-server

If the following error occurs:

Then the command to execute again, if also reported the same error, that indicates that your virtual machine configuration file has a problem, a little Baidu can be a bit.

If you report the following error:

This can be resolved:

First, execute:sudo rpm-uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.

Such as:

Then execute:# yum install MySQL mysql-server mysql-libs mysql-server

Such as:

This will install successfully.

Configure boot: # chkconfig mysqld on

start mysql service:# Service mysqld Start

then execute:# yum Install-y Mysql-connector-java

go to MySQL:# mysql

Set Password:update mysql.user set Password=password (' 123456 ') where user= ' root ';

Brush:flush privileges;

quit:quit

re-login:# mysql-uroot-p123456

CREATE DATABASE eshop default character set UTF8;

View database: show databases;

Access database use eshop;

View table:show tables;

Creating tables: CREATE TABLE User (

Age Int (11),

-Name varchar (20)

);

View table:show tables;

Insert Data:Insert User values ("Yu");

View Database IP access rights:Select host from Mysql.user;select host,user from Mysql.user; (if this is an error, try to put two of these Mysql.user Replace User )

Updated:update mysql.user set host = '% ' where user = ' root ';

There is no reason for this error.

View database again:select host from Mysql.user;

Brush:flush privileges;

exit:quit;

Open 3306 port;

Enter:# Cd/etc/sysconfig

If there is no iptables

just create a:# VI Iptables

Paste the following words into the past.

# Firewall configuration written by System-config-firewall

# Manual Customization of this file are not recommended.

*filter

: INPUT ACCEPT [0:0]

: FORWARD ACCEPT [0:0]

: OUTPUT ACCEPT [0:0]

-A input-m state--state established,related-j ACCEPT

-A input-p icmp-j ACCEPT

-A input-i lo-j ACCEPT

-A input-m state--state new-m tcp-p TCP--dport 22-j ACCEPT

-A input-m state--state new-m tcp-p TCP--dport 3306-j ACCEPT

-A input-j REJECT--reject-with icmp-host-prohibited

-A forward-j REJECT--reject-with icmp-host-prohibited

COMMIT

exit, restart the network:# service Iptables restart

If you report the following error:

do it:# yum Install Iptables-service

If the error is displayed:

on execution:# systemctl Stop Firewalld

# Systemctl Mask Firewalld

re-execute:# yum Install iptables-services

setboot up: # Systemctl enable Iptables

Restart Network:# service Iptables restart

View open ports:# IPTABLES-L-N

add the Linux install mysql command endhere)

here is basically the end, using springboot can access the database, restart the springboot project, you can see:

Finally, remember the MySQL database login problem:

If you log in to the MySQL report:

check a lot of Baidu, changed a lot of files, not yet, and finally use the connection tool Navicat Open the connection, locate the MySQL database, open the user table,

I changed it, and when I opened it, I found it.localhostthat's fine.Userand thePasswordis empty, just put127.0.0.1that's fine.Userand thePasswordcopied tolocalhost, and then putlocalhostthat line exceptpassword_expiredThis does not change, the others areNthe full change toYyou can do it.

Remember springboot access to Linux MySQL database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.