1. 程式人生 > >Centos7 tar包安裝mysql5.7

Centos7 tar包安裝mysql5.7

1、由於線上安裝受制於網路環境,所以選擇tar包編譯安裝。

2、上傳到centos

3、檢查你所用的Linux下有沒有安裝過mysql,沒有解除安裝乾淨

rpm -qa|grep -i mysql

如果存在,則需要先解除安裝掉,不然會出現覆蓋錯誤。

yum remove  mysql mysql-server mysql-libs mysql-server;

find / -name mysql 將找到的相關東西delete掉;

rpm -qa|grep mysql(查詢出來的東東yum remove)

4、建立mysql的使用者組/使用者, data目錄及其使用者目錄

userdel mysql                                          

# 刪除使用者

groupdel mysql                                          # 刪除使用者組名

mkdir /usr/local/mysql                                  # mysql的預設安裝路徑,建議不要更換,如果更換後續需要更新配置

mkdir /usr/local/mysql/data                             # mysql資料夾下建立資料夾data 

groupadd mysql                                          #

建立一個名為mysql的使用者組      

useradd -g mysql -d /usr/local/mysql mysql              # 在使用者組下建立使用者

5、解壓縮並轉移

# tar -xzvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz             # 解壓檔案

# cd mysql-5.7.17-linux-glibc2.5-x86_64                           # 進入

# mv * /usr/local/mysql/

6、編譯安裝

cd /usr/local/mysql/

./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize

https://static.oschina.net/uploads/space/2017/0412/113503_nmXK_196698.png

記錄上面成功安裝後的密碼,如上:hIE;k,h8gd#q,後續用到!

7、啟動mysql服務

./support-files/mysql.server start

https://static.oschina.net/uploads/space/2017/0412/113755_557o_196698.png

截至目前,證明mysql已執行成功!!!

8、配置mysql

#進入mysql的安裝目錄支援檔案目錄

cd /usr/local/mysql//support-files

# 拷貝配置檔案模板為新的mysql配置檔案,

cp my-default.cnf /etc/my.cnf

#設定編碼,可按需修改新的配置檔案選項, 不修改配置選項, mysql則按預設配置引數執行.

#如下是我修改配置檔案/etc/my.cnf 設定編碼為utf8以防亂碼

vim /etc/my.cnf

[mysqld]

basedir = /usr/local/mysql

datadir = /usr/local/mysql/data

character_set_server=utf8

init_connect='SET NAMES utf8'

[client]

default-character-set=utf8

此處需要註釋掉##sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

否則後續登陸時會報異常:

mysql: [ERROR] unknown variable 'sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES'

9、配置mysql服務開機自動啟動

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld      # 拷貝啟動檔案到/etc/init.d/下並重命令為mysqld

# chmod 755 /etc/init.d/mysqld                                      # 增加執行許可權

# chkconfig --list mysqld                                           # 檢查自啟動項列表中沒有mysqld這個,

# chkconfig --add mysqld                                            # 如果沒有就新增mysqld

# chkconfig mysqld on                                               # 用這個命令設定開機啟動:

10mysql服務的啟動/重啟/停止

# service mysqld start                                               # 啟動服務

# service mysqld restart                                             # 重啟服務

# service mysqld stop                                                # 停止服務

11、修改mysql使用者root的密碼

mysql -u root -p

輸入上邊自動生成的密碼,進入mysql環境

Mysql -u root -p

-bash : mysql :command not found

原因:這是由於系統預設會查詢/usr/bin下的命令,如果這個命令不在這個目錄下,當然會找不到命令,我們需要做的就是對映一個連結到/usr/bin目錄下,相當於建立一個連結檔案。


首先得知道mysql命令完整路徑,比如我的Linuxmysql的路徑是:/usr/local/mysql/bin/mysql,我們則可以這樣執行命令:

 /usr/local/mysql/bin/mysql  -uroot這樣執行命令,或者建立一個軟連結

 ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

再次嘗試就可以進入了

https://static.oschina.net/uploads/space/2017/0412/150832_n8PT_196698.png

mysql> SET PASSWORD = PASSWORD('123456');             # PASSWORD()裡面的123456 是設定的新密碼,可以設定成你的密碼

Query OK, 0 rows affected, 1 warning (0.00 sec)

Rows matched: 2 Changed: 0 Warnings: 1

mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;

Query OK, 0 rows affected, 1 warning (0.00 sec)

Rows matched: 2 Changed: 0 Warnings: 1

mysql> flush privileges;

Query OK, 0 rows affected, 1 warning (0.00 sec)

Rows matched: 2 Changed: 0 Warnings: 1

12、更改一些編碼

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> \s

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

mysql  Ver 14.14 Distrib 5.7.17, for linux-glibc2.5 (x86_64) using  EditLine wrapper

Connection id:           5

Current database:        mysql

Current user:             root@localhost

SSL:                      Not in use

Current pager:           stdout

Using outfile:           ''

Using delimiter: ;

Server version:          5.7.17

Protocol version:        10

Connection:               Localhost via UNIX socket

Server characterset:     utf8

Db     characterset:      latin1

Client characterset:     utf8

Conn.  characterset:     utf8

UNIX socket:              /tmp/mysql.sock

Uptime:                   5 min 8 sec

Threads: 1  Questions: 44  Slow queries: 0  Opens: 136  Flush tables: 1  Open tables: 129  Queries per second avg: 0.142

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

mysql> show variables like 'character%';

+--------------------------+----------------------------------+

| Variable_name            | Value                            |

+--------------------------+----------------------------------+

| character_set_client     | utf8                             |

| character_set_connection | utf8                             |

| character_set_database   | latin1                           |

| character_set_filesystem | binary                           |

| character_set_results    | utf8                             |

| character_set_server     | utf8                             |

| character_set_system     | utf8                             |

| character_sets_dir       | /usr/local/mysql/share/charsets/ |

+--------------------------+----------------------------------+

8 rows in set (0.01 sec)

mysql>  SET character_set_database = utf8;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show variables like 'character%';

+--------------------------+----------------------------------+

| Variable_name            | Value                            |

+--------------------------+----------------------------------+

| character_set_client     | utf8                             |

| character_set_connection | utf8                             |

| character_set_database   | utf8                             |

| character_set_filesystem | binary                           |

| character_set_results    | utf8                             |

| character_set_server     | utf8                             |

| character_set_system     | utf8                             |

| character_sets_dir       | /usr/local/mysql/share/charsets/ |

+--------------------------+----------------------------------+

8 rows in set (0.00 sec)

mysql> \s

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

mysql  Ver 14.14 Distrib 5.7.17, for linux-glibc2.5 (x86_64) using  EditLine wrapper

Connection id:           5

Current database:        mysql

Current user:             root@localhost

SSL:                      Not in use

Current pager:           stdout

Using outfile:           ''

Using delimiter: ;

Server version:          5.7.17

Protocol version:        10

Connection:               Localhost via UNIX socket

Server characterset:     utf8

Db     characterset:      utf8

Client characterset:     utf8

Conn.  characterset:     utf8

UNIX socket:              /tmp/mysql.sock

Uptime:                   6 min 20 sec

Threads: 1  Questions: 50  Slow queries: 0  Opens: 137  Flush tables: 1  Open tables: 130  Queries per second avg: 0.131

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

13mysql遠端授權

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';

Query OK, 0 rows affected, 1 warning (0.00 sec)

14、驗證,可能需要關閉防火牆

Service iptables stop

https://static.oschina.net/uploads/space/2017/0412/152259_8gkm_196698.png