---
title: "mysql命令记录"
author: "tinyhema"
date: "2014-12-05T09:55:15Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2014-12-05T10:02:33Z"
id: 2162410
url: "https://tinyhema.iteye.com/blog/2162410"
---

记录用过的mysql命令

###  添加用户


### 用户授权

```shell

grant all privileges on *.* to root@'%'' identified by 'root'
flush privileges;

```


### 字符串操作

```shell

# 替换
update tbl_name set field_name = replace(field_name, 'src', 'dest') where xxx = xxx 
# 拼接，注意ifnull是为了兼容字段为空的场景
concat(ifnull(field_name, ''), 's2')

```


### 删除重复值

```shell

delete torrent from torrent , (select id from torrent group by id having count(id)>1 ) as t2 where torrent.id=t2.id

```


### 添加主键

```shell

Alter table torrent add primary key(id)

```


### md5值建立索引

```shell

conv(substr(md5('?'),1,14),16,10)
# md5值是 32位字符串，通过以上转换，相当于把md5值的一部分取出来转成数字存储，可能会有重复的，但是数字的索引效率是要高于字符串的，可以提高索引效率

```



## dump数据时提升性能

```shell

	sed -i '/\[mysqld\]/a\innodb_flush_log_at_trx_commit=0' /etc/my.cnf
	sed -i '/\[mysqld\]/a\sync_frm=0' /etc/my.cnf

```


### java.sql.SQLException: Table 'xxx.XXX' doesn't exist
mysql + linux区分表名大小写，大写sql访问小写的表名会报错，修改my.cnf后重启mysql即可

```shell

	sed -i '/\[mysqld\]/a\lower_case_table_names=1' /etc/my.cnf

```


### 命令行监测db server连接数

```shell

mysql -uusername -ppassword -P3307 -h127.0.0.1 -e"show status like '%Threads_connected%'" -ss | awk '{print $2}'

```