Skip to content

Instantly share code, notes, and snippets.

@zhanzhenchao
Last active July 31, 2020 07:44
Show Gist options
  • Save zhanzhenchao/9131a15b74ee271751a0b01d704b0fae to your computer and use it in GitHub Desktop.
Save zhanzhenchao/9131a15b74ee271751a0b01d704b0fae to your computer and use it in GitHub Desktop.
mysql
  1. 添加安装包
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
  1. 更新yum命令
yum clean all && yum makecache
  1. 安装
yum install -y  mysql-community-server
  1. 配置文件
vim /etc/my.cnf
[mysqld]

port = 3306

character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

# 表名不区分大小写(启动前配置)
lower_case_table_names=1

#设置日志时区和系统一致
log_timestamps=SYSTEM

[client]
default-character-set=utf8mb4
  1. 启动服务
#启动服务
systemctl start mysqld

#查看版本信息
mysql -V

#查看状态
systemctl status mysqld

##开机启动
systemctl enable mysqld
systemctl daemon-reload

  1. 修改账号密码
#1、查看MySQL为Root账号生成的临时密码
grep "A temporary password" /var/log/mysqld.log

#2、进入MySQL shell
mysql -u root -p

#3、修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

  1. 开启远程连接
#选择 mysql 数据库:
USE mysql;

#在 mysql 数据库的 user 表中查看当前 root 用户的相关信息:
SELECT host, user, authentication_string, plugin FROM user;

#设置root 用户远程访问:
update user set host = '%' where user ='root';

#刷新权限:
FLUSH PRIVILEGES;

#授权的所有权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

#更新 root 用户密码及加密规则(如果客户端不支持加密插件):
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'MyNewPass4!';

#刷新权限:
FLUSH PRIVILEGES;
  1. 新建远程用户
#1、新建远程用户
CREATE USER 'devops'@'%' IDENTIFIED BY 'MyNewPass3!';

#2、赋予指定账户指定(数据库名称.表名)远程访问权限
GRANT ALL PRIVILEGES ON mydb_name.* TO 'devops'@'%';

#3、查看权限
SHOW GRANTS FOR 'devops'@'%';

#4、收回权限
REVOKE ALL PRIVILEGES ON *.* FROM 'devops'@'%';

#5、删除用户
DROP USER 'devops'@'%';

#6、刷新权限
FLUSH PRIVILEGES;

  1. 找回密码
#1.关闭MySQL
service mysql stop

#2.用以下命令启动MySQL,以不检查权限的方式启动;
service mysql start --skip-grant-tables

#3.然后用空密码方式使用root用户登录 MySQL;
mysql -u root

  1. 启动失败
#权限问题
chown mysql:mysql -R /var/run/mysqld

/usr/sbin/mysqld --user=mysql &
  1. stop mysql
  2. sudo mysqld_safe --skip-grant-tables
  3. mysql -u root
  4. open another terminal
  5. running the following command:
UPDATE mysql.user SET authentication_string=PASSWORD('password') WHERE User='root';

FLUSH PRIVILEGES;

then, close both terminal, and access Mysql to reset password

mysql -uroot -p

SET PASSWORD = PASSWORD('your_new_password');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment