Attention : vous consultez actuellement la documentation dédiée aux versions 1.x de Moodle. La documentation pour les versions 2.x de Moodle est consultable ici : Convertir votre base MySQL en UTF-8, celle pour les versions 3.x de Moodle est consultable ici : Convertir votre base MySQL en UTF-8 et celle pour Moodle 4.x est consultable là : Convertir votre base MySQL en UTF-8.

« Convertir votre base MySQL en UTF-8 » : différence entre les versions

De MoodleDocs
Aller à :navigation, rechercher
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
{{En cours de traduction}}
{{En cours de traduction}}
Ce document explique comment convertir votre base de données interne de Moodle de l'encodage latin1 vers UTF8. Moodle nécessite maintenant l'encodage [[:Catégorie:UTF-8|UTF8]] et ne se mettra pas à jour si ce n'est pas le cas. Les instructions ci-dessous vous permettront de résoudre un tel problème éventuel.


This document looks at how to convert your MySQL database from the latin1 charset to UTF8. Moodle requires that your Database is now UTF8 and will not upgrade if your database is not, following the steps below will guide you in converting your database so that things once again work.
== Pourquoi ?==


==Why?==
Vous risquez de voir les erreurs suivantes lorsque vous [[Mise à jour|mettez à jour]] Moodle.
 
You may see the following error when upgrading your Moodle.


''It is required that you store all your data in Unicode format (UTF-8). New installations must be performed into databases that have their default character set as Unicode. If you are upgrading, you should perform the UTF-8 migration process (see the Admin page).''
''It is required that you store all your data in Unicode format (UTF-8). New installations must be performed into databases that have their default character set as Unicode. If you are upgrading, you should perform the UTF-8 migration process (see the Admin page).''
Ligne 12 : Ligne 11 :
Moodle requires UTF8 in order to provide better multilingual support and has done since Moodle 1.8. However the UTF8 check during install and upgrade has only been implemented recently and there are likely going to many users who find they are unable to upgrade because they did no set their database up correctly when they first installed Moodle, or for those who have been running Moodle from before 1.8 because they simply havn't already converted their database.
Moodle requires UTF8 in order to provide better multilingual support and has done since Moodle 1.8. However the UTF8 check during install and upgrade has only been implemented recently and there are likely going to many users who find they are unable to upgrade because they did no set their database up correctly when they first installed Moodle, or for those who have been running Moodle from before 1.8 because they simply havn't already converted their database.


For more information about UTF8 have a look at the doc on [https://docs.moodle.org/en/Unicode unicode].
For more information about UTF8 have a look at the doc on [[Unicode]].


==Linux & Mac==
==Linux & Mac==
Ligne 25 : Ligne 24 :
</code>
</code>


===Explained===
ou si vous préférez utiliser sed :
<code bash>
#  $1-dbusername $2-password $3-dbname
mysqldump -u$1 -p$2 -c -e --default-character-set=utf8 --single-transaction --skip-set-charset --add-drop-database -B $3 > dump.sql
sed 's/DEFAULT CHARACTER SET latin1/DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci/' <dump.sql | sed 's/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/' >dump-fixed.sql
mysql -u$1 -p$2 < dump-fixed.sql
</code>
 
=== Explications ===
The following steps will guide you in creating a database dumb, editing the database dump so that the correct charset and collation are used and then restoring the new database.
The following steps will guide you in creating a database dumb, editing the database dump so that the correct charset and collation are used and then restoring the new database.


Ligne 81 : Ligne 88 :
There are likely several additional arguments for mysqldump you will need to specify including setting the file for output using -r to avoid newline issues.
There are likely several additional arguments for mysqldump you will need to specify including setting the file for output using -r to avoid newline issues.


==More information==
== Voir aussi ==
* [https://docs.moodle.org/en/Unicode Moodle docs: Unicode]
* [[Unicode]]
 
[[Catégorie:UTF-8]]


[[en:Converting your MySQL database to UTF8]]
[[en:Converting your MySQL database to UTF8]]

Version du 19 novembre 2010 à 10:51

Remarque : la traduction de cet article n'est pas terminée. N'hésitez pas à traduire tout ou partie de cette page ou à la compléter. Vous pouvez aussi utiliser la page de discussion pour vos recommandations et suggestions d'améliorations.


Ce document explique comment convertir votre base de données interne de Moodle de l'encodage latin1 vers UTF8. Moodle nécessite maintenant l'encodage UTF8 et ne se mettra pas à jour si ce n'est pas le cas. Les instructions ci-dessous vous permettront de résoudre un tel problème éventuel.

Pourquoi ?

Vous risquez de voir les erreurs suivantes lorsque vous mettez à jour Moodle.

It is required that you store all your data in Unicode format (UTF-8). New installations must be performed into databases that have their default character set as Unicode. If you are upgrading, you should perform the UTF-8 migration process (see the Admin page).


Moodle requires UTF8 in order to provide better multilingual support and has done since Moodle 1.8. However the UTF8 check during install and upgrade has only been implemented recently and there are likely going to many users who find they are unable to upgrade because they did no set their database up correctly when they first installed Moodle, or for those who have been running Moodle from before 1.8 because they simply havn't already converted their database.

For more information about UTF8 have a look at the doc on Unicode.

Linux & Mac

mysqldump -uusername -ppassword -c -e --default-character-set=utf8 --single-transaction --skip-set-charset --add-drop-database -B dbname > dump.sql cp dump.sql dump-fixed.sql vim dump-fixed.sql

%s/DEFAULT CHARACTER SET latin1/DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci/
%s/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/
wq

mysql -uusername -ppassword < dump-fixed.sql

ou si vous préférez utiliser sed :

  1. $1-dbusername $2-password $3-dbname

mysqldump -u$1 -p$2 -c -e --default-character-set=utf8 --single-transaction --skip-set-charset --add-drop-database -B $3 > dump.sql sed 's/DEFAULT CHARACTER SET latin1/DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci/' <dump.sql | sed 's/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/' >dump-fixed.sql mysql -u$1 -p$2 < dump-fixed.sql

Explications

The following steps will guide you in creating a database dumb, editing the database dump so that the correct charset and collation are used and then restoring the new database.

To start please open a new terminal and move to a temp directory.

mysqldump -uusername -ppassword -c -e --default-character-set=utf8 --single-transaction --skip-set-charset --add-drop-database -B dbname > dump.sql

The first step is of course to dump out the database and of course we will use mysqldump for this. We do however need to set several arguments in order to clean up the charsets and provide a dump that is not going to cause you any problems if you are moving this database to a different database server or find yourself having to restore on a reverted system.

username
The username to access your database.
password
The password for the above user.
-c
Complete inserts for better compatibility.
-e
Extended inserts for better performance.
--default-character-set=utf8
To set the default character set.
--single-transaction
To reduce our workload if anything goes wrong.
--skip-set-charset
Obviously not wanted or needed as we are changing it anyway.
--add-drop-database
Required so we can restore over the top of our existing database.
-B
We use this option so that our dump will contain drop table and create table syntax (which we will change the syntax for).
dbname
The name of the database to convert.

When you run this command a database dump will be generated into dump.sql

cp dump.sql dump-fixed.sql Next step is to copy dump.sql to dump-fixed.sql.

We will make the desired changes within dump-fixed.sql and we will keep dump.sql as it is as a backup just in case.

vim dump-fixed.sql

%s/DEFAULT CHARACTER SET latin1/DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci/
%s/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/
wq

Now we need to edit the dump and correct the incorrect charsets that have been used. I have chosen to do this with VIM however you can use any search+replace editor or program. ( I choose VIM for this only because every linux user is/should be familiar with it).

First we open the file using VIM, and then run the three commands.

The first command replaces all instances of DEFAULT CHARACTER SET latin1 with DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci. This is used to fix up the database's default charset and collation.

The second command replaces all instances of DEFAULT CHARSET=latin1 with DEFAULT CHARSET=utf8. This converts all tables from using latin1 to using UTF8.

The third command simply saves it and exits.

mysql -uusername -ppassword < dump-fixed.sql Now that we've made the required changes we simply need to restore the database over top of the existing database. We can do this by running the above command.

Windows

Could someone who is familiar with Windows please convert the above into something that will work on Windows? There are likely several additional arguments for mysqldump you will need to specify including setting the file for output using -r to avoid newline issues.

Voir aussi