1. 程式人生 > >mysql-5.7.18版本(二進制包安裝)-自定義安裝路徑

mysql-5.7.18版本(二進制包安裝)-自定義安裝路徑

mysql linux lnmp

mysql-5.7.18版本(二進制包安裝)-自定義安裝路徑

安裝路徑:/application/mysql-5.7.18

1.前期準備

mysql依賴

libaio

yum install -y libaio

創建用戶mysql,以該用戶的身份執行mysql

useradd -s /bin/false -M mysql

下載mysql二進制包並解壓

cd /tools

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

tar -zxf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /application/

切換到/application目錄,將mysql文件夾名改短,給mysql目錄做一個軟鏈接

cd /application/

mv mysql-5.7.18-linux-glibc2.5-x86_64/ mysql-5.7.18

ln -s mysql-5.7.18/ mysql

在mysql目錄下創建mysql-files,該文件夾權限為750,遞歸設置mysql目錄的所屬組和所屬用戶

mkdir mysql/mysql-files

chmod 750 mysql/mysql-files

chown -R mysql:mysql mysql-5.7.18/

2.mysql目錄內操作

cd mysql

初始化數據庫

會在mysql目錄內生成一個

data目錄,存放數據庫的目錄

bin/mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data

返回結果最後一行的末尾有隨機密碼,我的記下來:wa0I:1w?V--a

2017-04-28T02:49:00.853710Z 1 [Note] A temporary password is generated for [email protected]: wa0I:1w?V--a

想設置默認密碼為空則將--initialize選項替換為--initialize-insecure選項

bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data

安裝ssl

bin/mysql_ssl_rsa_setup --datadir /application/mysql/data/

指定data目錄的路徑

更改所屬用戶和組

chown -R root .

chown -R mysql data mysql-files

除了mysql目錄下的data目錄和mysql-files目錄所屬用戶不變,其他所有文件的所屬用戶改為root

修改配置文件

sed -i ‘s/^datadir=\/var\/lib\/mysql/datadir=\/application\/mysql\/data/g‘ /etc/my.cnf

sed -i ‘s/^socket=\/var\/lib\/mysql\/mysql.sock/socket=\/tmp\/mysql.sock/g‘ /etc/my.cnf

sed -i ‘s/^log-error=\/var\/log\/mariadb\/mariadb.log/log-error=\/application\/mysql\/data\/err.log/g‘ /etc/my.cnf

sed -i ‘s/^pid-file=\/var\/run\/mariadb\/mariadb.pid/pid-file=\/application\/mysql\/data\/mysql.pid/g‘ /etc/my.cnf

等價於:

vi /etc/my.cnf

datadir=/application/mysql/data

socket=/tmp/mysql.sock

log-error=/application/mysql/data/err.log

pid-file=/application/mysql/data/mysql.pid

/etc/my.cnf Content:

[mysqld]

datadir=/application/mysql/data

socket=/tmp/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]

log-error=/application/mysql/data/err.log

pid-file=/application/mysql/data/mysql.pid

#

# include all files from the config directory

#

!includedir /etc/my.cnf.d

拷貝啟動程序

cp support-files/mysql.server /etc/init.d/mysql

mysql的啟動程序拷貝到/etc/init.d/目錄下,以便啟動程序

編輯啟動文件,配置啟動目錄

方法一:

思路是給配置文件提供的變量進行賦值。較麻煩。

sed -i ‘s/^basedir=/basedir=\/application\/mysql/g‘ /etc/init.d/mysql

sed -i ‘s/^datadir=/datadir=\/application\/mysql\/data/g‘ /etc/init.d/mysql

sed -i ‘s/^mysqld_pid_file_path=/mysqld_pid_file_path=\/application\/mysql\/data\/mysql.pid/g‘ /etc/init.d/mysql

等價於將4546兩行

basedir=

datadir=

mysqld_pid_file_path=

替換成

basedir=/application/mysql

datadir=/application/mysql/data

mysqld_pid_file_path=/application/mysql/data/mysql.pid

方法二(推薦):

思路是將腳本的默認地址(/usr/local/mysql)直接替換成自定義路徑(/application/mysql),便不用給變量賦值

sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /etc/init.d/mysql

到這裏mysql安裝完成可以正常啟動

3.後期結尾

命令創建軟鏈接

mysql命令創建軟鏈接到環境變量的目錄,使用戶可以在變量找到相應的命令

ln -s /application/mysql/bin/* /usr/local/sbin

登錄mysql

[[email protected] mysql]# mysql -u root -p

Enter password: #輸入之前保存的隨機密碼drRR0<ZuDgX_

...

mysql> #成功登陸到mysql控制臺

修改密碼sql語句

mysql> alter user [email protected] identified by ‘NewPassWord‘;

#將密碼修改為NewPassWord

Query OK, 0 rows affected (0.01 sec) #修改成功

鍵入quit或Ctrl + d退出

mysql> quit

Bye

[[email protected] mysql]#

or

mysql> ^DBye

[[email protected] mysql]#

4.常用命令

進入mysql

mysql -u root -p

啟動mysql

service mysql start

停止mysql

service mysql stop

重啟mysql

service mysql restart


本文出自 “這就是你的選擇 | CzlunSu” 博客,請務必保留此出處http://czlun.blog.51cto.com/9876583/1945877

mysql-5.7.18版本(二進制包安裝)-自定義安裝路徑