1. 程式人生 > >Linux上MySQL搭建

Linux上MySQL搭建

安裝包

        官網下載,或者這裡

Linux OS版本

        CentOS-6.8-x86_64,下載地址

------------------

虛擬機器IP:192.168.160.130

-------------------

1:下載

        下載安裝包,mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz。

         並把安裝包放到Linux的/usr/local/software目錄。

         PS、這個目錄純屬個人習慣。

2:解壓

 cd /usr/local/software/
 tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

3:修改mysql資料夾名字

 mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql

4:檢查庫檔案是否有刪除,若有便刪除(linux系統自帶的)


[[email protected] software]# rpm -qa | grep mysql            註釋:檢查檔案
mysql-libs-5.1.73-7.el6.x86_64
[[email protected] software]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64         註釋:刪除檔案
[[email protected] software]# rpm -qa | grep mysql            註釋:再次檢查
[[email protected]
software]#
5:檢查mysql組和使用者是否存在,如無建立

        檢查:

[[email protected] software]# cat /etc/group | grep mysql
[[email protected] software]# cat /etc/passwd |grep mysql
[[email protected] software]#

        建立:

[[email protected] software]# groupadd mysql
[[email protected] software]# useradd -r -g mysql mysql
[[email protected] software]#

        再次檢查

[[email protected] software]# cat /etc/group | grep mysql
mysql:x:502:
[[email protected] software]# cat /etc/passwd |grep mysql
mysql:x:496:502::/home/mysql:/bin/bash
[[email protected] software]#

6:在mysql下新增data目錄

[[email protected] software]# cd /usr/local/software/mysql
[[email protected] mysql]# mkdir data
[[email protected] mysql]# ll
total 56
drwxr-xr-x.  2 root root   4096 Jun 25 19:43 bin
-rw-r--r--.  1 7161 31415 17987 Sep 13  2017 COPYING
drwxr-xr-x.  2 root root   4096 Jun 25 19:57 data
drwxr-xr-x.  2 root root   4096 Jun 25 19:43 docs
drwxr-xr-x.  3 root root   4096 Jun 25 19:42 include
drwxr-xr-x.  5 root root   4096 Jun 25 19:43 lib
drwxr-xr-x.  4 root root   4096 Jun 25 19:43 man
-rw-r--r--.  1 7161 31415  2478 Sep 13  2017 README
drwxr-xr-x. 28 root root   4096 Jun 25 19:43 share
drwxr-xr-x.  2 root root   4096 Jun 25 19:43 support-files
[[email protected] mysql]# 

7:更改mysql目錄下所有的目錄及資料夾所屬組合使用者

[[email protected] mysql]# cd /usr/local/software/
[[email protected] software]# chown -R mysql mysql/
[[email protected] software]# chgrp -R mysql mysql/
[[email protected] software]# cd mysql
[[email protected] mysql]# ls -l
total 56
drwxr-xr-x.  2 mysql mysql  4096 Jun 25 19:43 bin
-rw-r--r--.  1 mysql mysql 17987 Sep 13  2017 COPYING
drwxr-xr-x.  2 mysql mysql  4096 Jun 25 19:57 data
drwxr-xr-x.  2 mysql mysql  4096 Jun 25 19:43 docs
drwxr-xr-x.  3 mysql mysql  4096 Jun 25 19:42 include
drwxr-xr-x.  5 mysql mysql  4096 Jun 25 19:43 lib
drwxr-xr-x.  4 mysql mysql  4096 Jun 25 19:43 man
-rw-r--r--.  1 mysql mysql  2478 Sep 13  2017 README
drwxr-xr-x. 28 mysql mysql  4096 Jun 25 19:43 share
drwxr-xr-x.  2 mysql mysql  4096 Jun 25 19:43 support-files
[[email protected] mysql]# 

8:修改mysql.server檔案

        mysql.server檔案位置:/usr/local/software/mysql/support-files

        修改相關配置

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

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

PS、這個地方。回顧頭來想想為什麼要改名為什麼要建data資料夾,以及我安裝位置。

        如果不修改,則預設的位置為/usr/local/mysql和/usr/local/mysql/data

9:安裝和初始化資料庫

[[email protected] mysql]# cd /usr/local/software/mysql/bin/
[[email protected] bin]# ./mysqld  --initialize --user=mysql --basedir=/usr/local/software/mysql/ --datadir=/usr/local/software/mysql/data/
2018-06-26T03:20:45.288071Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-06-26T03:20:45.714481Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-06-26T03:20:45.921467Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-06-26T03:20:46.034842Z 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: ea6d4b60-78ef-11e8-8c12-000c295f07f7.
2018-06-26T03:20:46.036718Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-06-26T03:20:46.037855Z 1 [Note] A temporary password is generated for [email protected]: sfPqTk1sae,c
[[email protected] bin]#
PS、這一步可以不在第8步之後。


10配置my.cnf

接下來進入/usr/local/software/mysql/support-files/目錄下
檢視是否存在my-default.cnf檔案,如果存在直接copy到/etc/my.cnf檔案中

cp /usr/local/software/mysql/support-files/my-medium.cnf /etc/my.cnf

如果不存在my-default.cnf檔案,則在/etc/目錄下建立my.cnf,並寫入以下內容

[mysql]
basedir=/usr/local/software/mysql/
datadir=/usr/local/software/mysql/data/
11:啟動服務?
[[email protected] bin]# pwd
/usr/local/software/mysql/bin
[[email protected] bin]# ./mysqld_safe --user=mysql &
[1] 5149
[[email protected] bin]# Logging to '/usr/local/software/mysql/data/localhost.localdomain.err'.
2018-06-26T03:34:20.900325Z mysqld_safe Starting mysqld daemon with databases from /usr/local/software/mysql/data

[[email protected] bin]# 
12:將mysqld服務加入開機自啟動項。

        將{mysql}/ support-files/mysql.server 拷貝為/etc/init.d/mysql並設定執行許可權,這樣就可以使用service mysql命令啟動/停止服務, 否則就只能使用{mysql}/bin/mysqld_safe &命令來啟動服務 。
        還需要把mysql.server中basedir的相關路徑,改為自定義的路徑,預設路徑是/usr/local/mysql。(第八步)

[[email protected] bin]# cd /usr/local/software/mysql/support-files/
[[email protected] support-files]# cp mysql.server /etc/init.d/mysql
[[email protected] support-files]#  chmod +x /etc/init.d/mysql
[[email protected] support-files]# chkconfig --add mysql
[[email protected] support-files]# chkconfig --list mysql
mysql          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[[email protected] support-files]#
13:啟動服務
[[email protected] support-files]# cd /usr/local/software/mysql/bin/
[[email protected] bin]# service mysql start
Starting MySQL SUCCESS! 
[[email protected] bin]# 2018-06-26T03:41:44.745534Z mysqld_safe A mysqld process already exists

[[email protected] bin]#
14:登入mysql
[[email protected] bin]# pwd
/usr/local/software/mysql/bin
[[email protected] bin]# ./mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20

Copyright (c) 2000, 2017, 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>
15:設定密碼
mysql>  set password=password("root");
Query OK, 0 rows affected, 1 warning (0.00 sec)
注意不要使用單引號,為什麼?你自己試試就知道了

16:  設定遠端登入許可權

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

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

mysql>  quit
Bye
[[email protected] bin]#

參考資料:

        https://blog.csdn.net/zhou920786312/article/details/77750604