diff --git a/data/sql/default.sql b/data/sql/default.sql index f5367322e..aecc32ee5 100755 --- a/data/sql/default.sql +++ b/data/sql/default.sql @@ -19,19 +19,6 @@ CREATE TABLE IF NOT EXISTS `binding` ( ); -CREATE TABLE IF NOT EXISTS `config` ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT, - `webserver` TEXT, - `backup_path` TEXT, - `sites_path` TEXT, - `status` INTEGER, - `mysql_root` TEXT -); - -INSERT INTO `config` (`id`, `webserver`, `backup_path`, `sites_path`, `status`, `mysql_root`) VALUES -(1, 'nginx', '/www/backup', '/www/wwwroot', 0, 'admin'); - - CREATE TABLE IF NOT EXISTS `crontab` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT, @@ -50,17 +37,6 @@ CREATE TABLE IF NOT EXISTS `crontab` ( `urladdress` TEXT ); -CREATE TABLE IF NOT EXISTS `databases` ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT, - `pid` INTEGER, - `name` TEXT, - `username` TEXT, - `password` TEXT, - `accept` TEXT, - `ps` TEXT, - `addtime` TEXT -); - CREATE TABLE IF NOT EXISTS `firewall` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `port` TEXT, diff --git a/plugins/mysql/class/mysql.py b/plugins/mysql/class/mysql.py new file mode 100755 index 000000000..6f1b058ab --- /dev/null +++ b/plugins/mysql/class/mysql.py @@ -0,0 +1,79 @@ +# coding: utf-8 + +import re +import os +import sys + +sys.path.append("/usr/local/lib/python2.7/site-packages") + + +class mysql: + __DB_PASS = None + __DB_USER = 'root' + __DB_PORT = 3306 + __DB_HOST = 'localhost' + __DB_CONN = None + __DB_CUR = None + __DB_ERR = None + __DB_CNF = '/etc/my.cnf' + + def __Conn(self): + '''连接MYSQL数据库''' + try: + import public + socket = '/tmp/mysql.sock' + try: + import MySQLdb + except Exception, ex: + self.__DB_ERR = ex + return False + try: + myconf = public.readFile(self.__DB_CNF) + rep = "port\s*=\s*([0-9]+)" + self.__DB_PORT = int(re.search(rep, myconf).groups()[0]) + except: + self.__DB_PORT = 3306 + #self.__DB_PASS = public.M('config').where('id=?', (1,)).getField('mysql_root') + try: + self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) + except MySQLdb.Error, e: + self.__DB_HOST = '127.0.0.1' + self.__DB_CONN = MySQLdb.connect(host=self.__DB_HOST, user=self.__DB_USER, passwd=self.__DB_PASS, + port=self.__DB_PORT, charset="utf8", connect_timeout=1, unix_socket=socket) + self.__DB_CUR = self.__DB_CONN.cursor() + return True + except MySQLdb.Error, e: + self.__DB_ERR = e + return False + + def execute(self, sql): + # 执行SQL语句返回受影响行 + if not self.__Conn(): + return self.__DB_ERR + try: + result = self.__DB_CUR.execute(sql) + self.__DB_CONN.commit() + self.__Close() + return result + except Exception, ex: + return ex + + def query(self, sql): + # 执行SQL语句返回数据集 + if not self.__Conn(): + return self.__DB_ERR + try: + self.__DB_CUR.execute(sql) + result = self.__DB_CUR.fetchall() + # 将元组转换成列表 + data = map(list, result) + self.__Close() + return data + except Exception, ex: + return ex + + # 关闭连接 + def __Close(self): + self.__DB_CUR.close() + self.__DB_CONN.close() diff --git a/plugins/mysql/conf/mysql.sql b/plugins/mysql/conf/mysql.sql new file mode 100755 index 000000000..6413b9e40 --- /dev/null +++ b/plugins/mysql/conf/mysql.sql @@ -0,0 +1,17 @@ +CREATE TABLE IF NOT EXISTS `config` ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT, + `mysql_root` TEXT +); + +INSERT INTO `config` (`id`, `mysql_root`) VALUES (1, 'admin'); + +CREATE TABLE IF NOT EXISTS `databases` ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT, + `pid` INTEGER, + `name` TEXT, + `username` TEXT, + `password` TEXT, + `accept` TEXT, + `ps` TEXT, + `addtime` TEXT +); diff --git a/plugins/mysql/index.py b/plugins/mysql/index.py index 966a7eaec..1095e908c 100755 --- a/plugins/mysql/index.py +++ b/plugins/mysql/index.py @@ -10,6 +10,7 @@ import re sys.path.append(os.getcwd() + "/class/core") import public + app_debug = False if public.isAppleSystem(): app_debug = True @@ -22,6 +23,9 @@ def getPluginName(): def getPluginDir(): return public.getPluginDir() + '/' + getPluginName() +sys.path.append(getPluginDir() + "/class") +import mysql + def getServerDir(): return public.getServerDir() + '/' + getPluginName() @@ -51,7 +55,7 @@ def getArgs(): def getConf(): - path = getServerDir() + '/conf/my.cnf' + path = getServerDir() + '/etc/my.cnf' return path @@ -68,6 +72,24 @@ def contentReplace(content): return content +def pSqliteDb(): + file = getServerDir() + '/mysql.db' + name = 'mysql' + if not os.path.exists(file): + conn = public.M(name).dbPos(getServerDir(), name) + csql = public.readFile(getPluginDir() + '/conf/mysql.sql') + csql_list = csql.split(';') + for index in range(len(csql_list)): + conn.execute(csql_list[index], ()) + else: + conn = public.M(name).dbPos(getServerDir(), name) + return conn + + +def pMysqlDb(): + return '' + + def initDreplace(): initd_tpl = getInitdTpl() @@ -82,7 +104,7 @@ def initDreplace(): public.writeFile(file_bin, content) public.execShell('chmod +x ' + file_bin) - mysql_conf_dir = getServerDir() + '/conf' + mysql_conf_dir = getServerDir() + '/etc' if not os.path.exists(mysql_conf_dir): os.mkdir(mysql_conf_dir) @@ -120,6 +142,10 @@ def initMysqlData(): cmd = 'cd ' + serverdir + ' && ./scripts/mysql_install_db --user=midoks --basedir=' + \ serverdir + ' --ldata=' + datadir public.execShell(cmd) + + pwd = public.getRandomString(16) + cmd_pass = serverdir + '/bin/mysqladmin -uroot -p12345' + print cmd_pass return True @@ -187,6 +213,14 @@ def initdUinstall(): return 'ok' +def runInfo(): + db = mysql.mysql() + db.__DB_CNF = getConf() + data = db.query('show global status') + print data + return 'ok' + + def getShowLog(): return 'ok' diff --git a/requirements.txt b/requirements.txt index 6bbd1e571..5326ae4d6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ flask_session gunicorn psutil pillow -chardet \ No newline at end of file +chardet +MySQL-python \ No newline at end of file