Ubuntu下Tomcat串連MySql資料庫

來源:互聯網
上載者:User

Linux下在tomcat上配置MySql資料庫沒Windows那麼簡單,得折騰一翻。。。因此值得寫下來做個記錄,供日後參考。

1. 下載安裝MySQL

Ubuntu下安裝MySQL非常簡單,只需要:

sudo apt-get install mysql-server

安裝過程中會讓你設定使用者名稱、密碼,我的是root,root.

2、下載JDBC

去http://www.mysql.com/downloads/下載MySQL Connectors for Java

解壓,然後把mysql-connector-java-5.1.24-bin.jar放到%TOMCAT_HOME/lib目錄下

3. 配置Tomcat

開啟%TOMCAT_HOME/conf/context.xml檔案,

加入以下代碼:

<Resource name="jdbc/mysqlds" auth="Container"  type="javax.sql.DataSource" maxIdle="30" maxWait="3000"  maxActive="10" username="root" password="root" driverClassName="org.gjt.mm.mysql.Driver"     url="jdbc:mysql://localhost/lc">    </Resource>

注意 username 和 password 設定成在安裝mySQL的時候設定的數值。

這時就可以用寫JSP代碼進行資料庫相關操作了。

但此時還會有一個問題,那就是當我們在終端中操作mysql時,會報 "mysql access denied for user" 的錯誤

解決方案是,在終端中輸入:

sudo mysql -uroot -p

然後再輸入root使用者的密碼,此時就能正常訪問mysql資料庫了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.