0、检查是否安装老版本的MySQL和mariadb,如果有先卸载,确保不存在 /etc/my.cnf 文件
shell> rpm -qa | grep mysql
shell> rpm -qa | grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
shell> yum remove mariadb-libs
... ...
Removed:
mariadb-libs.x86_64 1:5.5.52-1.el7
Dependency Removed:
postfix.x86_64 2:2.10.1-6.el7 redhat-lsb-core.x86_64 0:4.1-27.el7.centos.1
Complete!
shell> rpm -qa | grep mariadb
shell> ll /etc/my*
ls: cannot access /etc/my*: No such file or directory
1、先到mysql官网查看yum repository,根据CentOS版本,注意文件名,如下图红框所示
2、增加 MySQL Yum Repository
shell> sudo yum localinstall platform-and-version-specific-package-name.rpm
例如:要在CentOS 6.5下安装MySQL输入如下命令
shell>sudo yum localinstall https://repo.mysql.com/mysql80-community-release-el6-1.noarch.rpm
要在CentOS 7.x下安装MySQL输入如下命令
shell>sudo yum localinstall https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
安装完成后,在 /etc/yum.repos.d/下有个 mysql-community.repo文件,MySQL的不同版本的安装配置都有,选择你想安装的版本,把enabled设为1。如下图:
3、Selecting a Release Series
shell> yum repolist all | grep mysql
查看是否设为 enabled
4、Installing MySQL
shell> sudo yum install mysql-community-server
如有需要,修改MySQL数据存储目录
shell> vim /etc/my.cnf
修改
datadir=/var/lib/mysql/data
5、Starting the MySQL Server
shell> sudo service mysqld start
Initializing MySQL database: [ OK ]
Starting mysqld: [ OK ]
6、设置开机启动
CentOS 6
shell> chkconfig mysqld on
CentOS 7
shell> systemctl enable mysqld.service
7、查看随机生成的密码
shell> grep 'temporary password' /var/log/mysqld.log
2018-06-28T22:01:13.609839Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: -TIMLMsTf7ay
8、登录MySQL
shell> mysql -uroot -p
Enter password: -TIMLMsTf7ay
ommands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.11
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>
9、修改root密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@1234';
Query OK, 0 rows affected (0.43 sec)
10、允许root用户在任何地方进行远程登录,并具有所有库任何操作权限
MySQL 5.7
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Root@1234' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.13 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MySQL 8.0 (注意密码策略更严格,要求大小写字母+数字+其他字符组合)
mysql> CREATE USER `root`@`%` IDENTIFIED WITH mysql_native_password BY 'Root@1234';
Query OK, 0 rows affected (0.42 sec)
mysql> GRANT ALL ON *.* TO `root`@`%` WITH GRANT OPTION;
Query OK, 0 rows affected (0.19 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)