Task MySQL

Administración

Script para listas las tablas fragmentadas y correr un optimize

#!/bin/sh echo -n "MySQL username: " ; read username echo -n "MySQL password: " ; stty -echo ; read password ; stty echo ; echo mysql -u $username -p"$password" -NBe "SHOW DATABASES;" | grep -v 'lost+found' | while read database ; do mysql -u $username -p"$password" -NBe "SHOW TABLE STATUS;" $database | while read name engine version rowformat rows avgrowlength datalength maxdatalength indexlength datafree autoincrement createtime updatetime checktime collation checksum createoptions comment ; do if [ "$datafree" -gt 0 ] ; then fragmentation=$(($datafree * 100 / $datalength)) echo "$database.$name is $fragmentation% fragmented." mysql -u "$username" -p"$password" -NBe "OPTIMIZE TABLE $name;" "$database" fi done done

Manejo de datos

MySQL y datos CSV

Importacion :

LOAD DATA LOCAL INFILE 'usuarios.csv' INTO TABLE usuarios FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' (nombre, pass, usuario);

Exportacion :

SELECT * FROM usuarios ORDER BY DATE DESC INTO OUTFILE "usuarios.csv" FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n';
mysql -u root -p cdr -B -e "select * from \`calldetails\` limit 2;" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g'

Unix Time

SELECT * , FROM_UNIXTIME( `dateTimeOrigination` ) AS Fecha FROM `calldetails` WHERE `finalCalledPartyNumber` = '7420'
SELECT * , FROM_UNIXTIME( `dateTimeOrigination` ) AS fecha FROM `calldetails` WHERE `finalCalledPartyNumber` = '' AND FROM_UNIXTIME( `dateTimeOrigination` ) LIKE '%' ORDER BY fecha

MySQL y Wordpress

UPDATE wp_posts SET guid = REPLACE(guid, 'http://dominioviejo.com','http://dominionuevo.com'); UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://dominioviejo.com', 'http://dominionuevo.com'); UPDATE wp_links SET link_url = REPLACE(link_url, 'http://dominioviejo.com', 'http://dominionuevo.com'); UPDATE wp_links SET link_image = REPLACE(link_image, 'http://dominioviejo.com', 'http://dominionuevo.com'); UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, 'http://dominioviejo.com', 'http://dominionuevo.com'); UPDATE wp_usermeta SET meta_value = REPLACE(meta_value, 'http://dominioviejo.com', 'http://dominionuevo.com'); /*UPDATE wp_options SET option_value = replace(option_value, 'http://dominioviejo.com', 'http://dominionuevo.com') WHERE option_name = 'home' OR option_name = 'siteurl' OR option_name = 'widget_text' OR option_name = 'dashboard_widget_options';*/ UPDATE wp_options SET option_value = REPLACE(option_value, 'http://dominioviejo.com', 'http://dominionuevo.com');