当前位置: 移动技术网 > 网络运营>服务器>Linux > centos7安装mysql并jdbc测试实例详解

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

2019年04月25日  | 移动技术网网络运营  | 我要评论
centos7安装mysql并jdbc测试实例详解 前言: 之前用rpm安装方式安装不成功,换成yum安装之后安装ok了,在网上搜索到很多的rmp安装和tar包安装的方

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

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网