MySQL - 定时数据备份

数据备份真的很重要, 因为可能有一天数据会被莫名其妙的删掉了. ...

December 28, 2020 · 1 min · K8sCat

MySQL 用户管理

不要直接使用 root 用户管理你的应用数据 ...

December 27, 2020 · 2 min · K8sCat

记一次 MySQL 启动导致的事故

MySQL 启动存在端口被监听, 但服务没完全启动的情况. ...

December 13, 2020 · 1 min · K8sCat

CentOS 安装 MySQL

MySQL 被 Oracle 收购后,CentOS 的镜像仓库中提供的默认的数据库也变为了 MariaDB, 那如何在 CentOS 上如何安装 MySQL 呢? 现在一般都是用 Docker, 跑个 MySQL: docker run -d --name mysql8 -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 mysql:8. 本文安装的是 MySQL8 # CentOS7 阿里源 sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo sudo yum update -y # centos7 # 下载mysql的rpm包: https://dev.mysql.com/downloads/repo/yum/ wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm # 安装rpm包 sudo rpm -ivh mysql80-community-release-el7-3.noarch.rpm # 安装MySQL sudo yum install mysql-community-server # 启动MySQL sudo service mysqld start # 查看MySQL状态 # service mysqld status # 获取临时密码 sudo cat /var/log/mysqld.log | grep 'temporary password' > 2019-10-28T04:31:58.792853Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: SAUraYtA3h+s # MySQL 配置文件 # mysql --help Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf # 进入MySQL mysql -uroot -p # 查看当前安全变量值, PS: 这是我修改后的查看的, MySQL8要在修改临时密码后才可以执行下面的命令 # 另外, MySQL57 用的是下划线, 例如: validate_password_policy mysql> show variables like 'validate_password%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | MEDIUM | | validate_password.special_char_count | 1 | +--------------------------------------+--------+ 7 rows in set (0.01 sec) # 设置密码策略, validate_password.policy # 0 or LOW: Length # 1 or MEDIUM: Length; numeric, lowercase/uppercase, and special characters # 2 or STRONG: Length; numeric, lowercase/uppercase, and special characters; dictionary file # global: 全局配置, 但是在MySQL重启后配合 mysql> set global validate_password.policy = 0; # 设置密码至少4位, validate_password.length mysql> set global validate_password.length = 4; # 成功修改密码为 123456 mysql> alter user 'root'@'localhost' identified by '123456' mysql> select user, host, authentication_string from user; +------------------+-----------+ | user | host | +------------------+-----------+ | mysql.infoschema | localhost | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +------------------+-----------+ 4 rows in set (0.00 sec) # 设置所有的IP都可以连接MySQL mysql> update user set host = '%' where user = 'root'; mysql> select user, host, plugin from user; +------------------+-----------+-----------------------+ | user | host | plugin | +------------------+-----------+-----------------------+ | root | % | mysql_native_password | | mysql.infoschema | localhost | caching_sha2_password | | mysql.session | localhost | caching_sha2_password | | mysql.sys | localhost | caching_sha2_password | +------------------+-----------+-----------------------+ 4 rows in set (0.00 sec) # 图形化工具(Navicat)无法连接数据库 # MySQL8 的密码认证插件: caching_sha2_password # MySQL57: mysql_native_password mysql> alter user 'root'@'%' identified with mysql_native_password by '123456'; # 上面的命令会出现问题: ERROR 1524 (HY000): Plugin 'mysql_native_plugin' is not loaded # 通过下面的命令设置认证插件 mysql> update user set plugin='mysql_native_password' where user = 'root'; mysql> alter user 'root'@'%' identified by '123456'; mysql> flush privileges; mysql> exit ...

October 28, 2019 · 3 min · K8sCat

MySQL性能优化

{% img /images/mysql.jpg %} 环境搭建 Vagrant + VirtualBox Vagrant: https://www.vagrantup.com/ VirtualBox: https://www.virtualbox.org/ Ubuntu16安装mysql57 更换阿里源 wget -O install.sh https://gitee.com/hsowan/ausi/raw/master/install.sh && sudo bash install.sh xenial 安装mysql57 sudo apt install mysql-server mysql-client 配置mysql(开启慢查询日志) mysql -uroot -p # 进入mysql # 修改root用户的host, 避免在其他主机上无法登录, 即远程登录 update mysql.user set host = '%' where user = 'root' and host = 'localhost'; flush privileges; # 上一步操作后必须使用该命令进行刷新 # 修改root的密码的几种方式 # 第一种 alter user 'root'@'%' identified by ''; # 第二种, 这种方式需要刷新权限 update user set authentication_string = password('') where user = 'root' and host = '%'; flush privileges; # 第三种 set password for 'root'@'%' = password(''); # 忘记密码, 在配置文件(vi /etc/mysql/my.cnf)中添加以下内容 # 则可以不用密码使用root登录, 再做修改密码的操作 [mysqld] skip-grant-tables # 查看mysql读取配置文件的顺序, 靠后的配置文件会覆盖前面的配置文件 /usr/sbin/mysqld --verbose --help | grep -A 1 'Default options' # /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf show variables # 查看mysql的配置参数 set slow_query_log = on; # 开启慢查询日志 set slow_query_log_file = /var/lib/mysql/slow-query.log; # 设置慢查询日志的文件 set log_queries_not_using_indexes = on; # 记录那些未使用索引的查询日志 # 上面的设置只针对当前会话有效 # mysql5可以使用global设置当前mysql服务有效(set global slow_query_log = on;), 意味着重启mysql服务这些将会恢复默认 # mysql8可以使用persist进行持久化设置, 即使重启mysql服务也有效 \s 查看mysql版本信息 \G 输出结果旋转90度 ...

June 17, 2019 · 4 min · K8sCat