1 先检查系统是否装有MySQL
$ rpm -qa | grep mysql
返回空值,说明没安装,如果有安装,则删除可用:
$ yum remove mysql
注意:这里执行安装命令yum install mysql
是无效的,因为centos-7默认是Mariadb
,所以它只是更新Mariadb
数据库。
2 下载MySQL
从 MySQL 官网选取合适的 MySQL 版本,获取下载地址。然后使用 wget
下载:
$ wget http://repo.mysql.com//mysql57-community-release-el7-8.noarch.rpm
3 安装yum repository
$ yum -y install mysql57-community-release-el7-8.noarch.rpm
4 安装MySQL服务端
查看mysql server:
$ yum search mysql-com
安装:
$ yum -y install mysql-community-server.x86_64
5 启动MySQL服务
注意 CentOS7 的启动方式和以前不一样,CentOS7 使用了 systemctl
$ systemctl start mysqld.service
查看启动是否成功:
$ systemctl status mysqld.service
6 登录数据库
MySQL5.7.6 之后会在启动 mysql 进程的时候生成一个用户密码,首次登陆需要这个密码才行。密码保存在 mysql 进程的日志里,即(/var/log/mysqld.log
)。
查看密码:
[root@localhost ~]# cat /var/log/mysqld.log | grep 'password'
2018-05-09T05:05:40.656798Z 1 [Note] A temporary password is generated for root@localhost: pldEoqu1h!ks
登录:
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
安装完成!
7 修改root密码
如果需要正常使用,需要重置密码,否则如下:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
重置密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_passwd';
注意设置密码的复杂度,过于简单会报错:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
8 开启远程连接
CentOS系统安装好MySQL后,默认情况下不支持用户通过非本机连接上数据库服务器,开启远程连接方法:
# 任何远程主机都可以访问数据库
$ GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
# 使修改生效
$ FLUSH PRIVILEGES;