64位centos6.7安裝mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz

1. 從官網下載 mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz

經測試, 本文還適用於如下版本:
mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz
mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz

官網: http://dev.mysql.com/downloads/mysql/

這裏寫圖片描述

mysql搜狐鏡像站:
http://mirrors.sohu.com/mysql/

# wget -c http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz

2. 創建mysql的用戶組/用戶, data目錄及其用戶目錄

# groupadd mysql
# useradd -g mysql -d /home/mysql mysql
# mkdir /home/mysql/data

3. 解壓安裝包並將解壓包裏的內容拷貝到mysql的安裝目錄/home/mysql

# tar -xzvf mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz
# cd mysql-5.7.9-linux-glibc2.5-x86_64 
# mv * /home/mysql

4. 初始化mysql數據庫

#  sudo apt-get install  libaio1 libaio-dev
# cd /home/mysql
# ./bin/mysql_install_db --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data

2015-11-10 02:09:17 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2015-11-10 02:09:23 [WARNING] The bootstrap log isn't empty:
2015-11-10 02:09:23 [WARNING] 2015-11-10T10:09:18.114182Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2015-11-10T10:09:18.129343Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2015-11-10T10:09:18.129408Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)

p.s. : mysql5.7新特性:由上面可以看出, mysql_install_db 已經不再推薦使用了,建議改成 mysqld --initialize 完成實例初始化。

# ./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize

改用 mysqld --initialize 後,如果 datadir 指向的目標目錄下已經有數據文件, 會出現如下提示:

# ./mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
2016-04-08T01:46:53.153530Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-04-08T01:46:53.155856Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2016-04-08T01:46:53.155879Z 0 [ERROR] Aborting

由上可知, 我們需要清空mysql的data目錄, 執行清空命令如下:

# cd /home/mysql/data
# rm -fr *

然後重新執行初始化命令如下:

# ./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
2016-04-08T01:47:57.556677Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-04-08T01:47:59.945537Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-04-08T01:48:00.333528Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-04-08T01:48:00.434908Z 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: ece26421-fd2b-11e5-a1e3-00163e001e5c.
2016-04-08T01:48:00.440125Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2016-04-08T01:48:00.440904Z 1 [Note] A temporary password is generated for root@localhost: **mjT,#x_5sW

牢記上面的隨機密碼, 如上**mjT,#x_5sW, 下面我們修改密碼時需要用到。

5. 檢測下是否能啓動mysql服務

# cd /home/mysql
# ./support-files/mysql.server start
Starting MySQL.. SUCCESS! 

若改用了/home/mysql爲mysql的安裝目錄basedir, 則在啓動服務時會出現如下錯誤:

# ./support-files/mysql.server start
./support-files/mysql.server: line 276: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

由上面可知mysql的tar.gz安裝包的默認安裝目錄爲/usr/local/mysql, 這時候我們需要修改/support-files/mysql.server文件的basedir和datadir目錄路徑爲我們環境所在的mysql的basedir和datadir路徑, 如下:

# vim support-files/mysql.server
--------------------------
...
basedir=/home/mysql
datadir=/home/mysql/data
...	
--------------------------
# ./support-files/mysql.server start
Starting MySQL.. SUCCESS! 

6. 創建軟鏈接

# ln -s /home/mysql/bin/mysql /usr/bin/mysql

7. 創建配置文件

將默認生成的my.cnf備份

# mv /etc/my.cnf /etc/my.cnf.bak

進入mysql的安裝目錄支持文件目錄

# cd /home/mysql/support-files

拷貝配置文件模板爲新的mysql配置文件,

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

可按需修改新的配置文件選項, 不修改配置選項, mysql則按默認配置參數運行.
如下是我修改配置文件/etc/my.cnf, 設置編碼爲utf8以防亂碼

# vim /etc/my.cnf

[mysqld]

basedir = /home/mysql
datadir = /home/mysql/data

character_set_server=utf8
init_connect='SET NAMES utf8'


[client]
default-character-set=utf8

8. 配置mysql服務開機自動啓動

拷貝啓動文件到/etc/init.d/下並重命令爲mysqld

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

增加執行權限

# chmod 755 /etc/init.d/mysqld

檢查自啓動項列表中沒有mysqld這個,如果沒有就添加mysqld

# chkconfig --list mysqld
# chkconfig --add mysqld

設置MySQL在345等級自動啓動

# chkconfig --level 345 mysqld on

或用這個命令設置開機啓動:

# chkconfig mysqld on

新版的linux發行版採用systemctl來管理服務, 則chkconfig命令不能使用, 通過systemctl來實現服務開機自啓動, 如下:

# systemctl enable mysqld
mysqld.service is not a native service, redirecting to systemd-sysv-install
Executing /lib/systemd/systemd-sysv-install enable mysqld

9. mysql服務的啓動/重啓/停止

啓動mysql服務

# service mysqld start

# systemctl start mysqld

重啓mysql服務

# service mysqld restart

# systemctl restart mysqld

停止mysql服務

# service mysqld stop

# systemctl stop mysqld

10. 初始化mysql用戶root的密碼

先將mysql服務停止

# service mysqld stop

進入mysql安裝目錄, 執行:

# cd /home/mysql
# ./bin/mysqld_safe --skip-grant-tables --skip-networking&
[1] 6225
[root@localhost mysql]# 151110 02:46:08 mysqld_safe Logging to '/home/mysql/data/localhost.localdomain.err'.
151110 02:46:08 mysqld_safe Starting mysqld daemon with databases from /home/mysql/data

另外打開一個終端(p.s. 如果是ssh連接登錄的, 另外創建一個ssh連接即可), 執行操作如下:

# mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> use mysql;
Database changed
mysql> UPDATE user SET password=PASSWORD('123456') WHERE user='root';
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql> update user set authentication_string = PASSWORD('123456') where user = 'root';
Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 1

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

mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.9, for linux-glibc2.5 (x86_64) using  EditLine wrapper

Connection id:		2
Current database:	mysql
Current user:		root@
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.9 MySQL Community Server (GPL)
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:			4 min 47 sec

Threads: 1  Questions: 43  Slow queries: 0  Opens: 127  Flush tables: 1  Open tables: 122  Queries per second avg: 0.149
--------------

mysql> exit;
Bye

到此, 設置完mysql用戶root的密碼且確保mysql編碼集是utf8, 注意上面, 新版本的mysql.user表裏的密碼字段是authentication_string

快捷鍵ctrl + c停止# ./bin/mysqld_safe ...命令, 重新啓動mysql服務, 用新密碼連接mysql:

# service mysqld start
Starting MySQL SUCCESS! 
[root@localhost bin]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.9

Copyright (c) 2000, 2015, 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> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql > exit;
Bye

咦?又要我改密碼, 我們通過mysqladmin來修改密碼, 先輸入原密碼, 再設置新密碼, 總算可以了吧!!!

# cd /home/mysql
# ./bin/mysqladmin -u root -p password
Enter password: 
New password: 
Confirm new password: 
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> 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> 

或直接:

# ./bin/mysqladmin -uroot -p'**mjT,#x_5sW' password '123456'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

其中, **mjT,#x_5sW就是我們在使用mysqld --initialize時牢記下的隨機密碼

11. mysql遠程授權

格式如下:

mysql> grant all [privileges] on db_name.table_name to 'username'@'host' identified by 'password';

示例如下:

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

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> 

或用

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

到此, 完成了mysql的安裝 及配置!!!

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