LNMP之二進制MySQL的安裝

通常企業環境中的MySQL安裝使用兩種方法

1、MySQL二進制安裝包       mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz

2、MySQL源碼包安裝            mysql-5.6.33.tar.gz

區別:

二進制包安裝方便不需要編譯,直接解壓就可以使用。但是二進制包要比源碼包大。下面介紹一下MySQL二進制安裝包的安裝方法。(寫的不好的地方大家多多指教)

1、添加mysql用戶

useradd mysql -s /sbin/nologin -M

參數說明:-s /sbin/nologin  表示禁止該用戶登錄,

                -M  不用創建家目錄。

2、創建一個目錄    mkdir /application

解釋:二進制MySQL安裝包默認安裝在/usr/local下,這個目錄可以不創建,這個只是個人習慣。運維工作人員的習慣就是走老路,避免新的坑。

3、解壓安裝包

tar -xvf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz

4、將解壓文件重命名

mv mysql-5.6.33-linux-glibc2.5-x86_64  /application/mysql-5.6.33

5、建立一個軟連接

ln -s /application/mysql-5.6.33   /application/mysql

6、修改MySQL的所屬主、所屬組

chown -R mysql.mysql /application/mysql/

7、初始化數據庫

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql  --datadir=/application/mysql/data/  --user=mysql

8、拷貝MySQL啓動腳本

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

9、給啓動腳本加執行權限

chmod +x /etc/init.d/mysqld

10、二進制安裝包默認安裝路徑是/usr/local,所以將腳本中的/usr/local替換成/application

11、啓動MySQL

/etc/init.d/mysqld start

Starting MySQL........................................     [確定]

12、檢查MySQL是否真正啓動

netstat  -nplt | grep 3306

tcp        0      0 :::3306                     :::*                        LISTEN      15064/mysqld

13、如果發現MySQL端口沒有啓動,請看/application/mysql/data/***.err日誌,根據報錯信息在調試

vim /application/mysql/data/***.err

14、設置MySQL開機自啓動

chkconfig --add mysqld
chkconfig mysqld on

15、設置全局變量

vim /etc/profile

在文件最後加入

export PATH=/application/mysql/bin:$PATH

16、登錄MySQL,這個時候不需要密碼也可以登錄,並且是root用戶

[root@Jhyeliu data]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 1
Server version: 5.6.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>

17、如果出現下面這個情況,重新初始化數據。

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

1)、刪除初始化的data目錄    rm -rf /application/mysql/data

2)、重新初始化

18、MySQL安全加固

爲root用戶設置密碼

mysqladmin -u root password 'password'

19、清理無用的庫

drop database test ;

20、清理無用的用戶

select user,host from mysql.user;
+------+-----------+
| user | host    |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1    |
|    | jhyeliu  |
| root | jhyeliu  |
|    | localhost |
| root | localhost |
+------+-----------+
drop user "root"@"::1";

Query OK, 0 rows affected (0.05 sec)

drop user ""@"localhost";

Query OK, 0 rows affected (0.00 sec) 

drop user ""@"jhyeliu";

Query OK, 0 rows affected (0.00 sec)

drop user "root"@"jhyeliu";

Query OK, 0 rows affected (0.00 sec)

select user,host from mysql.user;
+------+-----------+
| user | host    |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
flush privileges;

至此MySQL數據庫就算基本完成了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章