手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜Qcloud , 注册 | 登陆
浏览模式: 标准 | 列表分类:DataBase

将原数据库从latin1改为utf8

 原来的数据库是N年前的,默认字符集是latin1,但是有一些旧代码中,连接参数写的是UTF8,于是就这样。。。DB字符集为latin1,连接UTF8,存到数据库也是UTF8,wj但查看的时候显示是???

为了标准统一,于是使用mysqldump -uxx -p database a b c d e > dump.sql,将部分表先导出来

然后修改导出sql的charset=latin1;改为charset=utf8;

 

至此,操作完成。简单粗暴 

 

-------EOF--

deeka说,如果有200G呢。目前我是停机处理的。因为要处理的数据也不是很多。如果不是因为升级换库的时候出现不能访问我也不会这么做。

但好象也没有特别好的方案。因为直接alter的时候也是各种不正常。才dump的

Error connecting remote MySQL server [ERROR 1042 (HY000): Can't get hostname for your address]

设置了远程数据库允许IP连接,也设置了bind ip为0.0.0.0 ,但有时候能连,有时候不能连接,而且还设置了是IP连接,报标题所在的错误,即 [ERROR 1042 (HY000): Can't get hostname for your address]。

开始就在想,是不是skip-name-resolve的问题,但因为我不是用host连接的,也不是内部的域名解析的问题。所以开始没注意,但google了之后,还是决定加了skipnameresolve

http://serverfault.com/questions/174242/error-connecting-remote-mysql-server-error-1042-hy000-cant-get-hostname-for
  1. ave MySQL 5.5 Server setup on a windows machine. I am able to connect to the server from console / app running on the same machine but not from a remote machine. While connecting using the command  
  2.   
  3. mysql -h xx.xx.xx.xx --port=3306 -u root -p  
  4. I get error as:  
  5.   
  6. ERROR 1042 (HY000): Can't get hostname for your address  
  7. Have tried putting entry of client ip in server's etc\hosts file as  
  8.   
  9. <client-ip>  <client-hostname>  
所有的回答都指向了skip-name-resolve
XML/HTML代码
  1. I believe this is to do with the fact that MySQL tries to establish the DNS name associated with your IP address on connect. See here for more information at the MySQL site.  
  2.   
  3. You have two options:  
  4.   
  5. 1) Fix the connecting machine's reverse DNS. Since you've already added the machine to the hosts file, this might be unnecessary. You should also issue a FLUSH HOSTS statement on the MySQL server. See the same link above for more information about this.  
  6.   
  7. 2) Run MySQL with the '--skip-name-resolve' option. However, if you do this, you won't be able to use DNS names in GRANT statements. Instead you'll be restricted to using IP addresses.  
  8.   
  9. 2.1) or put in my.ini :  
  10.   
  11. [mysqld]  
  12. skip-name-resolve  
  13. I'd recommend (1) if you can.  
  14.   
  15. Hope this helps.  
然后也确实解决了这个问题。记录一下
 
 

 
 

Tags: mysql

What's the difference between utf8_general_ci and utf8_unicode_ci

Copy from http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci

看到本文其实是源于Yii2 的migrate中的init,它其中就有一段:

PHP代码
  1. if ($this->db->driverName === 'mysql') {  
  2.     // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci  
  3.     $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';  
  4. }  

于是我才发现了上面的URL,然后打开看了下,发现是有人在提问:Between utf8_general_ci and utf8_unicode_ci, are there any differences in terms of performance?

然后有一个554个赞的评论,这样写:

There are at least two important differences:

  • Accuracy of sorting

    utf8_unicode_ci is based on the Unicode standard for sorting, and sorts accurately in a very wide range of languages.

    utf8_general_ci comes close to correct Unicode sorting in many common languages, but has a number of limitations: in some languages, it won't sort correctly at all. In others, it will merely have some quirks.

  • Performance

    utf8_general_ci is faster at comparisons and sorting, because it takes a bunch of performance-related shortcuts.

    utf8_unicode_ci uses a much more complex comparison algorithm which aims for correct sorting according in a very wide range of languages. This makes it slower to sort and compare large numbers of fields.

Unicode defines complex sets of rules for how characters should be sorted. These rules need to take into account language-specific conventions; not everybody sorts their characters in what we would call 'alphabetical order'.

  • As far as Latin (ie "European") languages go, there is not much difference between the Unicode sorting and the simplified utf8_general_ci sorting in MySQL, but there are still a few differences:

    For examples, the Unicode collation sorts "ß" like "ss", and "Œ" like "OE" as people using those characters would normally want, whereas utf8_general_ci sorts them as single characters (presumably like "s" and "e" respectively).

  • In non-latin languages, such as Asian languages or languages with different alphabets, there may be a lot more differences between Unicode sorting and the simplified utf8_general_ci sorting. The suitability of utf8_general_ci will depend heavily on the language used. For some languages, it'll be quite inadequate.

Some Unicode characters are defined as ignorable, which means they shouldn't count toward the sort order and the comparison should move on to the next character instead. utf8_unicode_ci handles these properly.

What should you use?

There is almost never any reason to use utf_general_ci anymore, as we have left behind the point where CPU speed is low enough that the performance difference would be important. Your database will almost certainly be limited by quite other bottlenecks than this nowadays. The difference in performance is only going to be measurable in extremely specialised situations, and if that's you, you'd already know about it. If you're experiencing slow sorting, in almost all cases it'll be an issue with your indexes/query plan. Changing your collation function should not be high on the list of things to troubleshoot.

When I originally wrote this answer (over 4 years ago) I said that if you wanted, you could use utf8_general_ci most of the time, and only use utf8_unicode_ci when sorting was going to be important enough to justify the performance cost. However, the performance cost is no longer really relevant (and it may not have been back then, either). It's more important to sort properly in whichever language your users are using.

One other thing I'll add is that even if you know your application only supports the English language, it may still need to deal with people's names, which can often contain characters used in other languages in which it is just as important to sort correctly. Using the Unicode rules for everything helps add peace of mind that the very smart Unicode people have worked very hard to make sorting work properly.

还有一些代码,不过我就不贴了,建议查看原网页,别说没地址。。

 

解决:Data truncated for column 'fieldname' at row 1

 在修改数据库结构的时候,突然间报这个错:Data truncated for column 'xxxxx' at row 1,xxxxx是数据库的字段名。

检查了一下,原来刚才在修改字段的时候,加了一个允许 null,然后默认的值都变成null,可是后来我又修改表结构为not null default '';所以,报会这个错

到数据库里:update xxxx set xxx = '';,再修改表结构,一切Over。

MYSQL 存储过程

 说实话,关于存储过程的博客还真的不多,有几个是值得看一下的

1、官方;http://dev.mysql.com/doc/refman/5.1/zh/stored-procedures.html

2、http://www.netingcn.com/tag/%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B

3、http://blog.why100000.com/?p=711

也发现,如果不做复杂查询,存储过程对我来说几乎没有,本来是想解决查找GEO相关的信息的,但发现这样的SQL:

SQL代码
  1.  SELECT userid,lat,lng,gender,  
  2.   ( 6371 * acos( cos( radians(31.000700) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(120.000099) ) + sin( radians(31.000700) ) * sin( radians( lat ) ) ) ) AS distance  
  3. FROM `user_geo` WHERE last_activity_time > '2013-03-11 00:00:00'    
  4. ORDER BY distance ASC limit 100  

这其中的复杂度就在于distance每次都要计算,所以我尝试换成了存储过程:

SQL代码
  1. DROP PROCEDURE IF EXISTS search_around_user;  
  2. DELIMITER //  
  3. CREATE PROCEDURE search_around_user  
  4. (  
  5.     s_lat float(10,6),  
  6.     s_lng float(10,6),  
  7.     s_last_act datetime,  
  8.     s_gender tinyint,  
  9.     s_number tinyint,  
  10.     s_page tinyint  
  11. )  
  12. LABEL_PROC:  
  13. BEGIN  
  14.     if s_number <= 1 then  
  15.         set s_number = 20;  
  16.     end if;  
  17.     if s_page <= 0 then  
  18.         set s_page = 0;  
  19.     end if;  
  20.     if s_gender <= 0 then  
  21.         set @genderQuery = "";  
  22.     else  
  23.         set @genderQuery = concat(" and gender = " , s_gender , " ");  
  24.     end if;  
  25.     set @limitQuery = concat("LIMIT " , s_page * s_number , " , " , s_number , " ");  
  26.   
  27.     set @strsql =  CONCAT("select userid, ",  
  28.         "( 6371 * acos( cos( radians(",s_lat,") ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( ",s_lng," ) ) ",  
  29.         "+ sin( radians( ",s_lat," ) ) * sin( radians( lat ) ) ) ) AS distance ",  
  30.         " FROM user_geo where last_activity_time >= '", s_last_act , "' " , @genderQuery ,  " ORDER BY distance " , @limitQuery) ;  
  31.   
  32.     prepare stmtsql from @strsql;  
  33.     execute stmtsql;  
  34.       
  35.   
  36. END LABEL_PROC;  
  37. //  
  38. DELIMITER ;  

然后再次调用:

SQL代码
  1. call search_around_user(31.000700,120.000099,'2013-03-11 00:00:00',0,20,0)  

所耗费的时间和上述直接写SQL的时间是几乎一样的。想来,这也是因为distance的计算不能被优化而导致的。。。于是乎,放弃用存储过程

 

 

Tags: mysql, 存储过程

Records:15712345678910»