centos7安裝mysql并jdbc測(cè)試實(shí)例詳解
centos7安裝mysql并jdbc測(cè)試實(shí)例詳解
前言:
之前用rpm安裝方式安裝不成功,換成yum安裝之后安裝ok了,在網(wǎng)上搜索到很多的rmp安裝和tar包安裝的方式,但是是centos7.x與centos6.x做了很大的改變,可能別人的6.x不適合7.x的安裝,尤其是對(duì)于像博主一樣的新人來說,照搬教程可能導(dǎo)致安裝不成功,如果你rmp安裝失敗,那么嘗試跟著本教程來吧。
先卸載已經(jīng)存在的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就已經(jīng)安裝成功,因?yàn)閥um安裝是傻瓜式安裝嘛,現(xiàn)在啟動(dòng)mysql服務(wù)。
[root@shizongger bin]# service mysqld start
查看mysql是否啟動(dòng),可以監(jiān)聽它的端口號(hào),mysql監(jiān)聽的段口號(hào)是3306,現(xiàn)在我們來監(jiān)聽3306端口是否已經(jīng)啟動(dòng):
[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服務(wù)已經(jīng)起來了,可以登陸數(shù)據(jù)庫(kù)了,第一次登陸數(shù)據(jù)密碼為空,然后再去給數(shù)據(jù)庫(kù)配置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 | +--------------------+
到這里已經(jīng)成功的登陸本地?cái)?shù)據(jù)庫(kù)了,停止mysql服務(wù)的命令是:
[root@shizongger bin]# service mysqld stop
好了,本地的mysql服務(wù)已經(jīng)搭建起來了,作為一個(gè)Java程序員,那么下面請(qǐng)繼續(xù)
測(cè)試jdbc
首先需要準(zhǔn)備好jar包,mysql只需要一個(gè)jar包:mysql-connector-java-3.0.14-production-bin.jar,如果你是用vi/vim作為編輯工具,那么你的jar包需要放在jdk的lib文件夾下面,或者放在一個(gè)獨(dú)立的地方,然后將其加入classpath里面,我在centos下面用ide eclipse來開發(fā),這里以eclipse為例子說來(感覺自己在Linux用ide,有點(diǎn)low).復(fù)制+粘帖,放在項(xiàng)目的lib下面。接著來開放我們的jdbc測(cè)試用例。
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中創(chuàng)建了shopping的數(shù)據(jù)庫(kù),并在其中添加了user的表,表結(jié)構(gòu)只有一個(gè)int型的id和varchar類型的name.
到這一步,java開放中的數(shù)據(jù)庫(kù)準(zhǔn)備基本完成。linux安裝軟件會(huì)比window麻煩,這篇博客也并非符合一切機(jī)器,當(dāng)你遇到困難或者安裝失敗的時(shí)候,請(qǐng)耐心,盡力之后終于有解決辦法。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- CentOS 6.4安裝配置LAMP服務(wù)器(Apache+PHP5+MySQL)
- CentOS+Nginx+PHP+MySQL詳細(xì)配置(圖解)
- CentOS 6.4安裝配置LNMP服務(wù)器(Nginx+PHP+MySQL)
- centos6利用yum安裝php mysql gd的步驟
- CentOS 5.5下安裝MySQL 5.5全過程分享
- CentOS系統(tǒng)中PHP和MySQL的升級(jí)方法
- CentOS Linux更改MySQL數(shù)據(jù)庫(kù)目錄位置具體操作
- 在CentOS 6 中安裝WordPress(一) 安裝Apache,Mysql, PHP環(huán)境
- centos 下面安裝python2.7 +pip +mysqld
- CentOS Linux 下配置Apache2+PHP5+MySQL5+GD庫(kù)的方法
- CentOS 5.4 服務(wù)器配置 yum安裝Apache+php+Mysql
- CentOS 6.5下yum安裝 MySQL-5.5全過程圖文教程
相關(guān)文章
Linux之配置路由轉(zhuǎn)發(fā)功能的測(cè)試
這篇文章主要介紹了Linux之配置路由轉(zhuǎn)發(fā)功能的測(cè)試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05Linux如何設(shè)置java.library.path
這篇文章主要介紹了Linux如何設(shè)置java.library.path問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12關(guān)于Linux安裝mysql默認(rèn)配置文件位置詳解
本篇文章主要介紹了關(guān)于Linux安裝mysql默認(rèn)配置文件位置詳解,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-06-06linux?類型??版本?內(nèi)存?磁盤?查詢命令介紹
這篇文章介紹了linux?類型?、版本、內(nèi)存、磁盤?查詢命令,希望本篇文章可以幫助到大家的學(xué)習(xí),喜歡本篇文章可以收藏一下方便下次瀏覽2021-11-11linux 網(wǎng)絡(luò)編程 socket選項(xiàng)的實(shí)現(xiàn)
這篇文章主要介紹了linux 網(wǎng)絡(luò)編程 socket選項(xiàng)的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06Apache 的 order deny allow 設(shè)置說明
Allow和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用來控制目錄和文件的訪問授權(quán)。2010-12-12