[saiku] 將saiku自帶的H2嵌入式數據庫遷移到本地mysql數據庫


 

 

saiku數據庫的表和用戶默認創建是在啟動項目的時候,通過初始化 saiku-beans.xml 中的 h2database 這個 bean 

執行org.saiku.service.Database類的init方法來初始化數據表和默認用戶的。

 

具體修改如下:

1/修改web.xml 中 數據連接配置信息

將數據庫鏈接修改為本地mysql配置信息

  <context-param>
    <param-name>db.url</param-name>
    <param-value>jdbc:mysql://localhost:3306/saiku</param-value>
  </context-param>
  <context-param>
    <param-name>db.user</param-name>
    <param-value>root</param-value>
  </context-param>
  <context-param>
    <param-name>db.password</param-name>
    <param-value>root</param-value>
  </context-param>

 

2/修改saiku-beans.xml中h2database配置信息

新增mysql數據源適配:

 

    <bean id="h2database" class="org.saiku.database.Database" init-method="init">
        <property name="datasourceManager" ref="repositoryDsManager"/>
        <property name="datasourcetype" value="mysql" />
    </bean>

 

3/修改Database中獲取的數據源為mysql

修改Database類:

private String datasourcetype = "mysql";
private void initDB() {

        String url = servletContext.getInitParameter("db.url");
        String user = servletContext.getInitParameter("db.user");
        String pword = servletContext.getInitParameter("db.password");
        
        if (this.datasourcetype.equals("mysql")) {
            ds = new MysqlDataSource();
            ((MysqlDataSource) ds).setUrl(url);
            ((MysqlDataSource) ds).setUser(user);
            ((MysqlDataSource) ds).setPassword(pword);
        } else if (this.datasourcetype.equals("h2")) {
            ds = new JdbcDataSource();
            ((JdbcDataSource) ds).setUrl(url);
            ((JdbcDataSource) ds).setUser(user);
            ((JdbcDataSource) ds).setPassword(pword);
        }
        
    }

 

4/因為h2建表sql和mysql的還是有差異的,所以將創建表和用戶信息的代碼替換如下:

mysql數據表和用戶的創建代碼為:

 

    private void loadUsers() throws SQLException {
     
        Connection c = ds.getConnection();
      
        Statement statement = c.createStatement();
        
        statement.execute(" CREATE TABLE IF NOT EXISTS log ( time  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, log  TEXT); ");
        statement.execute(" CREATE TABLE IF NOT EXISTS users(user_id INT(11) NOT NULL AUTO_INCREMENT, " + " username VARCHAR(45) NOT NULL UNIQUE, password VARCHAR(100) NOT NULL, email VARCHAR(100), " + " enabled TINYINT NOT NULL DEFAULT 1, PRIMARY KEY(user_id)); ");
        statement.execute(" CREATE TABLE IF NOT EXISTS user_roles ( " + " user_role_id INT(11) NOT NULL AUTO_INCREMENT,username VARCHAR(45), "  + " user_id INT(11) NOT NULL REFERENCES users(user_id), " + " ROLE VARCHAR(45) NOT NULL, " + " PRIMARY KEY (user_role_id)); ");
    
        ResultSet result = statement.executeQuery("select count(*) as c from log where log = 'insert users'");
        
        result.next();
        
        if (result.getInt("c") == 0) {
            
            statement.execute("INSERT INTO users (username,password,email, enabled) VALUES ('admin','admin', 'test@admin.com',TRUE);");
            statement.execute("INSERT INTO users (username,password,enabled) VALUES ('smith','smith', TRUE);");
            statement.execute("INSERT INTO user_roles (user_id, username, ROLE) VALUES (1, 'admin', 'ROLE_USER');");
            statement.execute("INSERT INTO user_roles (user_id, username, ROLE) VALUES (1, 'admin', 'ROLE_ADMIN');");
            statement.execute("INSERT INTO user_roles (user_id, username, ROLE) VALUES (2, 'smith', 'ROLE_USER');");
            statement.execute("INSERT INTO log (log) VALUES('insert users');");
        }

        String encrypt = servletContext.getInitParameter("db.encryptpassword");
        if (encrypt.equals("true") && !checkUpdatedEncyption()) {
            updateForEncyption();
        }
    }

 
    public boolean checkUpdatedEncyption() throws SQLException{
        Connection c = ds.getConnection();
        Statement statement = c.createStatement();
        ResultSet result = statement.executeQuery("select count(*) as c from log where log = 'update passwords'");
        result.next();
        return result.getInt("c") != 0;
    }
 
    public void updateForEncyption() throws SQLException {
        
        Connection c = ds.getConnection();
        Statement statement = c.createStatement();
        statement.execute("ALTER TABLE users MODIFY COLUMN PASSWORD VARCHAR(100) DEFAULT NULL");
        ResultSet result = statement.executeQuery("select username, password from users");
        while (result.next()) {
            statement = c.createStatement();
            String pword = result.getString("password");
            String hashedPassword = passwordEncoder.encode(pword);
            String sql = "UPDATE users " + "SET password = '" + hashedPassword
                    + "' WHERE username = '" + result.getString("username")
                    + "'";
            statement.executeUpdate(sql);
        }
        statement = c.createStatement();
        statement.execute("INSERT INTO log (log) VALUES('update passwords');");
    }

以上的信息修改完畢后,在本地mysql創建url中指定的database->saikuBase,此時db中無任何表

運行saiku項目成功后,刷新db,可以看見db中多出了三張表user/user_roles/log,說明初始化表和默認數據已經成功。

但是,此時只是創建了表結構和默認的登陸賬戶,並沒有修改登錄的用戶認證數據源。

當我們使用admin登陸時還是會訪問到jdbc中配置的h2數據庫數據源。

其實這時候還是通過訪問h2內嵌數據庫的db文件登陸的,在管理端新建的用戶也會保存在h2數據源中,mysql數據源中查詢不到新建的用戶

所以,接下來要修改認證數據源為本地的mysql

 

5/修改用戶認證數據源

將 bean id 為 datasource 的 數據源的配置信息改為我們本地mysql的配置信息,重新部署服務並啟動

這時,我們在管理端創建一個用戶saiku,點擊保存。

查看本地mysql數據庫的user表,能看見saiku已經存在user表中了,並且使用saiku用戶登錄也能登錄成功。

將*security-jdbc.xml 中的配置信息改成mysql的即可

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >  
        <property name="driverClassName" value="mysql的驅動"/>  
        <property name="url" value="mysql的url"/>  
        <property name="username" value="賬戶名"/>  
        <property name="password" value="密碼"/>  
</bean>

至此就完成了h2數據庫遷移到本地mysql的操作了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM