MySQL使用随笔
001 查看版本
mysql --version
mysql > select version();
mysql > status;
002 新建MySQL用户、授权
insert into mysql.user(Host,User,Password) values("localhost","username",password("yourpassword"));
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY "yourpassword" WITH GRANT OPTION;
flush privileges;
003. 查询支持的存储引擎
show engines;
004 查看变量
show variables like 'innodb%';
005 导入数据库(sql文件)
mysql -uroot -p dbname < dbname.sql
006 my.cnf说明
[client] #客户端配置
port = 3306
socket = /var/lib/mysql/mysql.sock #本地客户端使用这个socket链接mysqld
default_character_set = utf8
secure_auth = 0 #跳过密码格式不统一问题
[mysqld]
datadir=/usr/local/mysql/data #指定数据路径
socket=/var/lib/mysql/mysql.sock #使用这个socket启动server
user=mysql
old_passwords = 1
secure_auth = 0
innodb_force_recovery=0 #必须为0,否则innodb就是read only模式
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
007 修改root密码
mysqld_safe --skip-grant-tables
mysql > update mysql.user set password=password("new_pass") where user="root";
008 让mysql服务器输出所有执行的SQL语句记录
mysqld --general-log=TRUE
日志输出为mysql服务器数据目录的localhost.log, 其他日志请执行mysqld --verbose --help查看
009 数据库导出导入
导出数据
mysqldump -uusername -ppassword databasename > databasename.sql
导出存储过程和函数
mysqldump -uusername -ppassword -ntd -R databasename > stored-procs.sql
只导出表结构
mysqldump -uusername -ppassword -d --add-drop-table databasename > table-init.sql
导入sql文件(可以为数据,表结构,存储过程...)
mysql -uusername -ppassword databasename < sql-file.sql
010 查看当前哪些线程在运行
mysql >show processlist;
在Info字段还可以查看执行的语句,如果有执行很慢的语句,可以直接查询到。
011 删除表记录
DELETE FROM tablename
TRUNCATE TABLE tablename
012 mysql存储过程的sql secuirty
mysql存储过程中sql secuirty决定了执行相关存储过程时的安全限制,sql secuirty可以为DEFINER或者INVOKER。
DEFINER表示执行此存储过程的用户必须是存储过程创建者且必须存在这个用户并有对应权限。
INVOKER表示执行此存储过程的用户必须是存在的用户且这个用户并有对应权限。
创建存储过程时,可以指定sql secuirty,以下例子指定sql secuirty为INVOKER。
CREATE DEFINER = 'admin'@'localhost' PROCEDURE account_count()
SQL SECURITY INVOKER
BEGIN
SELECT 'Number of accounts:', COUNT(*) FROM mysql.user;
END;
也可以通过修改mysql.proc表中对应字段security_type来设定sql secuirty。例
update mysql.proc set security_type = ...
或
alter procedure pro_name sql security invoker;
013 数据表增加字段(时间字段)
mysql> alter table mytable add column ctime datetime DEFAULT NOW() NOT NULL after flag;
Query OK, 0 rows affected (0.26 sec)
Records: 0 Duplicates: 0 Warnings: 0
014 左、右join
语法:
SELECT * FROM tableA
LEFT|RIGHT JOIN tableB
ON tableA.field1 = tableB.field2
左JOIN:以左(tableA)表为基础查出所有符合条件的tableA和tableB,tableB不存在的记录显示tableB相关字段为NULL;
右JOIN:以右(tableB)表为基础查出所有符合条件的tableA和tableB,tableA不存在的记录显示tableA相关字段为NULL;
015 修改列定义,定义索引
ALTER TABLE table_name modify column_name varchar(32);
ALTER TABLE table_name ADD INDEX `column_name` (`column_name`);
016 make mysql
http://dev.mysql.com/doc/internals/en/cmake-build-options-official-mysql.html
Unix (Makefiles)
mkdir bld
cd bld
cmake .. -DBUILD_CONFIG=mysql_release
make
注:安装libaio后如果还是提示依赖,重新执行上面步骤(删除bld)
017 mysql升级后无法赋用户权限,修正
2014-05-27 14:48:34 17291 [ERROR] Missing system table mysql.proxies_priv; please run mysql_upgrade to create it
2014-05-27 14:48:34 17291 [Warning] Info table is not ready to be used. Table 'mysql.slave_master_info' cannot be opened.
2014-05-27 14:48:34 17291 [Warning] Info table is not ready to be used. Table 'mysql.slave_relay_log_info' cannot be opened.
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_current' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_history' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_history_long' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_host_by_event_name' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_instance' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_thread_by_event_name' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_user_by_event_name' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_account_by_event_name' has the wrong structure
解决办法:
mysql_upgrade -u root -p
018 sql_mode doesn't have a default value null
现象是无法导入存储过程,提示如题所示的错误,解决办法,修改配置文件中sql_mode,注释掉重新启动MySQL
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
如果发现执行存储过程时有默认值的情况仍旧无法插入数据,也可能是这个原因,这时要直接修改mysql.proc表中对应存储过程的sql_mode为NO_ENGINE_SUBSTITUTION即可
详细信息查看这里
也可以直接连接到mysql执行source /path/to/sql_file.sql
019 MySQL安装后没有mysql数据库
运行安装目录中的./scripts/mysql_install_db
020 mysql连接localhost
安装mysql后,只能使用localhost作为host连接,无法使用其他远程主机连接,纠结许久(防火墙好像没有限制啊!),发现mysqld是以13306端口启动的,但应用server的数据库配置中写的是3306,仍然连接成功。为什么?
原因是mysql连接localhost使用的是socket文件,而不是tcp连接,因此指定端口是无效的。如下所示的13306端口会被忽略
shell> mysql --port=13306 --host=localhost
如果需要使用TCP连接,则需要指定--host为不同于localhost,或指定以TCP方式连接。
shell>mysql --port=13306 --host=127.0.0.1
shell>mysql --port=13306 --protocol=TCP
MySQL使用随笔的更多相关文章
- Mysql查询优化随笔记录
select SQL_CALC_FOUND_ROWS * from (select * from oses_vehdata201606 union all select * from oses_ ...
- MySQL的随笔
数据库引擎 MySQL5.0支持的存储引擎包括MyISAM,InnoDB.MEMORY.MERGE.BDB等等,其中InnoDB和BDB提供事务安全表,其他存储引擎都是非事务安全表. MyISAM M ...
- 深入浅出mysql全文随笔
进入mysql :mysql -uroot -p 1.DDL(Data Definition Languages)语句:数据定义语言 2.DML(Data Manipulation Language) ...
- MySQL学习随笔记录
安装选custmer自定义安装.默认安装全部在c盘.自定义安装的时候有个advance port选项用来选择安装目录. -----------------------MySQL常见的一些操作命令--- ...
- MYSQL练习随笔
解法练习 案例1.子查询练习 字段 说明film_id 电影idtitle 电影名称description 电影描述信息category_id 电影分类idname 电影分类名称last_update ...
- MySQL -2- 体系结构--随笔小记
简介与安装NoSQLRDBMS版本安装方式二进制安装,源码安装体系结构CS模型TCP/IPsocketmysql master thread 实例mysqld 的程序构成连接层 协议.验证.链接线程S ...
- MySQL/MariaDB随笔二
mariaDB的二进制安装:系统版本和MariaDB版本 [root@ ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@ ~ ...
- MySQL/MariaDB随笔一
1.yum 安装后先跑一下系统自带的安全脚本,否则数据库很不安全,任何人都可以登录 [root@xixi ~]# mysql_secure_installation NOTE: RUNNING ALL ...
- mysql时间随笔
SELECT FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') FROM `order`; select date_add(FROM_UNIXTIME(cr ...
随机推荐
- ubuntu的dns设置
ubuntu的dns设置为: dns-nameservers 8.8.8.8 注意不要少s
- iOS - Block 代码块
1.Block Block 是一段预先准备好的代码,可以在需要的时候执行,可以当作参数传递.Block 可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.Block 是 C 语言的, ...
- 笔记本_hp
1.技术支持 http://support.hp.com/cn-zh 2.搜到的信息:“http://forum.51nb.com/thread-1080424-1-1.html” Product N ...
- 一次tomcat服务器被入侵解决办法
突然tomcat目录下莫名其妙的多了几个war文件,里面内容只有一个index.jsp,打开控制台发现多了几个应用,我可以确定不是我部署上去的,顺着应用访问竟然看到了
- Python学习笔记1—模块
模块的使用 引用模块的两种形式 形式一: import module_name 形式二: from module1 import module11 (module11是module的子模块) 例: ...
- python中self,cls
cls主要用在类方法定义,而self则是实例方法. self, cls 不是关键字,完全可以使用自己写的任意变量代替实现一样的效果. 普通的实例方法,第一个参数需要是self,它表示一个具体的实例本身 ...
- img、input到底是行内还是块级元素?
一.img.input属于行内替换元素.height/width/padding/margin均可用.效果等于块元素. 行内非替换元素,例如, height/width/padding to ...
- 使用kaptcha生成验证码
原文:http://www.cnblogs.com/xdp-gacl/p/4221848.html kaptcha是一个简单好用的验证码生成工具,通过配置,可以自己定义验证码大小.颜色.显示的字符等等 ...
- eclipse下安装插件
最近想自己弄弄Python,手上就有eclipse,也不想在安装别的IDE占空间,就在网上找了一下eclipse支持开发python的插件,果然有. pydev官网地址:http://pydev.or ...
- Linux 打包和压缩 方法详解
一般基因组的数据都非常大,所以都会 打包 压缩 后进行传输,拿到数据后的第一步必然就是要 解包 和 解压缩. 基本常识 首先要弄清两个概念:打包 和 压缩. 打包 是指将一大堆文件或目录变成一个总的文 ...