1. 程式人生 > >CentOS7下安裝mysql免安裝版(mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz)

CentOS7下安裝mysql免安裝版(mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz)

1.從mysql官網下載二進位制安裝包(https://dev.mysql.com/downloads/mysql/)

2.檢查是否已經安裝了mysql或者是MariaDB,如果已經安裝,則先把安裝的解除安裝

[[email protected] software]# rpm -qa | grep mysql
[[email protected] software]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[[email protected] software]# yum -y remove mariadb-libs-5.5.56-2.el7.x86_64
3.通過winSCP或者是Xftp等工具將安裝包上傳到CentOS上,並解壓
[[email protected] software]# ll
總用量 629816
drwxr-xr-x. 9 root root       129 12月 25 16:49 mysql-5.7.24-linux-glibc2.12-x86_64
-rw-r--r--. 1 root root 644930593 12月 24 17:15 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[[email protected] software]# 
4.重新命名mysql安裝目錄
[[email protected] software]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
[[email protected] software]# ll
總用量 629816
drwxr-xr-x. 9 root root       129 12月 25 16:49 mysql
-rw-r--r--. 1 root root 644930593 12月 24 17:15 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[[email protected]
software]#
5.新增新的使用者組和新的使用者,用來管理mysql,提高安全性(非必要的,不過mysql官網安裝步驟推薦這樣做,這個步驟可以省略)
[[email protected] software]# groupadd mysql
[[email protected] software]# useradd -r -g mysql -s /bin/false mysql
[[email protected] software]# 
6.新建mysql的data目錄
[[email protected] software]# cd mysql
[[email protected] mysql]# mkdir data
[[email protected] mysql]# ll
總用量 36
drwxr-xr-x.  2 root root   4096 12月 25 16:49 bin
-rw-r--r--.  1 7161 31415 17987 10月  4 13:48 COPYING
drwxr-xr-x.  2 root root      6 12月 25 16:57 data
drwxr-xr-x.  2 root root     55 12月 25 16:49 docs
drwxr-xr-x.  3 root root   4096 12月 25 16:48 include
drwxr-xr-x.  5 root root    230 12月 25 16:49 lib
drwxr-xr-x.  4 root root     30 12月 25 16:49 man
-rw-r--r--.  1 7161 31415  2478 10月  4 13:48 README
drwxr-xr-x. 28 root root   4096 12月 25 16:49 share
drwxr-xr-x.  2 root root     90 12月 25 16:49 support-files
[[email protected] mysql]#
7.修改mysql目錄使用者為剛剛新建的mysql組中的mysql使用者
[[email protected] mysql]# chown -R mysql:mysql ./
8.初始化安裝mysql資料庫
[[email protected] mysql]# ./bin/mysqld --user=mysql --basedir=/usr/local/software/mysql --datadir=/usr/local/software/mysql/data --initialize
2018-12-25T09:00:17.116437Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-12-25T09:00:21.961838Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-12-25T09:00:22.747386Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-12-25T09:00:22.860663Z 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: 8324a8be-0823-11e9-b13d-000c29ca91c6.
2018-12-25T09:00:22.862806Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-12-25T09:00:22.867377Z 1 [Note] A temporary password is generated for [email protected]: Q&ZsMY#sV3du
[[email protected] mysql]#
9.修改my.conf配置檔案,通過mysql官網可以知道,從版本5.7.18開始,mysql免安裝版二進位制包中就不包含該檔案了,即不需要my.conf檔案也可以正常執行;my.conf檔案中配置的選項會在命令列啟動mysql的時候作為引數進行啟動,為了後面搭建mysql主從環境方便,下面可以添加了一個簡單的my.conf檔案作為例項(如果只是單純的搭建一個mysql例項,可以直接忽略此步驟),使用vim /etc/my.conf命令,如果在該目錄上不存在則會新建
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/software/mysql
datadir=/usr/local/software/mysql/data
socket=/usr/local/software/mysql/mysql.sock
#設定忽略大小寫(簡單來說就是sql語句是否嚴格),預設庫名錶名儲存為小寫, 不區分大小寫
lower_case_table_names = 1
# 開啟ip繫結
bind-address = 0.0.0.0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/software/mysql/data/mysqld.pid
#指定客戶端連線mysql時的socket通訊檔案路徑
[client]
socket=/usr/local/software/mysql/mysql.sock
default-character-set=utf8
10.將mysql新增至開機啟動
[[email protected] mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# 

修改mysqld,使用vim /etc/init.d/mysqld 命令 修改以下程式碼部分

basedir=/usr/local/software/mysql
datadir=/usr/local/software/mysql/data

設定開機啟動

[[email protected] mysql]# chkconfig --add mysqld
11.到這一步,mysql已經算是安裝完畢了,接下來使用以下命令啟動mysql
[[email protected]alhost mysql]# service mysqld start
Starting MySQL.Logging to '/usr/local/software/mysql/data/localhost.localdomain.err'.
. SUCCESS! 
[[email protected] mysql]#
12.為了可以在任意目錄上都可以使用mysql命令登入mysql,將mysql安裝目錄配置到環境變數中,在/etc/profile檔案的末尾新增以下程式碼

export PATH=$PATH:/usr/local/software/mysql/bin

使配置檔案的配置立即生效

[[email protected] mysql]# source /etc/profile
13.重啟mysql服務,並且使用mysql的root使用者登入mysql,密碼在第8步最後一行有
[[email protected] mysql]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[[email protected] mysql]# 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.24

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> 
14.修改root使用者的密碼為root,並且重新整理
mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
15.此時mysql資料庫只能在本機上使用mysql命令進行登入,還無法使用navicat等資料庫視覺化工具進行遠端登入,下面設定允許root使用者遠端連線資料庫
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set user.Host='%' where user.User='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[[email protected] mysql]# 

為了方便試驗,此處已事先使用 systemctl stop firewalld 命令將防火牆關閉,在實際使用中,只需要開放資料庫執行的3306埠即可,結果如下: