mysql -h127.0.0.1 -uzabbix -pzabbix --database=zabbix -B -N -e "SHOW TABLES" | grep -v "^history$\|^history_uint$\|^trends$\|^trends_uint$" | awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin; SET foreign_key_checks = 1; "}'
mysql -h127.0.0.1 -uzabbix -pzabbix --database=zabbix -B -N -e "SHOW TABLES" | grep -v "^history$\|^history_uint$\|^trends$\|^trends_uint$" | awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin; SET foreign_key_checks = 1; "}' | wc -l
Check characterset and collate for current database:
use zabbix; SELECT @@character_set_database, @@collation_database;
Print the queries to update database:
SELECT DISTINCT concat('ALTER DATABASE `', TABLE_SCHEMA, '` CHARACTER SET utf8 COLLATE utf8_bin;') from information_schema.tables where TABLE_SCHEMA like 'zabbix';
Print the queries to update tables:
SELECT concat('ALTER TABLE `', TABLE_SCHEMA, '`.`', table_name, '` CHARACTER SET utf8 COLLATE utf8_bin') from information_schema.tables where TABLE_SCHEMA like 'zabbix';
Update columns:
SELECT concat('ALTER TABLE `', t1.TABLE_SCHEMA, '`.`', t1.table_name, '` MODIFY `', t1.column_name, '` ', t1.data_type , '(' , t1.CHARACTER_MAXIMUM_LENGTH , ')' , ' CHARACTER SET utf8 COLLATE utf8_bin;')
from information_schema.columns t1
where t1.TABLE_SCHEMA like 'zabbix_database_name' and t1.COLLATION_NAME = 'utf8_general_ci';
Show all columns which has some textual values:
select COLUMN_NAME,COLLATION_NAME,TABLE_NAME from information_schema.columns where TABLE_SCHEMA like 'zabbix' and COLLATION_NAME is not null;
# authorize inside database
mysql zabbix
# this should report empty string
SELECT * FROM information_schema.TABLES WHERE table_schema = 'zabbix' AND table_collation != 'utf8_bin';
# this should report content
SELECT * FROM information_schema.TABLES WHERE table_schema = 'zabbix' AND table_collation = 'utf8_bin';
exit
mysql zabbix <<< 'show tables;'
Replace
^ with:
"ALTER TABLE "
Replace
$ with:
" CHARACTER SET utf8 COLLATE utf8_bin;"
List all tables, take one by one anc set "charset utf8 collate utf8_bin" for each of them:
mysql --database=zabbix -u'root' -p'rootPassword' -B -N -e "SHOW TABLES" | grep -v "^history$\|^history_uint$\|^history_str$\|^history_log$\|^history_text$\|^trends$\|^trends_uint$\|^events$" | awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin; SET foreign_key_checks = 1; "}' | mysql -u'root' -p'rootPassword' --database=zabbix
mysql --database=zabbix -B -N -e "SHOW TABLES" | grep -v "^history$\|^history_uint$\|^trends$\|^trends_uint$" | wc -l
No comments:
Post a Comment