CHINA华军 2020-08-18
mysql -h${主机名或主机IP} -P${端口} -u${用户名} -p ${数据库名}; # 回车之后再输入密码 mysql -h${主机名或主机IP} -P${端口} -u${用户名} -p${密码} ${数据库名}; # 不建议这种显示输入密码的方式
show databases;
use ${数据库名};
create database ${数据库名} character set ${编码方式}; -- "character set ${编码方式}"可以省略, 此时使用默认的编码方式, 建议手动指定编码方式, 不要省略.
-- 查看schema下表清单 show tables; show full tables from ${schema名}; -- 查看表详细信息 select * from information_schema.tables where table_schema = ‘${Schema名}‘ and table_name = ‘${表名}‘;
create table ${表名} { ${字段名1} ${字段类型及长度} ${null/not null} default ${默认值} common ‘${字段注释}‘, ${字段名2} ${字段类型及长度} ${null/not null} default ${默认值} common ‘${字段注释}‘, ... ${字段名n} ${字段类型及长度} ${null/not null} default ${默认值} common ‘${字段注释}‘ primary key(${主键字段列表}) } ENGINE=${引擎类型} DEFAULT CHARSET=${表编码}; -- For example: create table zhoujl_test1{ id int not null common ‘ID‘, name varchar(600) not null common ‘姓名‘, birthday date not null common ‘出生日期‘, sex varchar(1) not null common ‘性别‘, salary double(19,2) null default 0.0 common ‘收入‘, primary key(id) } ENGINE=InnoDB DEFAULT CHARSET=utf8;
rename table ${原表名} to ${新表名}; ALTER TABLE t1 RENAME t2;
alter table ${表名} add column ${字段1表达式}, add column ${字段2表达式}, add column ${字段n表达式}; For example: CREATE TABLE t1 (c1 INT); ALTER TABLE t1 ADD COLUMN c2 INT;
alter table ${表名} modify column ${字段1表达式}, modify column ${字段2表达式}, modify column ${字段n表达式}; For example: alter table t2 modify column c4 varchar(20), modify column c3 int;
alter table ${表名} change ${原字段名} ${新字段表达式}; For example: alter table t2 change c3 c5 int;
alter table ${表名} drop column ${字名1}, drop column ${字段2}; For example: alter table t2 drop c5;
show full columns from ${表名}; select * from information_schema.columns where table_schema = ‘${schema名}‘ and table_name = ‘${表名}‘;
select table_schema, table_name from information_schema.tables where table_name not in (select distinct table_name from information_schema.columns where column_key = "PRI") AND table_schema not in (‘mysql‘, ‘information_schema‘, ‘sys‘, ‘performation_schema‘) and table_schema = ‘${schema名}‘;
show variables like ‘character%‘;
set names ${编码};
"set names xxx"一条命令相当于以下三条命令:
set character_set_client = xxx; set character_set_connection = xxx; set character_set_results = xxx;
1)修改表编码
alter table ${表名} default character set ${编码};
2)修改字段编码
alter table ${表名} change ${字段1} ${字段2} varchar(36) character set {编码};
3)转换表所有字段编码
alter table ${表名} convert to character set ${编码};
4)查看数据库编码格式
show variables like ‘character_set_database‘;
5)查看表编码格式
show create table ${表名};
6)创建/修改数据库时指定编码
create/alter database ${数据库名} character set utf8;
mysqldump -h${IP地址} -P${端口号} -u${登录用户名} -p${登录密码} [-t/-d] ${数据库名} --add-drop-table --tables ${表名列表, 用空格分隔} > ${保存到的本地文件名}
mysql -h${IP地址} -P${端口号} -u${登录用户名} -p${登录密码} ${数据库名} < ${数据文件}
grant ${权限类型} on ${数据库名}.${表名} to ${用户名} identified by ‘${登录密码}‘;
权限类型:
* all: 所有权限
* select/insert/update/delete: 查询/增加/修改/删除权限
* ...
举例: grant all on . to root identified by ‘Xxxxxxxx‘;
flush privileges;
systemctl start mysqld.service;
systemctl status mysqld.service;
systemctl restart mysqld.service;
systemctl stop mysqld.service;
show global variables like "%datadir%";
在/etc/my.cnf
文件中, [mysqld]
下面新增skip-grant-tables
,然后重启服务器.
MySQL执行插入或更新时, 当数据量过大时, 可能由于max_allowed_packet
参数的限制导致执行失败.此时, 可以重新设置该参数的值.max_allowed_packet
默认值为1M
.
max_allowed_packet
当前值show variables like ‘%max_allowed_packet%‘;
max_allowed_packet
值大小set global max_allowed_packet = ${大小}
my.ini
文件中, 修改或增加max_allowed_packet = 30M
, 数字根据需要设定./etc/my.cnf
文件中, 修改或增加max_allowed_packet = 30M
, 数字根据需要设定.show processlist;
kill ${进程ID}
连接进入MySQL服务, 使用source ${文件名}
执行. 末尾不能带分号.
alter user ‘${用户名}‘@‘${IP地址}‘ identified by ‘${登录密码}‘;