一、檢查是否安裝了mysql和mariadb,若已經安裝就需要卸載。
[root@localhost ~]# rpm -qa|grep mariadb // 查詢出來已安裝的mariadb [root@localhost ~]# rpm -e --nodeps 文件名 // 卸載mariadb,文件名為上述命令查詢出來的文件
[root@localhost ~]# rm /etc/my.cnf //刪除配置文件
二、添加mysql用戶及用戶組
[root@localhost ~]# groupadd mysql //創建mysql用戶組 [root@localhost ~]# useradd -g mysql mysql //創建mysql用戶,並添加到mysql用戶組
三、解壓文件,並移動到指定的目錄下
[root@localhost ~]# tar -zvxf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz [root@localhost ~]# mv 解壓出來的文件夾名 mysql [root@localhost ~]# mv mysql /usr/local/
四、創建配置文件
[root@localhost support-files]# vim /etc/my.cnf
#通過vim編輯器編輯my.cnf代碼如下:
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8
log-error = /usr/local/mysql/data/mysqld.log
pid-file = /usr/local/mysql/data/mysqld.pid
五、初始化數據庫
[root@localhost ~]# cd /usr/local/mysql/bin/
[root@localhost bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
六、查看初始默認密碼
[root@localhost bin]# cat /usr/local/mysql/data/mysqld.log 2019-06-05T07:08:28.263392Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-06-05T07:08:28.263457Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release. 2019-06-05T07:08:28.263462Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set. 2019-06-05T07:08:28.961752Z 0 [Warning] InnoDB: New log files created, LSN=45790 2019-06-05T07:08:29.039265Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2019-06-05T07:08:29.095290Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b858de41-8760-11e9-84f3-00505681edfc. 2019-06-05T07:08:29.096413Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2019-06-05T07:08:29.096953Z 1 [Note] A temporary password is generated for root@localhost: kXJh+_RMu52K
七、將啟動腳本放到開機初始化目錄
[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
八、啟動mysql
[root@localhost ~]# service mysql start
Starting MySQL. SUCCESS!
九、使用root賬號和默認密碼登錄mysql
[root@localhost ~]# cd /usr/local/mysql/bin/ [root@localhost bin]# ./mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.25 Copyright (c) 2000, 2019, 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>
十、修改root密碼
mysql> set password=password('root'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all privileges on *.* to root@'%' identified by '1234567890'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
十一、添加遠程訪問權限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '1234567890' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec)
十二、重啟mysql
[root@localhost bin]# service mysql restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!