环境准备
从CentOS 7.0发布以来,yum源中开始使用mariadb来代替MySQL的安装。即使你输入的是yum install mysql , 显示的也是mariadb的安装内容。
首先先卸载mariadb。
检查mariadb是否已安装
[root@localhost ~]# yum list installed | grep mariadb mariadb-libs.x86_64 1:5.5.56-2.el7 @anaconda
全部卸载

[root@localhost ~]# yum -y remove mariadb* Loaded plugins: fastestmirror Resolving Dependencies --> Running transaction check ---> Package mariadb-libs.x86_64 1:5.5.56-2.el7 will be erased --> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64 --> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-6.el7.x86_64 --> Running transaction check ---> Package postfix.x86_64 2:2.10.1-6.el7 will be erased --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================= Package Arch Version Repository Size ======================================================================================================================================================================= Removing: mariadb-libs x86_64 1:5.5.56-2.el7 @anaconda 4.4 M Removing for dependencies: postfix x86_64 2:2.10.1-6.el7 @anaconda 12 M Transaction Summary ======================================================================================================================================================================= Remove 1 Package (+1 Dependent package) Installed size: 17 M Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Erasing : 2:postfix-2.10.1-6.el7.x86_64 1/2 Erasing : 1:mariadb-libs-5.5.56-2.el7.x86_64 2/2 Verifying : 1:mariadb-libs-5.5.56-2.el7.x86_64 1/2 Verifying : 2:postfix-2.10.1-6.el7.x86_64 2/2 Removed: mariadb-libs.x86_64 1:5.5.56-2.el7 Dependency Removed: postfix.x86_64 2:2.10.1-6.el7 Complete!
安装Mysql
一、下载安装官方提供的yum rpm包
下载网页:https://dev.mysql.com/downloads/repo/yum/
点击Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package后面的download,进入新的页面点击No thanks, just start my download.就可以看到下载源地址了。
下载wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
安装rpm包
rpm -ivh mysql80-community-release-el7-1.noarch.rpm
检查mysql的yum源是否安装成功:yum repolist enabled | grep "mysql.*-community.*"
[root@localhost src]# yum repolist enabled | grep "mysql.*-community.*" mysql-connectors-community/x86_64 MySQL Connectors Community 51 mysql-tools-community/x86_64 MySQL Tools Community 63 mysql80-community/x86_64 MySQL 8.0 Community Server 17
二、使用yum install mysql-server安装(这里会有点慢)
yum -y install mysql-server

1 [root@localhost ~]# rpm -qi mysql-community-server 2 Name : mysql-community-server 3 Version : 8.0.11 4 Release : 1.el7 5 Architecture: x86_64 6 Install Date: Thu 17 May 2018 09:40:08 PM EDT 7 Group : Applications/Databases 8 Size : 1633523890 9 License : Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Under GPLv2 license as shown in the Description field. 10 Signature : DSA/SHA1, Sun 08 Apr 2018 01:07:21 PM EDT, Key ID 8c718d3b5072e1f5 11 Source RPM : mysql-community-8.0.11-1.el7.src.rpm 12 Build Date : Sun 08 Apr 2018 03:29:08 AM EDT 13 Build Host : siv27.no.oracle.com 14 Relocations : (not relocatable) 15 Packager : MySQL Release Engineering <mysql-build@oss.oracle.com> 16 Vendor : Oracle and/or its affiliates 17 URL : http://www.mysql.com/ 18 Summary : A very fast and reliable SQL database server 19 Description : 20 The MySQL(TM) software delivers a very fast, multi-threaded, multi-user, 21 and robust SQL (Structured Query Language) database server. MySQL Server 22 is intended for mission-critical, heavy-load production systems as well 23 as for embedding into mass-deployed software. MySQL is a trademark of 24 Oracle and/or its affiliates 25 26 The MySQL software has Dual Licensing, which means you can use the MySQL 27 software free of charge under the GNU General Public License 28 (http://www.gnu.org/licenses/). You can also purchase commercial MySQL 29 licenses from Oracle and/or its affiliates if you do not wish to be bound by the terms of 30 the GPL. See the chapter "Licensing and Support" in the manual for 31 further info. 32 33 The MySQL web site (http://www.mysql.com/) provides the latest news and 34 information about the MySQL software. Also please see the documentation 35 and the manual for more information. 36 37 This package includes the MySQL server binary as well as related utilities 38 to run and administer a MySQL server.
三、启动mysql
[root@localhost ~]# service mysqld start Redirecting to /bin/systemctl start mysqld.service
四、使用初始密码登录
[root@localhost ~]# cat /var/log/mysqld.log|grep 'A temporary password' 2018-05-18T02:01:17.558742Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: belPU>iuF4*H
最后一行冒号后面的部分belPU>iuF4*H就是初始密码。
五、mysql5.7开始对root密码复杂性要求很高,必须有数字、字母大小写、符号(应该是需要8位以上),这里我么还查show databases;不了。
[root@localhost ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 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> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
只需要把root密码更改的复杂一些即可。
更改密码方法:
解决办法 1、 修改用户密码 mysql> alter user 'root'@'localhost' identified by 'youpassword'; 或者(下面这个只能修改当前登录的用户密码) mysql> set password=password("youpassword"); 2、刷新权限 mysql> flush privileges;
示例
mysql> alter user 'root'@'localhost' identified by 'qA123,./'; Query OK, 0 rows affected (0.10 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)
重启mysqld服务。
[root@localhost ~]# systemctl restart mysqld.service
常用的命令: systemctl start mysqld #启动mysqld systemctl stop mysqld #停止mysqld systemctl restart mysqld #重启mysqld systemctl enable mysqld #设置开机启动 systemctl status mysqld #查看 MySQL Server 状态