选择版本

mysql 这里通过rpm的方式安装,使用 MySQL 官方提供的安装包选择适合自己系统的包进行安装
到这个页面下选择适合自己系统的版本:
http://dev.mysql.com/downloads/mysql/

Select Operating System: 选择系统,如果是Linux CentOS 选 Red Hat即可,如果没有你的版本,选 Linux-generic。

Select OS Version: 选对应的版本,CentOS 7 选 Enterprise 7 x86, 64-bit。

最近添新增了一些CentOS7 的机器,就使用 Red Hat 的版本进行安装。

选择版本

御载旧版本

一定要御载干净,否则安装过程中有版本问题!!!
yum 和 rpm 两种方式二选一:
列出所有已安装的,但不在rpm中的包
yum list installed mysql
结果

mysql-libs.x86_64
yum -y remove mysql-libs.x86_64

等价于上面的命令
rpm -qa | grep mysql
结果

mysql80-community-release-el7-4.noarch
mysql-community-common-8.0.27-1.el7.x86_64

阿里去自带 MariaDB,并不是需要的直接御载。
移除

rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64

下载

wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.27-1.el7.x86_64.rpm-bundle.tar

EL是Red Hat Enterprise Linux的简写

  • EL6 软件包用于在Red Hat 6.x, CentOS 6.x, and CloudLinux 6.x进行安装
  • EL7 软件包用于在Red Hat 7.x, CentOS 7.x, and CloudLinux 7.x的安装

下载文件

必须安装(注意顺序)

yum install libaio
sudo rpm -ivh mysql-community-common-8.0.27-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-client-plugins-8.0.27-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-8.0.27-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-client-8.0.27-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-server-8.0.27-1.el7.x86_64.rpm

启动

systemctl start mysqld.service

查看服务状态

systemctl status mysqld.service

查看状态

修改密码

查看默认密码,这是一个生成的随机密码,复制出来后,修改成己的密码。

grep "password" /var/log/mysqld.log

mysql -u root -p

输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库
格式:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';

输入:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'abcdefg1234567';

成功提示:Query OK, 0 rows affected (0.01 sec)

如果提示:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

说明密码不满足默认安全策略,把密码搞复杂一点,就是那种自己也记不住,只能靠复制粘贴的那种。
也可以调低安全策略:

set global validate_password_policy=0;
set global validate_password_length=1;

安全策略,非常有必要

要启动mysql服务的时候,有一些提示说明,在那里有一些有用的信息。
其中就有开启安全策略的方式:

/usr/bin/mysql_secure_installation

下面有很多选项,不用头疼:

Remove anonymous users? y 移除
移除 anonymous 用户

Disallow root login remotely? y
是否开启 root 远程访问?
选 Y 开启远程访问,这个需要考虑一下,如果是本地开发的话,不需要开,如果是不同机器上做主从复制的话,就开启

Remove test database? y
自带的测试数据库,移除。

Reload privilege tables now? y
重启数据库权限表。

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Securing the MySQL server deployment.

Enter password for user root:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

开启远程登陆

use mysql;

host里指定ip,%是通配符,可以添加多条

update user set host = '%' where user = 'root';
#刷新,否则不成功
flush privileges;

查看当前用户的 host

select host, user from user;

查看host

退出

quit;