Openfire + Smack linux下基本環境搭建和測試


Openfire Install
------------------
xiaoyang @2012-4-13

環境:fedora16,linux3.2內核,mysql服務器

(1)openfire安裝
默認安裝在/opt/openfire目錄下面。
xiaoyang@xiaoyang openfire$ sudo rpm -ivh openfire-3.7.1-1.i386.rpm
[sudo] password for xiaoyang:
Preparing...                ########################################### [100%]
   1:openfire               ########################################### [100%]

若防火牆開啟,則需要加入例外規則
xiaoyang@xiaoyang openfire$ vi /etc/sysconfig/iptables
追加2行
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 9090 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 9091 -j ACCEPT

啟動openfire。
xiaoyang@xiaoyang openfire$ sudo /etc/init.d/openfire restart
[sudo] password for xiaoyang:
Shutting down openfire:
Starting openfire:

(2)mysql基本配置
數據庫准備。我使用的是mysql.i686 VERSION:5.5.21-1.fc16

修改my.cfg數據庫配置文件,在最后添加兩行
[mysql]                        #xiaoyang add
default-character-set=utf8    #xiaoyang add


Fedora16默認沒有啟動,啟動數據庫。
xiaoyang@xiaoyang openfire$ service mysqld status
xiaoyang@xiaoyang openfire$ sudo service mysqld restart
xiaoyang@xiaoyang openfire$ service mysqld status
Redirecting to /bin/systemctl  status mysqld.service
mysqld.service - MySQL database server
      Loaded: loaded (/lib/systemd/system/mysqld.service; disabled)
      Active: active (running) since Fri, 13 Apr 2012 13:08:42 +0800; 9s ago
     Process: 5861 ExecStartPost=/usr/libexec/mysqld-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
     Process: 5722 ExecStart=/usr/bin/mysqld_safe --nowatch --basedir=/usr (code=exited, status=0/SUCCESS)
     Process: 5654 ExecStartPre=/usr/libexec/mysqld-prepare-db-dir (code=exited, status=0/SUCCESS)
    Main PID: 5860 (mysqld)
      CGroup: name=systemd:/system/mysqld.service
          └ 5860 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysqld.log --pid-file...

看到active就行了。

檢測mysql開放端口,默認是3306.
xiaoyang@xiaoyang openfire$ netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN   


輸入密碼后進入mysql:
xiaoyang@xiaoyang openfire$ mysql -u root -p

創建測試用戶:
mysql> insert into mysql.user(Host,User,Password) values("localhost","user1",password("user1"));
mysql> flush privileges;

新安裝的MySQL是默認不開戶遠程訪問權限的,也就不能外部連接MySQL。這樣對想遠程調用MySQL的數據很不方便。下面我們來看下如何設置允許遠程連接MySQL數據庫。
登錄MySQL
    mysql -uroot -p
如果無法登錄可以使用find / -name mysql搜索mysql程序的路徑,比如mysql程序路徑為:/usr/local/mysql/bin/mysql,我們可以這樣登錄:
    /usr/local/mysql/bin/mysql -uroot -p
執行如下命令:
    grant all privileges on *.* to 'root'@'%' identified by 'ROOT-PASSWORD';
    flush privileges;
    exit


(3)
mysql for openfire

創建opefire數據庫
mysql> create database openfire;
Query OK, 1 row affected (0.00 sec)

套用openfire的數據庫模板
xiaoyang@xiaoyang ~$ vim /opt/openfire/resources/database/openfire_mysql.sql
xiaoyang@xiaoyang ~$ sudo mysql -u root < /opt/openfire/resources/database/openfire_mysql.sql  -p

重啟openfire
xiaoyang@xiaoyang ~$ sudo /etc/init.d/openfire restart
Shutting down openfire:
Starting openfire:
xiaoyang@xiaoyang ~$

(4)
配置openfire。
瀏覽器打開 http://[本機IP]:9090/setup/index.jsp
    a)首先選擇語言,中文
    b)其次配置域ip和控制台端口,端口默認是9090
    c)然后選擇數據庫連接,
        使用標准數據庫連接”是連接外部MySQL數據
        注意如果出現錯誤,error.log顯示:
            Access denied for user 'root'@'192.168.1.3' (using password:YES)
        錯誤,請查看自己的mysql user表。
            mysql> use user
            ERROR 1049 (42000): Unknown database 'user'
            mysql> insert into  user (host,user) values ('xiaoyang.hit.edu.cn','root');
            ERROR 1062 (23000): Duplicate entry 'xiaoyang.hit.edu.cn-root' for key 'PRIMARY'
            mysql> select host,user from user;
            +---------------------+-------+
            | host                | user  |
            +---------------------+-------+
            | %                   | root  |
            | 127.0.0.1           | root  |
            | ::1                 | root  |
            | localhost           |       |
            | localhost           | root  |
            | localhost           | user1 |
            | xiaoyang.hit.edu.cn |       |
            | xiaoyang.hit.edu.cn | root  |
            +---------------------+-------+
            8 rows in set (0.00 sec)

        如在本地,我的user表中root密碼為'%',填寫密碼時空白即可。
    d)進入“標准數據連接”配置頁面,配置MySQL連接;配置后會在外部數據庫中創建表和字段;
    e)進入用戶和組配置界面,
        “初始配置”既在數據庫中保存用戶和用戶組資料
        “目錄服務器(LDAP)”就是使用外部LDAP服務器來管理
    f) 進入管理員密碼配置界面,修改管理員密碼;
    g)配置完畢,進入“管理控制台”,額外添加一個管理員帳號;默認登錄名是admin,密碼就是上一步填寫的密碼。可以在openfire數據庫的ofUser表中自己加用戶。

    至此,可以看到openfire 服務器console了。
   

下面可以使用smack插件進行一個簡單的測試。

package cn.edu.hit;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;

public class SmackTest {
	public static void main(String[] args) throws XMPPException {
		// 5222是openfire通信的端口
		ConnectionConfiguration config = new ConnectionConfiguration(
				"127.0.0.1", 5222);
		XMPPConnection connection = new XMPPConnection(config);
		XMPPConnection.DEBUG_ENABLED = true;
		connection.connect();
		connection.login("user1", "user1");
		connection.addPacketListener(new PacketListener() {
			@Override
			public void processPacket(Packet arg0) {
				System.out.println(arg0);
			}
		}, new PacketFilter() {

			@Override
			public boolean accept(Packet arg0) {
				return true;
			}
		});
		IQ iq = new IQ() {
			@Override
			public String getChildElementXML() {
				StringBuilder buf = new StringBuilder();
				buf.append("");
				return buf.toString();

			}
		};

		connection.sendPacket(iq);
		connection.disconnect();
		System.out.println("exit!\n");
		System.exit(0);
	}

}

運行完畢可以在控制台中的用戶界面看到 登錄記錄。

 


免責聲明!

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



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