MySQL常见问题

Mysql8无法进行远程连接

MySQL8.0 安装完成后出现无法远程链接,是因为MySQL8.0 只支持 localhost 访问,我们必须设置一下才可以远程访问。

 

具体设置步骤如下:

① 登录MySQL

执行命令为:mysql -u root -p

回车后输入密码

② 选择 mysql 数据库

执行命令为:use mysql;

查看mysql 数据库中存储的用户信息的 user 表。

③ 查看mysql 数据库的 user 表中当前 root 用户的相关信息

执行命令为:select host,user,authentication_string,plugin from user;

执行完命令后显示一个表格, root 用户的 host默认显示的 localhost,说明只支持本地访问,不允许远程访问。

④ 更改 host 的默认配置

执行命令为:update user set host='%' where user='root';

⑤ 刷新

执行命令为:flush privileges;

MQL配置远程登录权限

在安全规则及防火墙都放行3306商品的情况下,用如下语句授权mysql的远程登录用户及密码:

>> grant all privileges on *.* to '{用户名}'@'%' identified by '{密码}' with grant option;

>> flush privileges;

删除重复数据

删除表company中的重复的url记录(未保留第一条):

DELETE FROM company WHERE url IN (select a.url from (SELECT url FROM company GROUP BY url HAVING count(url)>1)a);

远程无法链接mysql服务器

登录mysql:

mysql> grant all privileges on *.* to 'root'@'%' identified by '<mysql登录密码>';  #将root用户授权给所以连接

mysql> flush privileges;

注意:如果用的是阿里云,还需要将3306端口加入到‘入方向’允许访问的规则中;

mysql : Lock wait timeout exceeded; try restarting transaction

问题:在数据表中进行频繁插入、删除操作后,提示:Lock wait timeout exceeded; try restarting transaction

原因:使用InnoDB表类型(默认类型)时,my.ini文件中“innodb_lock_wait_timeout”参数默认值50(即50秒),如果有锁等待超过了这个时间,就会报这个错.

解决方法:增大'innodb_lock_wait_timeout'参数的值,或者优化存储过程,事务避免过长时间的等待。

操作:进入my.ini文件,设置"innodb_lock_wait_timeout=500",即将锁等待时长设置为500秒。

备份报错:* is marked as crashed and should be repaired

描述:

在使用mysqldump命令对数据库进行备份时报错 "*(代表N个字符) is marked as crashed and should be repaired"

解决方法:

通过myisamchk命令进行数据库文件修复,以下为阿里云修复示例:

a、找到MYI文件位置,阿里云是在 "/alidata/server/mysql/data/数据库名"路径下,

b、执行命令: myisamchk -c -r /alidata/server/mysql/data/数据库名/*.MYI

然后再试下用mysqldump进行备份数据库,问题解决。

 

知识补充:

MySQL数据表默认类型是:myisam; 
.frm   是描述表结构的文件;
.MYD   是表的数据文件;
.MYI   是表数据文件中任何索引的数据树;

mysql不能进行远程连接

解决方法:

mysql> user mysql;

mysql> select host,user,password from user; #查看权限设置

msyql> grant all privileges on *.* to 'root'@'%' identified by 'kdm001' with grant option;   #授予所有电脑可用root用户进行远程连接

mysql> flush privileges;

获取最后插入与最新插入记录的id

import MySQLdb
conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='test')
cur=conn.cursor()
#cur.select_db('test')
print int(conn.insert_id())  #最新插入行主键id,一定要放在commit()前,否则会返回0  
print (cursor.lastrowid)  #最后插入行主键id
conn.commit()

Error:mysql55w conflicts with mysql-5.1.73-3.el6_5.i686

说明

系统默认安装的mysql5.1,我将5.1全部卸载后(记得卸载所有mysql插件,可用'rpm -qa|grep mysql'查看,并用'rpm -e <插件名>'进行卸载),安装了mysql5.5,在安装mysql-devel(安装MySQL-python包必须要安装这个)时报错提示:mysql55w conflicts with mysql-5.1.73-3.el6_5.i686

思考:

提示表示mysql55w与mysql5.1发生了冲突,可能时因为我采用"yum install mysql-devel" 安装,默认是安装与mysql5.1(默认版本)匹配的mysql-devel,所以与mysql5.5版本不兼容导致出错.

解决方法

理论上两种方法:

1、手动下载与mysql5.5兼容的mysql-devel进行安装,或更换yum源;

2、还原mysql版本至mysql5.1;

为了马上能用,我还原了版本.......,如果哪位也遇到类似问题,欢迎留言一起分享心得laugh

MySQL软件包升级地址

http://repo.webtatic.com/yum/centos/5/latest.rpm

yum install libmysqlclientl5 --enablerepo=webtatic

Error: 'Access denied for user 'root'@'localhost' (using password: YES)'

解决方法

[steve@localhost ~]$ sudo /etc/init.d/mysqld stop    #停止mysql服务

[steve@localhost ~]$ sudo mysqld_safe --user=mysql --skip-grant-tables --skip-networking &  #启动mysql,但不启动授权表,即不需要密码

[ooo@localhost ~]$ nohup: ignoring input and redirecting stderr to stdout    #本行为自动提示
Starting mysqld daemon with databases from /var/lib/mysql                          #本行为自动提示

mysql -u root mysql 注意这里要自己输入,没提示的

#即可进行到mysql

mysql> UPDATE user SET Password=PASSWORD('123456') where USER='root';  

mysql> FLUSH PRIVILEGES;

mysql> quit

[steve@localhost ~]$ sudo /etc/init.d/mysqld restart

[ooo@localhost ~]$ mysql -u root -p                 #用更改后的密码正常登录即可

Enter password:

来源:http://blog.sina.com.cn/s/blog_560e31000100v1a9.html