1.mkdir -p /root/scripts/
2. cd /root/scripts/
vim query.sql,代码如下:
SELECT CONCAT(t.table_schema,".",t.table_name) as table_name FROM information_schema.TABLES t LEFT JOIN information_schema.TABLE_CONSTRAINTS tc ON t.table_schema = tc.table_schema AND t.table_name = tc.table_name AND tc.constraint_type = 'PRIMARY KEY' WHERE tc.constraint_name IS NULL AND t.table_type = 'BASE TABLE' AND t.table_schema not in ('information_schema','mysql','performance_schema','test');
vim no_primarykey.sh 代码如下:
#!/bin/bash db_ip=192.168.56.$1 db_port=$2 db_user='xx' db_password='xxxxx' script_dir=/root/scripts db_login="mysql -u${db_user} -p${db_password} -h${db_ip} -P${db_port}" echo ${db_login} for tname in `${db_login} -N -s -e "source $script_dir/query.sql"` do echo "=======================$tname=====================">>$script_dir/${db_port}_table.log $db_login -N -s -e "show create table $tname \G;">>$script_dir/${db_port}_table.log done
3,运行脚本,将在目录下生产结果
#chmod +x no_primarykey.sh
#./no_primarykey.sh 13 3306 (其中13 为DB SERVER最后IP位 即$1, 3306 为DB PORT 即$2)
结果如下:
#cat 3306_table.log =======================db1.t1===================== *************************** 1. row *************************** t1 CREATE TABLE `t1` ( `deviceid` varchar(64) NOT NULL, `uid` int(11) DEFAULT NULL, `city` varchar(40) DEFAULT NULL, `ktvcity` varchar(40) DEFAULT NULL, `regsrc` int(11) DEFAULT NULL, `status` int(11) DEFAULT NULL, `clienttype` int(11) DEFAULT NULL, `pushtoken` varchar(64) DEFAULT NULL, `apntoken` varchar(64) DEFAULT NULL, KEY `uid` (`uid`), KEY `deviceid` (`deviceid`), KEY `status` (`status`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4