数据库的那些事儿
1、MySQL如何将select结果保存到一个数据库表中:MySql将查询结果插入到另外一张表
2、MySQL长事务导致的Table Metadata Lock:
现象:MySQL数据库表添加字段时卡死,报错为:Waiting for table metadata lock,此时数据库表无法删除和修改,只能read,这是因为MySQL出现了死锁(?)
解决方法:先执行 show processlist,查看当前那些id出现了上述错误,如下图:
然后执行kill id,对于上图就是kill 605546;
接着执行select * from information_schema.innodb_trx\G,结果如下图:
然后执行kill 562347;最后把数据库表删除即可(这一步不做,只要执行kill 562347就差不多了)
3、 MYSQL 提示Please DISCARD the tablespace before IMPORT:
这个是表删除不干净导致的,目录删除了 但是物理文件还在,可以使用show global variables like "%datadir%";查看mysql数据库文件保存的位置,然后删除 :表名.frm 表名.ibd;这两个文件 即可
4、mysql报“open too many files”错误的解决方法:How to permanently raise ulimit 'open files' and MySQL 'open_files_limit'
对于centOS,下面测试可行:
Hi, I have a CentOS 7 / MySQL 5.6 server and I tried all suggestions i found online, and still cat /proc/xxxx/limits showed 1024 max open files. (where X is the pid from the mysqld process. The missing thing was a .configuration setting in the mysql service.
I did the following:
Go to services directory:
cd /etc/systemd/system/
Remove the symlink:
rm mysql.service
Copy service to services directory (so it won't be overwritten by any upgrade):
cp /usr/lib/systemd/system/mysqld.service mysqld.service
In the Service section add LimitNOFILE=1024000.
In my case it's now:
[Service]
User=mysql
Group=mysql
LimitNOFILE=1024000
Restart.
limits are now:
[[email protected] system]# cat /proc/5981/limits
Max open files 1024000 1024000 files
4、MySQL 如何查看表的存储引擎:show create table +表名;
5、如何插入包含datetime类型字段的记录:MySQL: Insert datetime into other datetime field
According to MySQL documentation, you should be able to just enclose that datetime string in single quotes, ('YYYY-MM-DD HH:MM:SS') and it should work.
6、MySQL数据导入导出指令:
导出.sql文件:mysqldump,详细用法请参考MySQL mysqldump数据导出详解
导入.sql文件:source
7、MySQL可以使用limit关键字实现分页查询,详细参考:详解MySQL的limit用法和分页查询语句的性能分析
8、win7下启用MySQL服务:
当按照官网的教程,使用指令:MySQL安装路径\bin\mysqld或者使用网上教程:切换到bin路径下执行net start mysql均无法启动MySQL服务时,可以在win7的服务中手动启动MySQL服务,如下图:
9、在Ubuntu 16.04下,当mysql被动更新了版本时(如使用apt-get install安装Plibmysqlclient-dev库【Python的mysql-python包依赖该库】),比如从5.7版本更新为8.0版本,此时连上数据库(使用root账户)后执行指令,如show databases,会出现如下错误:
解决方法:
执行以下指令:mysql_upgrade -u root -p ,输入root账户的密码即可。
10、数据库脏读、幻读、不可重复读问题:MySQL的四种事务隔离级别