Linux安裝mysql教程


安裝之前需要先卸載mysql

1.1、下載壓縮包

1 [root@guohaien package]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

其中下載地址的獲取方法如下所示

1、打開mysql官網

2、按下圖所示的步驟點擊

 

3、右鍵下圖標記位置,復制下載地址

1.2、給mysql目錄創建一個用戶組和用戶,並在mysql目錄下創建一個data目錄,具體的操作如下所示

1 [root@guohaien soft]# groupadd mysql
2 [root@guohaien soft]# useradd -g mysql -d /soft/mysql mysql
3 [root@guohaien soft]# cd mysql
4 [root@guohaien mysql]# mkdir data

1.3、先解壓包,后進入文件夾mysql-5.7.25-linux-glibc2.12-x86_64,將里面的內容移動到上面創建的mysql目錄下,並刪除該文件夾,最后進入到mysql目錄中,具體的操作順序如下所示

1 [root@guohaien package]# tar zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
2 [root@guohaien package]# cd mysql-5.7.25-linux-glibc2.12-x86_64
3 [root@guohaien mysql-5.7.25-linux-glibc2.12-x86_64]# mv * /soft/mysql
4 [root@guohaien mysql-5.7.25-linux-glibc2.12-x86_64]# cd ..
5 [root@guohaien package]# rm -rf mysql-5.7.25-linux-glibc2.12-x86_64
6 [root@guohaien package]# cd ../mysql

1.4、初始化數據庫,初始化的時候會生成一個隨機密碼,需要記錄下來,后面會用到,其中密碼在如下圖標志的位置

初始化是可能會出現以下問題

其中一種輸出是如下所示

./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

這是因為沒有安裝libaio,只需要執行如下命令即可

1 [root@guohaien mysql]# yum install -y libaio

還有可能輸出如下結果,這是因為data目錄不為空,只需要進入到data目錄執行rm -rf *

2019-03-19T06:04:15.345713Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-03-19T06:04:15.347456Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2019-03-19T06:04:15.347485Z 0 [ERROR] Aborting

1.5、修改mysql.server文件中的basedir和datadir的值,一下是修改文件的命令,和修改的位置

1 [root@guohaien mysql]# vim support-files/mysql.server

修改完成按Esc,然后輸入wq退出

1.6、創建配置文件

給默認的my.cnf文件備份

1 [root@guohaien mysql]# cp /etc/my.cnf /etc/my.cnf.bak

可以將my.cnf文件修改為如下所示

[mysqld]
basedir=/soft/mysql
datadir=/soft/mysql/data
character_set_server=utf8
init_connect='SET NAMES utf8'
[client]
default-character-set=utf8

1.7、啟動mysql服務

[root@guohaien mysql]# ./support-files/mysql.server start

若輸出以上結果則啟動成功

可以通過如下步驟改變啟動的方法,並設置為開機自啟動

1 [root@guohaien mysql]# cp support-files/mysql.server /etc/init.d/mysqld
2 [root@guohaien mysql]# systemctl enable mysqld
3 mysqld.service is not a native service, redirecting to /sbin/chkconfig.
4 Executing /sbin/chkconfig mysqld on

修改后的mysql服務的啟動命令有兩種為

[root@guohaien mysql]# service mysqld start
[root@guohaien mysql]# systemctl start mysqld

其中關閉和重啟服務只需要將start改為stop和restart即可

1.8、修改登錄密碼

[root@guohaien mysql]# ./bin/mysqladmin -u root -p password
Enter password: 
New password: 
Confirm new password: 
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

先輸入原始密碼,在輸入兩次新密碼(其中原始密碼即為上面生成的那個隨機密碼)

也可以使用如下命令修改密碼

1 [root@guohaien mysql]# ./bin/mysqladmin -uroot -p'原始密碼' password '新密碼'

設置完成后就能使用新密碼登錄數據庫

 1 [root@guohaien mysql]# ./bin/mysql -u root -p
 2 Enter password: 
 3 Welcome to the MySQL monitor.  Commands end with ; or \g.
 4 Your MySQL connection id is 4
 5 Server version: 5.7.25 MySQL Community Server (GPL)
 6 
 7 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
 8 
 9 Oracle is a registered trademark of Oracle Corporation and/or its
10 affiliates. Other names may be trademarks of their respective
11 owners.
12 
13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14 
15 mysql> 

可以給登錄服務創建一個軟鏈接

[root@guohaien mysql]# ln -s /soft/mysql/bin/mysql /usr/bin/mysql

這樣就能將上面的./bin/mysql 替換為mysql

1.9、mysql遠程授權

目前只能在該服務器上登錄mysql,遠程並不能訪問,登錄mysql后執行如下sql

mysql> grant all privileges on *.* to 'root'@'%' identified by '密碼';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

以上即為mysql5.7在centos7.6 64位系統下的安裝及配置

2.1、關閉mysql服務

1 [root@guohaien /]# service mysqld stop

2.2、檢查是否有rpm包,若是沒有用rpm安裝過mysql,應該就沒有,若是有的話就要刪除

檢查的命令

1 [root@guohaien /]# rpm -qa|grep -i mysql

刪除的命令

1 [root@guohaien /]# rpm -e 包名

有時候會有依賴無法刪除,可以用命令

1 root@guohaien /]# rpm -e --nodeps 包名

2.3、檢查是否有mysql相關目錄或文件,若有則刪除

檢查的命令

1 [root@guohaien /]# find / -name mysql

刪除的命令

1 [root@guohaien /]# rm -rf 目錄或文件

2.4、刪除mysql用戶和分組

刪除用戶命令

1 [root@guohaien /]# userdel mysql

刪除分組命令

1 [root@guohaien /]# groupdel mysql

卸載完成后就可以開始安裝mysql


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM