发表时间:2008-04-01
最后修改:2011-01-19
用户命令
date加减语法
装confluence,使用mysql乱码,查出是mysql编码设置问题:
最终解决方案:
mysql UTF8设置:
vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8 (数据库缺省以utf8存储)
init_connect='SET NAMES utf8' (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)
[client]
default-character-set=utf8 (客户端缺省以utf8存储)
重启mysql
登陆mysql,查看:
show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
2010年1月18日 更新:
导入导出数据:
mysqldump db > table.sql
可选参数:
-n : --no-create-db
-t : --no-create-table
-d : --no-data
数据导出:mysqldump --default-character-set=gbk db > db.sql
数据导入:mysql --default-character-set=gbk db < db.sql
执行mysqldump sql:mysqldump -w "fmodify_date > '2010-4-10'"
执行指令后导出txt:mysql -uroot -p123 db_fund -e "select * from t_fund_bind limit 0,10" > bind.sql
修改字符集:
alter database da_name default character set 'gbk'
set names 'gbk'
set names gbk告诉mysql,接下来的数据将以该编码方式传输,等同于:
SET character_set_client = gbk;
SET character_set_results = gbk;
SET character_set_connection = gbk;
通过status指令查看当前的编码:
mysql> status;
Server characterset: latin1
Db characterset: gbk
Client characterset: gbk
Conn. characterset: gbk
7)导入我们转换成gbk后的文件
mysql> source base_user_gbk.sql;
用户命令:
mysql> CREATE USER yy IDENTIFIED BY '123';
yy表示你要建立的用户名,后面的123表示密码
上面建立的用户可以在任何地方登陆。
如果要限制在固定地址登陆,比如localhost 登陆:
mysql> CREATE USER yy@localhost IDENTIFIED BY '123';
mysql> GRANT ALL PRIVILEGES ON *.* TO user@localhost
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码"
修改密码:
mysql> grant all privileges on pureftpd.* to koko@localhost identified by 'mimi';
flush:
mysql> flush privileges;
查看用户信息:
mysql> select host,user from mysql.user;
sql inject:
1.使用PrepareStatement+bind-variable.
2.使用程序转换特殊字符,如mysql c api: mysql_real_escape_string()
3.自行编写函数进行校验
tomcat encode:http://wiki.apache.org/tomcat/FAQ/CharacterEncoding
jetty encode:http://docs.codehaus.org/display/JETTY/International+Characters+and+Character+Encodings
date加减语法
date + INTERVAL expr unit
select current_date() - INTERVAL 1 DAY;
where fdate = current_date()- INTERVAL 2 DAY
mysql insert or update的替代做法
insert t_app_pv (f_app_id,f_pv) values (?,?) on duplicate key update f_pv=?
insert select 语法
SELECT * FROM t_cft_fund_log t order by f_operation_date limit 0,10 ;
SELECT * FROM t_cft_fund_log_history order by f_operation_date desc limit 0,10;
insert into t_cft_fund_log_history select * from t_cft_fund_log where f_operation_date < '2010-10-01';
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[(col_name
,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE col_name
=expr
, ... ]
With INSERT ... SELECT
, you can quickly insert many rows into a table from one or many tables. For example:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
mysqldump导出数据不带时区信息
http://blog.kylinhome.net/2010/09/72
KylinHuang Post in 技术,Tags: MySQL, 时间
今天备份数据库的时候无意中看了一下备份出来的数据。
发现所有timestamp字段都不带时区信息,因为我在东8区,导出的数据中所有时间都提早了8个小时
后来发现mysqldump的选项--tz-utc
--tz-utc
Add SET TIME_ZONE='+00:00'
to the dump file so that TIMESTAMP
columns can be dumped and reloaded between servers in different time zones. Without this option, TIMESTAMP
columns are dumped and reloaded in the time zones local to the source and destination servers, which can cause the values to change. --tz-utc
also protects against changes due to daylight saving time. --tz-utc
is enabled by default. To disable it, use --skip-tz-utc
. This option was added in MySQL 5.0.15.
用了--skip-tz-utc
果然就OK了。
不过其实不用管这个啦,因为用mysql < … 的时候会自动再加上时区信息,因为你可以看到导出的sql文件中有
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
...
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;