MYSQL日常操作命令再熟悉

1,创建用户及密码:

CREATE USER 'user'@'%' IDENTIFIED BY 'password';

 

2,创建数据库:

create database PDB_chengang default character set utf8 collate utf8_general_ci;

 

3,分配权限:

GRANT ALL ON PDB_chengang.* TO 'user'@'%';

 

4,使配置即时生效:

flush privileges;

 

5,按条件检索:

 select * from deploy_deploypool where id=388;

 

6,更新一条数据库条目:

update deploy_deploypool set name='2452345 ' where id=388;

 

7,用另外表里的相等条目来更新本表里的条目:

update deploy_deploypool a set jenkins_number = (select jenkins_number from version_deployversion b where 
b.nginx_url=a.nginx_url);

8,导出数据库数据:

mysqldump -uuser -p PDB > PDB.20160303.sql

 

9,导入数据库数据:

source /root/dir/PDB.20160303.sql;

 

10,批量删除数据表:

#!/bin/bash
# 删除mysql中所有表
# 示例:
# Usage: ./script user password dbnane
# Usage: ./script user password dbnane server-ip
# Usage: ./script user password dbnane mysql.nixcraft.in
# ---------------------------------------------------
 
MUSER="$1"
MPASS="$2"
MDB="$3"
 
MHOST="localhost"
 
[ "$4" != "" ] && MHOST="$4"
 
# 设置命令路径
MYSQL=$(which mysql)
AWK=$(which awk)
GREP=$(which grep)
 
# help
if [ ! $# -ge 3 ]
then
 echo "Usage: $0 {MySQL-User-Name} {MySQL-User-Password} {MySQL-Database-Name} [host-name]"
 echo "Drops all tables from a MySQL"
 exit 1
fi
 
# 连接mysql数据库
$MYSQL -u $MUSER -p$MPASS -h $MHOST -e "use $MDB"  &>/dev/null
if [ $? -ne 0 ]
then
 echo "Error - 用户名或密码无效,无法连接mysql数据库"
 exit 2
fi
 
TABLES=$($MYSQL -u $MUSER -p$MPASS -h $MHOST $MDB -e 'show tables' | $AWK '{ print $1}' | $GREP -v '^Tables' )
 
# make sure tables exits
if [ "$TABLES" == "" ]
then
 echo "Error - 在数据库中 $MDB 未发现相关表"
 exit 3
fi
 
# let us do it
for t in $TABLES
do
 echo "Deleting $t table from $MDB database..."
 $MYSQL -u $MUSER -p$MPASS -h $MHOST $MDB -e "drop table $t"
done

慢慢积累,慢慢感觉。。。

 

你可能感兴趣的:(MYSQL日常操作命令再熟悉)