SimpleDao
MySQL数据库设置
2019-05-06, 访问数: 988

修改密码

  1. mysqladmin -u用户名 -p旧密码 password 新密码

备份还原数据库

  1. # 设置从特定地址的服务器导出数据, 缺省主机是localhost, 则设置参数-h 或 --host=
  2. # 备份所有数据库:
  3. mysqldump -u root -p123 --all-database > test.sql
  4. # 备份数据库test
  5. mysqldump -u root -p123 dbname > dbname.$(date "+%Y-%m-%d").sql
  6. # 备份数据库test下的temp表:
  7. mysqldump -u root -p123 test demp > test.sql
  8. # 还原数据库test
  9. mysqldump -u root -p123 test < test.sql
  10. # 还原数据库test下的temp表:
  11. mysqldump -u root -p123 test demp < test.sql
  12. # 但是有时候这样还原不了,那就进入mysql控制台,使用命令:
  13. source test.sql

权限设置,会自动创建账号

  1. # 授权
  2. grant all on dbname.* to username@'192.168.0.10' identified by 'passwd';
  3. # 删除权限
  4. revoke all on dbname.* from username@'192.168.0.10' identified by 'passwd';
  5. # 刷新权限
  6. FLUSH PRIVILEGES;
  7. # mysql8.0
  8. CREATE USER 'chat'@'%' IDENTIFIED BY '123456';
  9. GRANT ALL PRIVILEGES ON dbname.* TO 'chat'@'%' WITH GRANT OPTION;
  10. FLUSH PRIVILEGES;

编码设置

  1. set character_set_client=utf8;
  2. set character_set_connection=utf8;
  3. set character_set_database=utf8;
  4. set character_set_results=utf8;
  5. set character_set_server=utf8;

数据库及表转换成utf编码

  1. # 转换
  2. alter table table_name convert to character set utf8;
  3. # 和上面的不同,这个应该是设置编码
  4. alter database database_name character set utf8;
  5. alter table table_name character set utf8;

创建时指定编码

  1. # 创建数据库
  2. # create database name character set utf8;
  3. CREATE DATABASE test2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
  4. # 创建表
  5. CREATE TABLE 'table1' (
  6. `id` int(10) unsigned NOT NULL auto_increment,
  7. `flag_deleted` enum('Y','N') character set utf8 NOT NULL default 'N',
  8. `flag_type` int(5) NOT NULL default '0',
  9. `type_name` varchar(50) character set utf8 NOT NULL default '',
  10. PRIMARY KEY (`id`)
  11. ) DEFAULT CHARSET=utf8;

查看字符编码

  1. SHOW VARIABLES LIKE'character_set_%';
  2. SHOW VARIABLES LIKE'collation_%';

数据库设置

  1. # 找到客户端配置[client] 在下面添加
  2. default-character-set=utf8 #默认字符集为utf8
  3. # 在找到[mysqld] 添加
  4. character-set-server=utf8 #默认字符集为utf8
  5. init-connect='SET NAMES utf8'#(设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)