服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|

服务器之家 - 数据库 - Mysql - centos7安装mysql并jdbc测试教程

centos7安装mysql并jdbc测试教程

2020-07-07 16:18张仕宗 Mysql

这篇文章主要为大家详细介绍了centos7安装mysql并jdbc测试教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

yum安装mysql5.5

之前用rpm安装方式安装不成功,换成yum安装之后安装ok了,在网上搜索到很多的rmp安装和tar包安装的方式,但是是centos7.x与centos6.x做了很大的改变,可能别人的6.x不适合7.x的安装,尤其是对于像博主一样的新人来说,照搬教程可能导致安装不成功,如果你rmp安装失败,那么尝试跟着本教程来吧。

卸载已经存在的MySQL

?
1
2
3
4
5
6
7
[root@shizongger bin]# rpm -qa|grep mysql
[root@shizongger bin]# rpm -qa mysql
[root@shizongger bin]# rpm -qa|grep -i mysql
MySQL-client-5.5.54-1.el7.x86_64
[root@shizongger bin]# rpm -e --nodeps M
ModemManager ModemManager-glib MySQL-client
[root@shizongger bin]# rpm -e --nodeps MySQL-client

更新mysql的yum源

?
1
2
[root@shizongger bin]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
[root@shizongger bin]# rpm -ivh mysql-community-release-el7-5.noarch.rpm

yum安装

?
1
[root@shizongger bin]# yum install mysql-community-server

不出意外,mysql就已经安装成功,因为yum安装是傻瓜式安装嘛,现在启动mysql服务。

?
1
[root@shizongger bin]# service mysqld start

查看mysql是否启动,可以监听它的端口号,mysql监听的段口号是3306,现在我们来监听3306端口是否已经启动:

?
1
2
3
[shizongger@shizongger src]$ netstat -anp |grep 3306
(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)
tcp6 0 0 :::3306 :::* LISTEN -

mysql服务已经起来了,可以登陆数据库了,第一次登陆数据密码为空,然后再去给数据库配置root的密码。

?
1
2
3
[root@shizongger bin]# mysql -uroot
mysql> set password for 'root'@'localhost' =password('root');
mysql>exit

用新的密码登陆

?
1
2
3
4
5
6
7
8
9
10
11
[root@shizongger bin]# mysql -uroot -proot
mysql> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+

到这里已经成功的登陆本地数据库了,停止mysql服务的命令是:

?
1
[root@shizongger bin]# service mysqld stop

好了,本地的mysql服务已经搭建起来了,作为一个java程序员,那么下面请继续

测试jdbc

首先需要准备好jar包,mysql只需要一个jar包:mysql-connector-java-3.0.14-production-bin.jar,如果你是用vi/vim作为编辑工具,那么你的jar包需要放在jdk的lib文件夹下面,或者放在一个独立的地方,然后将其加入classpath里面,我在centos下面用ide eclipse来开发,这里以eclipse为例子说来(感觉自己在linux用ide,有点low).复制+粘帖,放在项目的lib下面。接着来开放我们的jdbc测试用例。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class JdbcTest {
 
  public static void main(String[] args) {
    Connection conn = null;
    Statement sm = null;
    ResultSet rs = null;
 
    try {
      Class.forName("com.mysql.jdbc.Driver");
      String url = "jdbc:mysql://localhost:3306/shopping?zeroDateTimeBehavior=convertToNull";
      conn = DriverManager.getConnection(url, "root", "root");
      sm = conn.createStatement();
      rs = sm.executeQuery("select * from user");
      while(rs.next()) {
        System.out.println("id:" + rs.getInt("id") + " name:" + rs.getString("name"));
      }
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try{
        if(rs != null) {
          rs.close();
          rs = null;
        }
        if(sm != null) {
          sm.close();
          sm = null;
        }
        if(conn != null) {
          conn.close();
          conn = null;
        }
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
 
  }
 
}

在安装好mysql之后,为在我的mysql中创建了shopping的数据库,并在其中添加了user的表,表结构只有一个int型的id和varchar类型的name.
到这一步,java开放中的数据库准备基本完成。linux安装软件会比window麻烦,这篇博客也并非符合一切机器,当你遇到困难或者安装失败的时候,请耐心,尽力之后终于有解决办法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/zhang5476499/article/details/53791710

延伸 · 阅读

精彩推荐