環境准備
從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 狀態