Herramientas de usuario

Herramientas del sitio


notas:bases_de_datos:mysql

Task MySQL

Administración

Scripts

Script para listar las tablas fragmentadas y correr un optimize

optimise.sh
#!/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');

Tamaños de bases y tablas

SELECT table_schema "DB Name",
        ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" 
FROM information_schema.tables 
GROUP BY table_schema; 
SELECT TABLE_NAME AS "Table",
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
FROM information_schema.TABLES
WHERE table_schema = "database_name"
ORDER BY (data_length + index_length) DESC;

Migrar una base a InnoDB

# mysql base < mig_innodb.sql
DROP PROCEDURE IF EXISTS convertToInnodb;
DELIMITER //
CREATE PROCEDURE convertToInnodb()
BEGIN
mainloop: LOOP
  SELECT TABLE_NAME INTO @convertTable FROM information_schema.TABLES
  WHERE `TABLE_SCHEMA` LIKE DATABASE()
  AND `ENGINE` LIKE 'MyISAM' ORDER BY TABLE_NAME LIMIT 1;
  IF @convertTable IS NULL THEN 
    LEAVE mainloop;
  END IF;
  SET @sqltext := CONCAT('ALTER TABLE `', DATABASE(), '`.`', @convertTable, '` ENGINE = INNODB');
  PREPARE convertTables FROM @sqltext;
  EXECUTE convertTables;
  DEALLOCATE PREPARE convertTables;
  SET @convertTable = NULL;
END LOOP mainloop;
 
END//
DELIMITER ;
 
CALL convertToInnodb();
DROP PROCEDURE IF EXISTS convertToInnodb;

Comprimir una tabla

ALTER TABLE syslog key_block_size=8 row_format=compressed;
notas/bases_de_datos/mysql.txt · Última modificación: 2021/01/21 17:47 por cayu