you are in: codestackercodes [RSS] → tag: mysql [RSS]

backup all mysql database Delicious Email

show/hide lines
   1  mysqldump -u username -ppassword –all-databases > dump.sql
created by leozera — 22 August 2009 — get a short url — tags: backup mysql shell embed

count domain users using sql Delicious Email

from: http://www.mendable.com/sql-trick-where-are-your-users-from/

show/hide lines
   1  SELECT COUNT(*) AS Total, SUBSTRING_INDEX(email, '@', -1) AS Domain FROM users 
   2  GROUP BY SUBSTRING_INDEX(email, '@', -1) ORDER BY COUNT(*) DESC LIMIT 10;
created by leozera — 12 July 2009 — get a short url — tags: mysql sql embed

mysql dump Delicious Email

import/export a database via terminal

show/hide lines
   1  Export
   2  mysqldump -u username -p -h mysqlhostname databasename > databasedump.sql
   3  
   4  Import
   5  mysql -u username -p -h mysqlhostname databasename < databasedump.sql
created by leozera — 17 October 2008 — get a short url — tags: mysql embed

stop/start mysql via terminal Delicious Email

show/hide lines
   1  sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist
   2  sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plis
created by leozera — 17 October 2008 — get a short url — tags: mac mysql terminal embed

php script for mysql backup Delicious Email

show/hide lines
   1  <?php
   2  
   3  function backup_tables($host,$user,$pass,$name,$tables = '*'){
   4  	
   5  	$link = mysql_connect($host,$user,$pass);
   6  	mysql_select_db($name,$link);
   7  	
   8  	//get all of the tables
   9  	if($tables == '*')
  10  	{
  11  		$tables = array();
  12  		$result = mysql_query('SHOW TABLES');
  13  		while($row = mysql_fetch_row($result))
  14  		{
  15  			$tables[] = $row[0];
  16  		}
  17  	}
  18  	else
  19  	{
  20  		$tables = is_array($tables) ? $tables : explode(',',$tables);
  21  	}
  22  	
  23  	//cycle through
  24  	foreach($tables as $table)
  25  	{
  26  		$result = mysql_query('SELECT * FROM '.$table);
  27  		$num_fields = mysql_num_fields($result);
  28  		for ($i = 0; $i < $num_fields; $i++) 
  29  		{
  30  			$return.= 'DROP TABLE '.$table.';';
  31  			
  32  			$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
  33  			$return.= "\n\n".$row2[1].";\n\n";
  34  			
  35  			while($row = mysql_fetch_row($result))
  36  			{
  37  				$return.= 'INSERT INTO '.$table.' VALUES(';
  38  				for($j=0; $j<$num_fields; $j++) 
  39  				{
  40  					$row[$j] = addslashes($row[$j]);
  41  					$row[$j] = ereg_replace("\n","\\n",$row[$j]);
  42  					if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
  43  					if ($j<($num_fields-1)) { $return.= ','; }
  44  				}
  45  				$return.= ");\n";
  46  			}
  47  		}
  48  		$return.="\n\n\n";
  49  	}
  50  	
  51  	//save file
  52  	$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
  53  	fwrite($handle,$return);
  54  	fclose($handle);
  55  }
  56  
  57  backup_tables('localhost','username','password','blog');
  58  
  59  ?>
created by leozera — 01 September 2008 — get a short url — tags: backup mysql php embed

Drop all tables in a MySQL Database Delicious Email

run in shell:

show/hide lines
   1  echo DROP TABLE `echo 'SHOW TABLES;' | mysql -u USER -p DATABASE | tail -n +2 | sed -e 's/$/,/'` | sed -e 's/,$/;/'
created by leozera — 31 July 2008 — get a short url — tags: mysql embed

mysql one line all database backup Delicious Email

show/hide lines
   1  mysqldump --al-databases -u -p > backup.sql
created by anonymous — 04 July 2008 — get a short url — tags: backup mysql shell embed

resets autoincrement (mysql) Delicious Email

show/hide lines
   1  ALTER TABLE tablename AUTO_INCREMENT = 1
created by leozera — 02 July 2008 — get a short url — tags: mysql sql embed