MYSQL
Pages: 1/5 First page 1 2 3 4 5 Next page Final page [ View by Articles | List ]
Feb 16
1. 类型声明
set 不需要声明类型,declare必须指定类型

2. 位置
set 位置可以任意, declare 必须在复合语句的开头,在任何其它语句之前

3.作用范围
DECLARE 定义的变量的作用范围是BEGIN … END块内,只能在块中使用。
SET 定义的变量用户变量,作用范围是会话/全局
    如SET @var=12的定义,则var的作用域为整个会话,为会话变量.
    如SET global var=12的定义,则var的作用域为全局,为全局变量.
Sep 22
查询列表中数据丢失,每页10条
以下query全部数据正常
SELECT * FROM TABLE ORDER BY CREATETIME DESC

以下分配查询数据丢失
SELECT * FROM TABLE ORDER BY CREATETIME DESC LIMIT 0,10;
SELECT * FROM TABLE ORDER BY CREATETIME DESC LIMIT 10,10;

怀疑是CREATETIME相同导致分页问题
查看数据库果然发现有3条数据CREATETIME相同

解决:
CREATETIME 类型到毫秒
或者
CREATETIME update时间为不同
或者
增加主键参与排序
ORDER BY CREATETIME,id DESC
Jul 1
举个例子来说:
有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等

现在有篇文章他既是 头条,又是热点,还是图文,

type中以 1,3,4的格式存储.

们我们如何用sql查找所有type中有4图文标准的文章呢??


这就要我们的find_in_set出马的时候到了.

以下为引用的内容:

select * from article where FIND_IN_SET('4',type)

----------------------------------------------------------
mysql手册中find_in_set函数的语法:

FIND_IN_SET(str,strlist)

假如字符串str 在由N 子链组成的字符串列表strlist 中,则返回值的范围在 1 到 N 之间。
一个字符串列表就是一个由一些被‘,’符号分开的子链组成的字符串。如果第一个参数是一个常数字符串,而第二个是type SET列,则   FIND_IN_SET() 函数被优化,使用比特计算。
如果str不在strlist 或strlist 为空字符串,则返回值为 0 。如任意一个参数为NULL,则返回值为 NULL。这个函数在第一个参数包含一个逗号(‘,’)时将无法正常运行。

mysql> SELECT FIND_IN_SET('b','a,b,c,d');

-> 2 因为b 在strlist集合中放在2的位置 从1开始
--------------------------------------------------------
select FIND_IN_SET('1','1'); 返回 就是1 这时候的strlist集合有点特殊 只有一个字符串 其实就是要求前一个字符串 一定要在后一个字符串集合中 才返回 大于0的数

select FIND_IN_SET('2','1,2');返回2
select FIND_IN_SET('6','1'); 返回0
----------------------------------------------------------

注意:
select * from treenodes where FIND_IN_SET(id,'1,2,3,4,5');
使用find_in_set函数一次返回多条记录

id 是一个表的字段 然后每条记录分别是id等于1,2,3,4,5的时候
有点类似in (集合)
select * from treenodes where id in (1,2,3,4,5);
Jun 15
May 14
1. 新增用户


2. 修改用户密码


3. 删除用户


4. 权限分配
    4.1. grant用法
           grant 权限 on 数据库.* to 用户名@'登录主机' identified by '密码'


4.2 例子


4.3 注意事项
grant 会覆盖用户的部分信息,跟insert 、update执行功能一样.
Mar 31
怎么样使用yum来安装mysql
linux下使用yum安装mysql,以及启动、登录和远程访问。
1、安装
查看有没有安装过:
          yum list installed mysql*
          rpm -qa | grep mysql*

查看有没有安装包:
          yum list mysql*

安装mysql客户端:
          yum install mysql

安装mysql 服务器端:
          yum install mysql-server

          yum install mysql-devel


2、启动&&停止

数据库字符集设置
          mysql配置文件/etc/my.cnf中加入default-character-set=utf8

启动mysql服务:
          service mysqld start或者/etc/init.d/mysqld start
开机启动:
          chkconfig -add mysqld,查看开机启动设置是否成功chkconfig --list | grep mysql*

          mysqld             0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
停止:

          service mysqld stop


3、登录
创建root管理员:
          mysqladmin -u root password 123456

登录:
          mysql -u root -p输入密码即可。
忘记密码:
          service mysqld stop

          mysqld_safe --user=root --skip-grant-tables

          mysql -u root

          use mysql

          update user set password=password("new_pass") where user="root";

          flush privileges;  

Feb 1
在制作一键安装程序时,需要将大量的初始化数据导入到数据表中,用到这个命令
MYSQL导入大数据命令load data infile load data local infile区别:
命令:load data local infile “filename.txt” into table tbl_name;
说明:MySQl的版本不得低于3.22.15,否则load data local不起作用。
如果你没有给出local,则服务器按如下方法对其进行定位:
1)如果你的filename为绝对路径,则服务器从根目录开始查找该文件.
2)如果你的filename为相对路径,则服务器从数据库的数据目录中开始查找该文件.
如果你给出了local,则文件将按以下方式进行定位:
1)如果你的filename为绝对路径,则客户机从根目录开始查找该文件.
2)如果你的filename为相对路径,则客户机从当前目录开始查找该文件.
另外要注意:
In MySQL 3.23.49 and MySQL 4.0.2, we added some new options to deal with possible security issues when it comes to LOAD DATA LOCAL.
There are two possible problems with supporting this command:
As the reading of the file is initiated from the server, one could theoretically create a patched MySQL server that could read any file on the client machine that the current user has read access to, when the client issues a query against the table.
In a web environment where the clients are connecting from a web server, a user could use LOAD DATA LOCAL to read any files that the web server process has read access to (assuming a user could run any command against the SQL server).
There are two separate fixes for this:
If you don't configure MySQL with --enable-local-infile, then LOAD DATA LOCAL will be disabled by all clients, unless one calls mysql_options(... MYSQL_OPT_LOCAL_INFILE, 0) in the client. See section 8.1.3.163 mysql_options().
For the mysql command-line client, LOAD DATA LOCAL can be enabled by specifying the option --local-infile[=1], or disabled with --local-infile=0.
By default, all MySQL clients and libraries are compiled with --enable-local-infile, to be compatible with MySQL 3.23.48 and before.
One can disable all LOAD DATA LOCAL commands in the MySQL server by starting mysqld with --local-infile=0.
In the case that LOAD DATA LOCAL INFILE is disabled in the server or the client, you will get the error message (1148):
The used command is not allowed with this MySQL version
Jan 26
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Red Hat Enterprise Linux 5服务器上mysql启动报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
原因1-启动命令错误:
我开始的时候直接输入命令:mysql start
正确的启动命令是:
/etc/rc.d/init.d/mysql start


原因2-配置文件错误:
检查etc下面的my.cnf如下内容:


原因3-启动文件错误:
需要修改MySQL启动脚本/etc/rc.d/init.d/mysql,
其中datadir=   ?     一行检查下!

原因4-前提是你在使用php连接时候报错!
在/etc/php.ini修改mysql.default_socket的值设置为:
mysql.default_socket=/var/lib/mysql/mysql.sock
回到终点设置个连接:ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
(在/etc/php.ini中mysql.default_socket这个文件中,关于mysql.default_socket的值的说明是这样的,
;Default socket name for local MySQL connects. If empty, uses the built-in MySQL defaults.
这个值一开始是空的,也就是说,如果我们不主动去修改的话,php将会使用内建在mysql中的默认值)

另一篇文章:
Mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost'问题的解决

这种问题需要强行重新修改密码,方法如下:


pkill -KILL -t pts/0 可将pts为0的**用户(之前运行mysqld_safe的用户窗口)强制踢出
正常启动 MySQL:/etc/init.d/mysql start   (service mysqld start)

注意:另外还遇到需要service mysql star才能启动service mysql stop才能停止。
还有直接使用mysql不能找到命令,错误为“bash: mysql: command not found”可以直接**mysql的安装目录中的bin文件夹跟绝对路径运行命令,还有的需要加./mysql 才能执行。

本文来自:http://sundful.javaeye.com/blog/704337

另一篇关于清除密码、重置用户的文章:

Quote:
Quotation

First things first. Log in as root and stop the mysql daemon.

sudo /etc/init.d/mysql stop

Now lets start up the mysql daemon and skip the grant tables which store the passwords.

sudo mysqld_safe --skip-grant-tables&

(press Ctrl+C now to disown the process and start typing commands again)

You should see mysqld start up successfully. If not, well you have bigger issues. Now you should be able to connect to mysql without a password.

sudo mysql --user=root mysql

update user set Password=PASSWORD('new-password');
flush privileges;
exit;

Now kill your running mysqld then restart it normally.

sudo killall mysqld_safe&
(press Ctrl+C now to disown the process and start typing commands again)
/etc/init.d/mysql start

You should be good to go. Try not to forget your password again.
http://www.howtoforge.com/reset-forgotten-mysql-root-password



另外关于denied的总结:
Quotation

MySQL Authentication Denial
3/29/2005, 12:05 am
It seems to me that a lot of people have auth denial when trying to make connections to MySQL. They seem to ignore the text of the error message. ‘Access Denied’ means access denied, nothing else.

Remember three things have to match. The host as MySQL sees it, the username, and the password. When MySQL returns access denied it’s not broken. One or more of those three things does not match. I don’t really need to reiterate what’s in the manual. Chang the lock or change the key to make it fit.

其中连接到mysql的文档内容为:
http://dev.mysql.com/doc/refman/5.5/en/access-denied.html
Pages: 1/5 First page 1 2 3 4 5 Next page Final page [ View by Articles | List ]