centos7安装mysql并jdbc测试实例详解

前言:

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

先卸载已经存在的MySQL。

 [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源

[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安装

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

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

[root@shizongger bin]# service mysqld start

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

[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的密码。

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

用新的密码登陆

  [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服务的命令是:

[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测试用例。

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麻烦,这篇博客也并非符合一切机器,当你遇到困难或者安装失败的时候,请耐心,尽力之后终于有解决办法。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部