1. 程式人生 > >centos7安裝mysql5.7並設定開機自啟動詳細步驟

centos7安裝mysql5.7並設定開機自啟動詳細步驟

1.下載mysql5.7版本

[[email protected] home]#wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
2.解除安裝系統自帶的Mariadb
Centos7將預設資料庫mysql替換成了Mariadb,如果想繼續使用mysql 需要解除安裝Mariadb 再安裝mysql
rpm -qa|grep mariadb
rpm -e --nodeps 檔名 (上面指令查出的所有檔案)
[[email protected] home]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
[
[email protected]
home]# rpm -e --nodeps mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

3.刪除etc目錄下的my.cnf檔案

[[email protected] home]# rm -rf /etc/my.cnf
4.建立mysql使用者組和mysql使用者,並加入mysql組
[[email protected] home]# groupadd mysql
[[email protected] home]# useradd -r -g mysql mysql
5.將下載的壓縮包放到 /usr/local/ 目錄下(通過mv 要移動的檔案  /usr/local/)
[
[email protected]
home]# mv mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz /usr/local/
6.進入/usr/local/下,解壓安裝包
[[email protected] home]# cd /usr/local/
[[email protected] local]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz 
7. 將解壓好的資料夾重新命名為mysql
[[email protected] local]# mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql
8.在 etc 下新建配置檔案my.cnf,並在該檔案中新增以下配置程式碼:
[[email protected] ~]# vi /etc/my.cnf 
[mysql]
# 設定mysql客戶端預設字符集
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

[mysqld]
skip-name-resolve
#設定3306埠
port = 3306
socket=/var/lib/mysql/mysql.sock
# 設定mysql的安裝目錄
basedir=/usr/local/mysql
# 設定mysql資料庫的資料的存放目錄
datadir=/home/local/mysql/data
# 允許最大連線數
max_connections=500
# 服務端使用的字符集預設為8位元編碼的latin1字符集
character-set-server=utf-8
# 建立新表時將使用的預設儲存引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M

9.建立上面配置檔案需要用到的目錄,並給用到的目錄修改擁有者為mysql使用者

[[email protected] local]# mkdir /var/lib/mysql
[[email protected] local]# mkdir /home/local/mysql/data
[[email protected] local]# chown -R mysql:mysql /home/local/mysql/data
[[email protected] local]# chown -R mysql:mysql /var/lib/mysql
[[email protected] local]# chown -R mysql:mysql /usr/local/mysql
10.配置環境變數
[[email protected] bin]# vi ~/.bash_profile

修改PATH,增加mysql的bin目錄:
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

11.執行下面的命令是修改的內容立即生效:

[[email protected] bin]# source ~/.bash_profile

12.授予/etc/my.cnf最大許可權

[email protected] local]# chown 777 /etc/my.cnf
13.設定開機自啟動服務控制指令碼:(將{mysql}/ support-files/mysql.server 拷貝為/etc/init.d/mysqld並設定執行許可權)
[[email protected] mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[[email protected] mysql]# chmod +x /etc/rc.d/init.d/mysqld
[[email protected] mysql]# chkconfig --add mysqld
[[email protected] mysql]# chkconfig --list mysqld
結果顯示:

mysqld          0:關    1:關    2:開    3:開    4:開    5:開    6:關

表明mysqld服務已經生效,在2、3、4、5執行級別隨系統啟動而自動啟動,以後可以使用systemctl 命令控制mysql的啟動和停止。

13.初始化資料庫
[[email protected] mysql]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/home/local/mysql/data/
14.啟動資料庫
[[email protected] mysql]# systemctl start mysqld
15.初次安裝執行安全配置嚮導
[[email protected] bin]# mysql_secure_installation
 a)為root使用者設定密碼
  b)刪除匿名賬號
  c)取消root使用者遠端登入
  d)刪除test庫和對test庫的訪問許可權
  e)重新整理授權表使修改生效
具體選項如下:
Securing the MySQL server deployment.
Enter password for user root: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

16.登入mysql,修改遠端訪問許可權

[[email protected] /]# mysql -uroot -p
mysql> use mysql;
Database changed
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)