本文章来给大家介绍一下mysql5.6设置sql_mode方法详解,希望此方法对各位同学会有所帮助.
首先登录mysql终端,代码如下:mysql -u xxx -pxxxxx
查看当前mysql的sql_mode,代码如下:
- mysql>select@@sql_mode;
- +--------------------------------------------+
- |@@sql_mode|
- +--------------------------------------------+
- |STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION|
- +--------------------------------------------+
将mysql的sql_mode 设为 MYSQL40 也就是 NO_FIELD_OPTIONS,HIGH_NOT_PRECEDENCE,代码如下:
- mysql>SETsql_mode=`NO_FIELD_OPTIONS,HIGH_NOT_PRECEDENCE`;
- QueryOK,0rowsaffected(0.00sec)
- mysql>select@@sql_mode;
- +--------------------------------------+
- |@@sql_mode|
- +--------------------------------------+
- |NO_FIELD_OPTIONS,HIGH_NOT_PRECEDENCE|
- +--------------------------------------+
- 1rowinset(0.00sec)
需要注意的是上面的修改 sql_mode 只是会话级别的,只影响当前会话,退出终端后既失效.
下面这个才是设置全局级别的变量,适用于所有会话,但仅限于本次mysql进程,下次重启还要重新设置,代码如下:
set global sql_mode=`NO_FIELD_OPTIONS,HIGH_NOT_PRECEDENCE`
详细用法,代码如下:SET [GLOBAL|SESSION] sql_mode='modes'
每次启动自动设置sql_mode可以在启动脚本里面加上,代码如下:
mysql --sql-mode="NO_FIELD_OPTIONS,HIGH_NOT_PRECEDENCE"
sql_mode默认设置问题:
编译并配置完MySQL5.6版本,导入之前在MySQL5.5中正常运行的数据库却发生错误,原来从MySQL5.6版本开始,在执行mysql_install_db命令时,该命令一般在编译完mysql后运行并对数据库进行初始配置,会将默认的一个my.cnf文件写入到安装路径中(CentOS下默认安装路径为/usr/local/mysql/),其包含的信息如下:
- #Foradviceonhowtochangesettingspleasesee
- #http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
- [mysqld]
- #Removeleading#andsettotheamountofRAMforthemostimportantdata
- #cacheinMySQL.Startat70%oftotalRAMfordedicatedserver,else10%.
- #innodb_buffer_pool_size=128M
- #Removeleading#toturnonaveryimportantdataintegrityoption:logging
- #changestothebinarylogbetweenbackups.
- #log_bin
- #Thesearecommonlyset,removethe#andsetasrequired.
- #basedir=.....//phpfensi.com
- #datadir=.....
- #port=.....
- #server_id=.....
- #socket=.....
- #Removeleading#tosetoptionsmainlyusefulforreportingservers.
- #TheserverdefaultsarefasterfortransactionsandfastSELECTs.
- #Adjustsizesasneeded,experimenttofindtheoptimalvalues.
- #join_buffer_size=128M
- #sort_buffer_size=2M
- #read_rnd_buffer_size=2M
- #作者按:见这里的配置
- sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES