[client] port = 3306 default-character-set = utf8 socket=/tmp/mysql.sock
[mysql] port = 3306 default-character-set = utf8 socket = /tmp/mysql.sock
8.初始化Mysql并配置根目录与数据目录
1 2 3 4 5 6 7 8 9
/]$ ./bin/mysqld --initialize --initialize-insecure --user=mysql --basedir=/data/mysql/ --datadir=/data/mysql/data --pid-file=/data/mysql/data/mysql.pid 2019-07-25T02:17:26.926972Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-07-25T02:17:27.873911Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-07-25T02:17:27.989175Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-07-25T02:17:28.055380Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 59691eec-ae82-11e9-a601-525400f8d66e. 2019-07-25T02:17:28.060887Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-07-25T02:17:28.061415Z 1 [Note] A temporary password is generated for root@localhost: e7TbpjDNQb?a //记住此处的密码
# 如果忘记密码使用此命令查看:# cat /root/.mysql_secret
9.复制mysql启动脚本至/etc/init.d目录下并启动mysql服务
1 2 3 4
/]$ cp /data/mysql/support-files/mysql.server /etc/init.d/mysql /]$ chkconfig --add mysql /]$ systemctl enable mysql /]$ systemctl restart mysql # 重启报错检查日志、数据、pid目录是否存在与权限问题或者my.cnf配置文件参数是否正确
10.优化mysql可执行路径
1
/]$ ln -s /data/mysql/bin/* /usr/bin/
11.mysql5.7第一次登陆不能执行任何操作,需要修改root密码后才可以执行操作
1 2 3 4 5 6 7
/]$ mysqladmin -uroot -p password 'newpasswd' Enter password: # 输入新密码 mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. // 此提示可以忽略,是说直接输入密码不安全
sql> ALTER USER USER() IDENTIFIED BY 'putianhui'; # 修改新密码为putianhui sql> flush privileges;
语法:GRANT 权限列表 ON 库名.表名 TO 用户名@来源地址 [ identified by ‘密码’ ] mysql> grant all on *.* to 'zhangsan'@'%' identified by 'putianhui'; #新建zhangsan用户授予所有库所有表有所有的权限
3.13查询用户的授权信息
1 2
语法:show grants for '用户名@'来源地址'; mysql> show grants for 'zhangsan'@'%'; #查询zhangsan用户授权信息
3.14撤销用户授权信息,撤销后还能登陆数据库,无法操作
1 2
语法:revoke 权限信息 on 库名.表名. from '用户名'@'来源地址'; mysql> revoke all on *.* from 'zhangsan'@'%'; #撤销zhangsan用户的授权信息
3.15修改表中列的属性
1 2
语法:alter table 表名 modify column 列名 新的列类型 Alter table user modify column 姓名 char(200) #将user表中姓名列类型改为char(200)
~]$ mysqlbinlog --no-defaults /usr/local/mysql/mysql-bin.000002 # 查看000002二进制日志文件的内容 --------------------------------------------------------------------------------------------------------- # at 203 #181214 0:43:07 server id 1 end_log_pos 337 CRC32 0x4209f33e Query thread_id=2 exec_time=0 error_code=0 use `client`/*!*/; SET TIMESTAMP=1544719387/*!*/; insert xinxi values ('000008','王五','女','018','23') /*!*/; # at 337 #181214 0:43:07 server id 1 end_log_pos 368 CRC32 0xf4cb4c7d Xid = 38 COMMIT/*!*/; # at 368 #181214 0:43:15 server id 1 end_log_pos 451 CRC32 0xa428e268 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1544719395/*!*/; BEGIN /*!*/; # at 451 #181214 0:43:15 server id 1 end_log_pos 585 CRC32 0xc3e48c8c Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1544719395/*!*/; insert xinxi values ('000009','赵六','男','019','37') /*!*/; # at 585 #181214 0:43:15 server id 1 end_log_pos 616 CRC32 0xf05f2902 Xid = 39 COMMIT/*!*/; # at 616 #181214 0:45:59 server id 1 end_log_pos 663 CRC32 0xb3cc49ad Rotate to mysql-bin.000003 pos: 4 DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
④通过查看二进制文件内容可以看到每个操作前都有一个id或者时间点,例如在ID at 451之前插入了王五的信息
格式一:例如执行下面操作可以只将数据恢复到ID451,也就是说只恢复王五的信息,赵六的信息不恢复
1 2
~]$ mysqlbinlog --no-defaults --stop-position='451' /usr/local/mysql/mysql-bin.000002 | mysql -uroot -p # 恢复操作ID451之前的所有数据 mysql> mysql> select * from xinxi; # 查看xinxi表发现王五信息已经恢复了
~]$ mysqlbinlog --no-defaults /usr/local/mysql/mysql-bin.000002 # 查看000002二进制日志文件的内容 --------------------------------------------------------------------------------------------------------- # at 203 #181214 0:43:07 server id 1 end_log_pos 337 CRC32 0x4209f33e Query thread_id=2 exec_time=0 error_code=0 use `client`/*!*/; SET TIMESTAMP=1544719387/*!*/; insert xinxi values ('000008','王五','女','018','23') /*!*/; # at 337 #181214 0:43:07 server id 1 end_log_pos 368 CRC32 0xf4cb4c7d Xid = 38 COMMIT/*!*/; # at 368 #181214 0:43:15 server id 1 end_log_pos 451 CRC32 0xa428e268 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1544719395/*!*/; BEGIN /*!*/; # at 451 #181214 0:43:15 server id 1 end_log_pos 585 CRC32 0xc3e48c8c Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1544719395/*!*/; insert xinxi values ('000009','赵六','男','019','37') /*!*/; # at 585 #181214 0:43:15 server id 1 end_log_pos 616 CRC32 0xf05f2902 Xid = 39 COMMIT/*!*/; # at 616 #181214 0:45:59 server id 1 end_log_pos 663 CRC32 0xb3cc49ad Rotate to mysql-bin.000003 pos: 4 DELIMITER ; # End of log file ROLLBACK /* added by mysqlbinlog */; /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;