PDA

View Full Version : نصب خودکار دیتابیس



eshpilen
جمعه 28 بهمن 1390, 19:42 عصر
من میخوام واسه پروژم برنامهء نصب خودکار دیتابیس درست کنم.

میشه از طریق PHP با دستور create database دیتابیس رو ایجاد کرد؟ یا فقط از طریق کنترل پنل میشه این کار رو انجام داد؟

بعدش برای ایجاد کردن جدول ها چطور؟

yones_safari
جمعه 28 بهمن 1390, 19:56 عصر
دادا میشه جدول رو ساخت ولی دیتابیس رو نمیدونم.
چون اکثر نرم افزار ها مثل جوملا،ویبولتین و ... هم نام پایگاه رو از کاربر میگیرن پس به احتمال زیاد نشه.

wordnet
جمعه 28 بهمن 1390, 20:00 عصر
سلام

منم همین سوالو دارم دوستان لطفا با نمونه کمک کنید یه نمونه install.php بزارید

Net So
جمعه 28 بهمن 1390, 20:01 عصر
میخوای installer برای اسکریپتت بسازی ؟ بهترین حالتش اینه که اگر سرور دست خودت هست با ssh بنویسی چون اکثر Hosting های معتبر Create DB رو میبندن . مخصوصا اگه سرورت ویندوزی باشه.

wordnet
جمعه 28 بهمن 1390, 20:03 عصر
این اسکریپت های مثل وردپرس چجوری هست؟

eshpilen
جمعه 28 بهمن 1390, 20:03 عصر
ای بابا پیچیده شد پس.
پس بگم کاربر خودش دیتابیس رو ایجاد کنه، بعد اسم دیتابیس رو در فایل کانفیگ وارد و سیو کنه؟ بعد فقط جدولها رو ایجاد کنم؟

بعدشم من الان این کد رو روی هاست تست کردم:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

mysql_connect('localhost', 'myacc_uu', '123456');

mysql_select_db('myacc_xx');

mysql_query('CREATE TABLE IF NOT EXISTS `xxs` (
`uid` char(8) CHARACTER SET ascii COLLATE ascii_bin NOT NULL DEFAULT '',
`username` varchar(30) CHARACTER SET utf8 DEFAULT NULL,
`password_hash` char(60) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
`email` varchar(40) CHARACTER SET ascii DEFAULT NULL,
`gender` char(1) CHARACTER SET ascii DEFAULT NULL,
`loginkey` char(43) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
`sesskey` mediumint(8) unsigned DEFAULT NULL,
`timestamp` int(10) unsigned NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci');

?>


هیچ خطایی نداد ولی هیچ جدولی هم در دیتابیس myacc_xx که از طریق کنترل پنل هاست ایجاد کرده بودم ایجاد نشد.
نمیدونم مشکل کجاست :متفکر:

wordnet
جمعه 28 بهمن 1390, 20:08 عصر
من دنبال یه کد مثل نصب وردپرسم

eshpilen
جمعه 28 بهمن 1390, 20:24 عصر
آهان درست شد کوئری ایراد داشت :لبخند:

پس بذاریم دیتابیس رو کاربر ایجاد کنه. بعد اسم دیتابیس و نام کاربری و کلمهء عبور دیتابیس رو در فایل کانفیگ برنامه وارد و سیو کنه. بعد ما فقط جدول ها رو ایجاد میکنیم.

eshpilen
جمعه 28 بهمن 1390, 20:25 عصر
من دنبال یه کد مثل نصب وردپرسم
چه شکلیه مگه؟

wordnet
جمعه 28 بهمن 1390, 20:33 عصر
ببینید من یه کد میخوام که کاربر توی فروم اطلاعات دیتابسی که ساخته بده و جداول از پیش تعیین شده نصب بشه

persian-boy
جمعه 28 بهمن 1390, 20:40 عصر
ببینید من یه کد میخوام که کاربر توی فروم اطلاعات دیتابسی که ساخته بده و جداول از پیش تعیین شده نصب بشه

با mysql_query انجام بدید خب :d

akbarg64
جمعه 28 بهمن 1390, 20:50 عصر
<?php
$con = mysql_connect("localhost","root","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

mysql_close($con);
?>

eshpilen
جمعه 28 بهمن 1390, 22:53 عصر
من از این روش برای ایجاد جدول های پروژه استفاده کردم:


foreach (glob('sql/*.sql') as $file) {
$contents=file_get_contents($file);
$query=substr($contents, strpos($contents, 'CREATE TABLE IF NOT EXISTS'));
$db->query($query);
$tablename=basename($file, '.sql');
echo "Table <span style=\"color: green\">$tablename</span> created.<br>";
}

با فرض اینکه فایلهای sql که با phpMyAdmin ایجاد شدن در دایرکتوری sql باشن.

MMSHFE
شنبه 29 بهمن 1390, 08:31 صبح
دوست عزیز با PHP دیتابیس هم میتونین بسازین:


mysql_connect('localhost', 'root', '') or die('Connection error');
mysql_query('CREATE DATABASE `temp` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;');
mysql_select_db('temp') or die('Database error');

برای Collationهای دیگه هم یکبار با phpMyAdmin دیتابیس رو بسازین ببینید چه کوئری بهتون میده (بالای صفحه) و همون رو کپی کنید توی اسکریپت خودتون. حتی میتونی اسم رو از کاربر بگیرین و کوئری رو برحسب اون تغییر بدین ولی برای Collation حتماً از کوئری خود phpMyAdmin استفاده کنید چون باید برای ساخت دیتابیس، هم DEFAULT CHARACTER SET رو مشخص کنید و هم COLLATE رو (برخلاف جدول که فقط COLLATE میخواد).
موفق باشید.

eshpilen
شنبه 29 بهمن 1390, 09:53 صبح
در شیرهاست نمیشه با PHP دیتابیس ایجاد کرد.
کاربر خودش باید در کنترل پنل این کار رو بکنه.
تست هم کردم.

narsic
شنبه 29 بهمن 1390, 11:40 صبح
معمولا دستوراتی مثل ساخت دیتابیس رو اراپه دهندگان نمیبندند (هاستینگ های معتبر) اما اگه شما نمیتونید این کار رو انجام بدید به خاطر سطح دسترسی کاربری هست که توسط اون به دیتابیس متصل شدید که این سطح دسترسی در زمان کاربر تعیین میشه.
برای وارد کردن بانک اطلاعاتی هم میتونید دستور ایجاد بانک رو تست کنید در صورتی که مجوز انجامش رو داشتید خودتون ایجاد کنید در غیر اینصورت از کاربر بخواهید که انجام بده و در نهایت فایل دیتابیس رو که از دیتابیس اصلی Export کردید وارد کنید (phpMyAdmin->Operation->Export Database->Export as sql file)
برای وارد کردن مجموعه ایی از دستورات موجود در فایل به دیتابیس میتونید از کد زیر استفاده کنید.

ini_set('memory_limit', '5120M');
set_time_limit ( 0 );
/************************************************** *************************
* sql_parse.php
* -------------------
* begin : Thu May 31, 2001
* copyright : (C) 2001 The phpBB Group
* email : support@phpbb.com
*
* $Id: sql_parse.php,v 1.8 2002/03/18 23:53:12 psotfx Exp $
*
************************************************** **************************/

/************************************************** *************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
************************************************** *************************/

/************************************************** *************************
*
* These functions are mainly for use in the db_utilities under the admin
* however in order to make these functions available elsewhere, specifically
* in the installation phase of phpBB I have seperated out a couple of
* functions into this file. JLH
*
\************************************************* **************************/

//
// remove_comments will strip the sql comment lines out of an uploaded sql file
// specifically for mssql and postgres type files in the install....
//
function remove_comments(&$output)
{
$lines = explode("\n", $output);
$output = "";

// try to keep mem. use down
$linecount = count($lines);

$in_comment = false;
for($i = 0; $i < $linecount; $i++)
{
if( preg_match("/^\/\*/", preg_quote($lines[$i])) )
{
$in_comment = true;
}

if( !$in_comment )
{
$output .= $lines[$i] . "\n";
}

if( preg_match("/\*\/$/", preg_quote($lines[$i])) )
{
$in_comment = false;
}
}

unset($lines);
return $output;
}

//
// remove_remarks will strip the sql comment lines out of an uploaded sql file
//
function remove_remarks($sql)
{
$lines = explode("\n", $sql);

// try to keep mem. use down
$sql = "";

$linecount = count($lines);
$output = "";

for ($i = 0; $i < $linecount; $i++)
{
if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0))
{
if (isset($lines[$i][0]) && $lines[$i][0] != "#")
{
$output .= $lines[$i] . "\n";
}
else
{
$output .= "\n";
}
// Trading a bit of speed for lower mem. use here.
$lines[$i] = "";
}
}

return $output;

}

//
// split_sql_file will split an uploaded sql file into single sql statements.
// Note: expects trim() to have already been run on $sql.
//
function split_sql_file($sql, $delimiter)
{
// Split up our string into "possible" SQL statements.
$tokens = explode($delimiter, $sql);

// try to save mem.
$sql = "";
$output = array();

// we don't actually care about the matches preg gives us.
$matches = array();

// this is faster than calling count($oktens) every time thru the loop.
$token_count = count($tokens);
for ($i = 0; $i < $token_count; $i++)
{
// Don't wanna add an empty string as the last thing in the array.
if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0)))
{
// This is the total number of single quotes in the token.
$total_quotes = preg_match_all("/'/", $tokens[$i], $matches);
// Counts single quotes that are preceded by an odd number of backslashes,
// which means they're escaped quotes.
$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);

$unescaped_quotes = $total_quotes - $escaped_quotes;

// If the number of unescaped quotes is even, then the delimiter did NOT occur inside a string literal.
if (($unescaped_quotes % 2) == 0)
{
// It's a complete sql statement.
$output[] = $tokens[$i];
// save memory.
$tokens[$i] = "";
}
else
{
// incomplete sql statement. keep adding tokens until we have a complete one.
// $temp will hold what we have so far.
$temp = $tokens[$i] . $delimiter;
// save memory..
$tokens[$i] = "";

// Do we have a complete statement yet?
$complete_stmt = false;

for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++)
{
// This is the total number of single quotes in the token.
$total_quotes = preg_match_all("/'/", $tokens[$j], $matches);
// Counts single quotes that are preceded by an odd number of backslashes,
// which means they're escaped quotes.
$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);

$unescaped_quotes = $total_quotes - $escaped_quotes;

if (($unescaped_quotes % 2) == 1)
{
// odd number of unescaped quotes. In combination with the previous incomplete
// statement(s), we now have a complete statement. (2 odds always make an even)
$output[] = $temp . $tokens[$j];

// save memory.
$tokens[$j] = "";
$temp = "";

// exit the loop.
$complete_stmt = true;
// make sure the outer loop continues at the right point.
$i = $j;
}
else
{
// even number of unescaped quotes. We still don't have a complete statement.
// (1 odd and 1 even always make an odd)
$temp .= $tokens[$j] . $delimiter;
// save memory.
$tokens[$j] = "";
}

} // for..
} // else
}
}

return $output;
}

$dbms_schema = 'yourfile.sql';

$sql_query = @fread(@fopen($dbms_schema, 'r'), @filesize($dbms_schema)) or die('problem ');
$sql_query = remove_remarks($sql_query);
$sql_query = split_sql_file($sql_query, ';');

$host = 'localhost';
$user = 'user';
$pass = 'pass';
$db = 'database_name';

mysql_connect($host,$user,$pass) or die('error connection');
mysql_select_db($db) or die('error database selection');

$i=1;
foreach($sql_query as $sql){
echo $i++;
echo "
";
mysql_query($sql) or die('error in query');
}

کد بالا از پروژه phpBB با تغییرات جزپی کپی شده.
موفق باشید

eshpilen
شنبه 29 بهمن 1390, 17:40 عصر
معمولا دستوراتی مثل ساخت دیتابیس رو اراپه دهندگان نمیبندند (هاستینگ های معتبر) اما اگه شما نمیتونید این کار رو انجام بدید به خاطر سطح دسترسی کاربری هست که توسط اون به دیتابیس متصل شدید که این سطح دسترسی در زمان کاربر تعیین میشه.من که تست کردم بسته بود.
شاید اونایی که تعداد دیتابیسشون نامحدود هست نمیبندن.
چون من فکر میکنم برای اینکه تعداد دیتابیس محدود باشه این کار رو میکنن. وگرنه نمیشه که طرف زرت و زرت با PHP دیتابیس درست میکنه خب. ولی وقتی فقط از طریق کنترل پنل هاست بتونید این کار رو بکنید میتونه حداکثر تعدادش رو محدود کنه.
در کنترل پنل هم موقعی که میخواید دیتابیس درست کنید، نام کاربری خودتون رو بعنوان پیشوند داره که با یک آندرلاین به اسم دیتابیس مورد نظر شما متصل میشه.
مثلا اگر نام کاربری اکانت شما majid باشه شما میتونید دیتابیس هایی درست کنید مثل majid_db1 و نه غیر از این. پس اگر ما بخوایم در PHP همینطوری با هر اسمی دیتابیس درست کنیم یحتمل نمیشه. البته من امتحان کردم و نام کاربری رو به همین صورت به اسم دیتابیس اضافه کردم تا ببینم در این صورت PHP میتونه دیتابیس درست کنه یا نه، که بازم نتونست.

پس هاستینگ ها و پلان هایی که محدودیت تعداد دیتابیس دارن حتما کاربر دیتابیسی که در کنترل پنل ایجاد میکنید نمیتونه مجوز ایجاد دیتابیس رو داشته باشه. مخصوصا اینطور هست تا بتونن تعداد دیتابیس هر اکانت رو کنترل کنن.
اونایی هم که محدودیت دارن ممکنه باز اینطور باشه یا نباشه. بهرحال هاست با هاست و کانفیگ با کانفیگ هم میتونه تفاوت بکنه.