Centos 7 & Ubuntu 20.04 MySQL的安装配置

1/15/2020 Mysql

# Centos 7

# 环境

  • 阿里云ecs
  • centos7.7

# 安装配置

  • 新建 /etc/yum.repos.d/mysql-community.repo
  • 把这些内容复制到上面的repo文件里
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7-x86_64/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql

[mysql-tools-community]
name=MySQL Tools Community
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7-x86_64/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql

[mysql-5.6-community]
name=MySQL 5.6 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.6-community-el7-x86_64/
enabled=0
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql

[mysql-5.7-community]
name=MySQL 5.7 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql

[mysql-8.0-community]
name=MySQL 8.0 Community Server
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/
enabled=1
gpgcheck=1
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

这里说一下,如果准备下5.7版本,就把8.0的enabled改为0,我这里装的是5.7

  • 执行 yum makecache & yum repolist all | grep mysql 应该就能看到 MySQL 5.7 Community Server(还有5.6 和8.0的版本)

  • 执行 yum -y install mysql-community-server 一会应该就能安装完,因为上面配置的是清华的源,速度还是可以的。

  • 安装好之后,执行 systemctl start mysqld.service

  • 然后是systemctl status mysqld.service 正常应该是能看到active/running字眼的

  • 获取数据库安装时生成的临时密码 grep "password" /var/log/mysqld.log 如果执行命令后,没有输出信息,执行rm -rf /var/lib/mysql,重启msyql服务 systemctl restart mysqld,然后再次获取试试。

  • 使用临时密码登录mysql mysql -u root -p--->输入临时密码--->登录到mysql cli。

  • 修改密码 ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password'; 这边测试已经有安全策略了,像123456的密码已经不能通过了,建议改个稍微复杂点的。 不想改的话可以修改密码安全策略 set global validate_password_policy=LOW; set global validate_password_length=6;

然后发现可以设置123456成功了

之后quit命令退出,测试新密码能否登录即可。

  • grant all privileges on *.* to root@"%" identified by "new password"; 该命令用于生成一个可以远程登录的root用户,然后navicat远程登录测试一下就行了。

  • 至于配置文件,在/etc/my.cnf

差不多就这些了。

# Ubuntu 20.04

  • 环境:腾讯云轻量服务器
  • mysql Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
  • 时间:2021/11/17
# install
sudo apt install mysql-server
# server will auto running after installed
# temporary user and password: /etc/mysql/debian.cnf, also can check /var/log/mysql/error.log
# login to mysql client and setting root password and remote user 

# update root password to 123456, you can change it to other if you would like
use mysql;
# the default plugin
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
update user set host = '%' where user = 'root' and host = 'localhost'
flush privileges;
flush hostsl

# check it
# select user, host, authentication_string, plugin from mysql.user;

# some useful command
# status
mysql> status
sudo service mysql status
sudo ss -tap | grep mysql
# restart 
sudo service mysql restart
# log
sudo journalctl -u mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

note bind-address on /etc/mysql/mysql.conf.d/mysqld.cnf if existed

[mysql]
# bind-address          = 127.0.0.1
# mysqlx-bind-address   = 127.0.0.1
1
2
3