Conversione dei database mysql in UTF-8 dopo upgrade 4.0-4.1
Dopo aver eseguito l'upgrade con
sudo apt-get install mysql-server-4.1 mysql-client-4.1
e aver lanciato lo script:
/usr/bin/mysql_fix_privilege_tables --verbose --password=thepass
se si entra con PhpMyAdmin, si vedrà nella pagina pricipale, che il database di default è in formato UTF-8 (double-byte):
Set di caratteri MySQL: UTF-8 Unicode (utf8) collazione della connessione di MySQL: utf8_general_ci
Però se si visalizzando i singoli database, essi sono ancora in utf8 (alias iso-8859-1):
Tabella RecordTip Tipo Collation Dimensione In eccesso Campi_Modulo 102 MyISAM utf8_swedish_ci 11,3 KB - Consumatore 3 MyISAM utf8_swedish_ci 2,1 KB - Documenti 4 MyISAM utf8_swedish_ci 2,1 KB -
Questo potrebbe provocare problemi con dati testo con lettere accentate.
Occorre convertire i dati e i database in UTF8.
Metodo 1
Chiudere tutte le connessioni al database (fermare apache, apache-ssl e apache2, o altri applicativi he usino database) Fare una copia del database per sicurezza:
mysqldump --all-databases > mysql_backup.original
Esportare il database SENZA definizioni di charset per conservare tutti i caratteri e non convertirli:
mysqldump --all-databases --default-character-set=utf8 --skip-set-charset > mysql_backup
Verificare che sia un utf8:
$ file mysql_backup mysql_backup: ISO-8859 text, with very long lines
Aprirlo con vim e sostituire "utf8" con "utf8"
vi mysql_backup :%s/utf8/utf8/g
Per chiarezza:
mysql_backup prima della sostituzione:
-- MySQL dump 10.9
--
-- Host: localhost Database:
-- ------------------------------------------------------
-- Server version 4.1.11-Debian_4sarge3-log
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `citifin`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `citifin` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `citifin`;
--
-- Table structure for table `Campi_Modulo`
--
DROP TABLE IF EXISTS `Campi_Modulo`;
CREATE TABLE `Campi_Modulo` (
`campo_modulo` varchar(50) NOT NULL default '',
`x` int(11) default NULL,
`y` int(11) default NULL,
`visibile` enum('y','n') NOT NULL default 'n',
`as_posizione` bigint(20) default NULL,
...
`Tabella_Valore` varchar(64) default NULL,
`Tabella_Descrizione` varchar(64) default NULL,
PRIMARY KEY (`campo_modulo`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `Campi_Modulo`
--
...
mysql_backup dopo la sostituzione:
-- MySQL dump 10.9
--
-- Host: localhost Database:
-- ------------------------------------------------------
-- Server version 4.1.11-Debian_4sarge3-log
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `citifin`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `citifin` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `citifin`;
--
-- Table structure for table `Campi_Modulo`
--
DROP TABLE IF EXISTS `Campi_Modulo`;
CREATE TABLE `Campi_Modulo` (
`campo_modulo` varchar(50) NOT NULL default '',
`x` int(11) default NULL,
`y` int(11) default NULL,
`visibile` enum('y','n') NOT NULL default 'n',
`as_posizione` bigint(20) default NULL,
...
`Tabella_Nome` varchar(64) default NULL,
`Tabella_Valore` varchar(64) default NULL,
`Tabella_Descrizione` varchar(64) default NULL,
PRIMARY KEY (`campo_modulo`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Dumping data for table `Campi_Modulo`
--
...
Salvare ed uscire da vi.
Convertire il file in utf8:
iconv -f iso-8859-1 -t utf8 -o mysql_backup.utf8 mysql_backup
Verificare che il file sia UTF8:
$ file mysql_backup.utf8 mysql_backup.utf8: UTF-8 Unicode text, with very long lines
Ora si importerà il dump del database con queste correzioni. I database verranno TUTTI CANCELLATI, ricreati con le giuste opzioni e riempiti con i dati giusti:
mysql --default-character-set=utf8 < mysql_backup.utf8
Ora in PhpMyAdmin tutto sarà in UTF8.
Metodo 2
MySQL has a charset= option at the end of the CREATE TABLE, so we ran through all our tables doing
alter table charset=utf8.
This worked, and we were pleased as punch.
But then examining a particular table, I noticed:
CREATE TABLE ( id integer not null default 0, name varchar(32) character set utf8 default NULL, ) charset=utf8;
Every column that actually stored text specified the old utf8? encoding.
It turns out in MySQL, when it comes time to change your character set, theres a special ALTER command that will also change each of the columns:
alter table convert to character set utf8;
This changes all the text columns to the new character set as well.
Riferimenti
Recenti:
- TIP Convert latin1 to UTF-8 in MySQL - Gentoo Linux Wiki
- Changing MySQL default character sets to UTF-8 : DerekAllard.com
Vecchi:
- Text* Snippets: Convert a db to UTF8 after upgrading to MySQL 4.1 (mysql)
- Text* Snippets: Convert table character set to UTF8 (mysql)
- The O'Reilly Network: Turning MySQL data in utf8 to utf8 utf-8
- Sébastien Erard » WordPress MySQL database : conversion to UTF-8:
- Wikipedia, the free encyclopedia: ISO/IEC 8859-1