當前位置:
首頁 > 知識 > RedHat7上安裝MySQL5.7.16

RedHat7上安裝MySQL5.7.16

1、查看系統中是否已將安裝MySQL,如果安裝了,需要卸載。

[root@chenguo etc]# rpm -qa|grep -i mysql

2、創建用戶和組

[root@chenguo ~]# groupadd mysql
[root@chenguo ~]# useradd -r -g mysql -p root mysql

3、通過SSH將mysql安裝包放到/usr/local目錄下面,並進行解壓操作。

[chenguo@chenguo ~]$ cd /usr/local/
[chenguo@chenguo local]$ tar -xvf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar

解壓之後出現如下:

mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm
mysql-community-devel-5.7.16-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.16-1.el7.x86_64.rpm
mysql-community-libs-5.7.16-1.el7.x86_64.rpm
mysql-community-common-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.16-1.el7.x86_64.rpm
mysql-community-test-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.16-1.el7.x86_64.rpm
mysql-community-server-minimal-5.7.16-1.el7.x86_64.rpm
mysql-community-server-5.7.16-1.el7.x86_64.rpm
mysql-community-client-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-5.7.16-1.el7.x86_64.rpm

4、安裝時候會提示與已經安裝的RPM包有衝突,所以我們先卸載一些RPM包,要卸載哪些呢?我們要卸載的是包含有mariadb關鍵字的RPM包,執行命令:

[chenguo@chenguo local]$ rpm -qa|grep mariadb
mariadb-libs-5.5.41-2.el7_0.x86_64

使用命令卸載:

[chenguo@chenguo local]$ su -
[root@chenguo ~]# rpm -e mariadb-libs-5.5.41-2.el7_0.x86_64 --nodeps

或者使用:

[root@chenguo ~]# rpm -e --nodeps mariadb-libs-5.5.41-2.el7_0.x86_64

5、進行MySQL包安裝

[root@chenguo lib]# su -chenguo
[chenguo@chenguo lib]$ cd /usr/local/
[chenguo@chenguo local]$ rpm -ivh mysql-community-common-5.7.16-1.el7.x86_64.rpm
warning: mysql-community-common-5.7.16-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: can"t create transaction lock on /var/lib/rpm/.rpm.lock (Permission denied)

出現錯誤,改用root用戶進行安裝:

[chenguo@chenguo local]$ su -
[root@chenguo local]# rpm -ivh mysql-community-common-5.7.16-1.el7.x86_64.rpm
warning: mysql-community-common-5.7.16-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-common-5.7.16-1.e################################# [100%]
[root@chenguo local]# rpm -ivh mysql-community-libs-5.7.16-1.el7.x86_64.rpm
warning: mysql-community-libs-5.7.16-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-libs-5.7.16-1.el7################################# [100%]
[root@chenguo local]# rpm -ivh mysql-community-client-5.7.16-1.el7.x86_64.rpm
warning: mysql-community-client-5.7.16-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-client-5.7.16-1.er################################# [100%])
[root@chenguo local]# rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.16-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-server-5.7.16-1.e################################# [100%]

6、命令執行完畢後,mysql就安裝完成,下面需要做的就是對mysql 進行初始化

[root@chenguo bin]# cd /usr/local/bin
[root@chenguo bin]# mysqld --initialize --user=mysql
[root@chenguo bin]# mysqld --initialize-insecure --user=mysql
2016-11-18T08:46:09.910921Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-11-18T08:46:09.912342Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2016-11-18T08:46:09.912368Z 0 [ERROR] Aborting

解決辦法:

進入/var/lib/目錄中查看是否有mysql目錄,有的話進行刪除:

[root@chenguo bin]# cd /var/lib
[root@chenguo lib]# rm -rf mysql
[root@chenguo local]# cd /usr/local/bin/
[root@chenguo bin]# mysqld --initialize-insecure --user=mysql

7、啟動mysql服務

[root@chenguo lib]# systemctl start mysqld
Job for mysqld.service failed. See "systemctl status mysqld.service" and "journalctl -xn" for details.

啟動失敗解決辦法:

首先,查看/var/lib/mysql-files目錄是否存在:

[root@chenguo lib]# ls -ld /var/lib/mysql-files

如果不顯示任何信息就表示不存在,執行命令:

[root@chenguo lib]# mkdir /var/lib/mysql-files

這時,我們試著執行命令:

[root@chenguo lib]# systemctl start mysqld

如果還出現和下圖一樣的提示,我們執行另一條命令:

[root@chenguo lib]# chown -R mysql:mysql /var/lib/mysql

依然出錯。

查看日誌信息:

[root@chenguo ~]# journalctl |grep mysql

啟動失敗也有可能是SElinux導致,關閉SElinux功能:

vim /etc/selinux/config 修改文件永久關閉

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted

SELINUX=permissive #修改此參數為permissive

查看系統日誌:

[root@chenguo ~]# cd /var/log
[root@chenguo log]# cat mysqld.log

2016-11-22T02:51:32.860176Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
2016-11-22T02:51:32.860231Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
2016-11-22T02:51:32.860238Z 0 [ERROR] InnoDB: os_file_get_status failed on "./ibdata1". Can"t determine file permissions
2016-11-22T02:51:32.860246Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2016-11-22T02:51:33.471773Z 0 [ERROR] Plugin "InnoDB" init function returned error.
2016-11-22T02:51:33.471974Z 0 [ERROR] Plugin "InnoDB" registration as a STORAGE ENGINE failed.
2016-11-22T02:51:33.472096Z 0 [ERROR] Failed to initialize plugins.
2016-11-22T02:51:33.472245Z 0 [ERROR] Aborting

[root@chenguo bin]# ls -ld /usr/sbin/mysqld
-rwxr-xr-x. 1 root root 241273154 Sep 28 13:35 /usr/sbin/mysqld
[root@chenguo bin]# chown -R mysql:mysql /usr/sbin/mysqld

啟動之後依然報錯

還原許可權:

[root@chenguo bin]# chown -R root:root /usr/sbin/mysqld

在重啟了系統之後,使用:

[root@chenguo lib]# systemctl start mysqld

居然神奇的成功了,搞不懂。應該是最後的vim /etc/selinux/config 修改文件永久關閉,這個操作實現的

8、允許mysql遠程訪問

[root@chenguo ~]# mysql
mysql> use mysql;
mysql> select host, user from user;
mysql> update user set host = "%" where user = "root";
mysql> select host, user from user;

9、退出mysql

mysql> exit

10、查看和修改資料庫編碼

[root@chenguo ~]# mysql
mysql> use mysql;
mysql> select Host,user from user where User="root";
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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

設置資料庫的編碼:

mysql> exit
[root@chenguo ~]# vi /etc/my.cnf

在其中加入如下內容:

character-set-server=utf8
collation-server=utf8_general_ci
如果要修改其他的屬性可以繼續添加:
character-set-server=utf8
collation-server=utf8_general_ci
max_connections=1000
table_open_cache=6000
thread_cache_size=50
open_files_limit=8000
event_scheduler=ON

group_concat_max_len=999999999999

拷貝my.cnf文件內容:

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
collation-server=utf8_general_ci
max_connections=1000
table_open_cache=6000
thread_cache_size=50
open_files_limit=8000
event_scheduler=ON

group_concat_max_len=999999999999

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

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

修改配置後需要重啟mysql:

[root@chenguo ~]# systemctl stop mysqld
[root@chenguo ~]# systemctl restart mysqld

進入mysql,查看修改內容是否成功:

[root@chenguo ~]# mysql
mysql> use mysql
mysql> SHOW VARIABLES LIKE "event_scheduler";
mysql> SHOW VARIABLES LIKE "group_concat_max_len";

PS:當切換mysql的時候出現錯誤:

[root@chenguo ~]# mysql
ERROR 1045 (28000): Access denied for user "root"@"localhost" (using password: NO)

我的錯誤解決是直接將上面的第6步重新做一次

6、命令執行完畢後,mysql就安裝完成,下面需要做的就是對mysql 進行初始化
[root@chenguo bin]# cd /usr/local/bin
[root@chenguo bin]# mysqld --initialize --user=mysql
[root@chenguo bin]# mysqld --initialize-insecure --user=mysql
2016-11-18T08:46:09.910921Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-11-18T08:46:09.912342Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2016-11-18T08:46:09.912368Z 0 [ERROR] Aborting
解決辦法:
進入/var/lib/目錄中查看是否有mysql目錄,有的話進行刪除:
[root@chenguo bin]# cd /var/lib
[root@chenguo lib]# rm -rf mysql
[root@chenguo local]# cd /usr/local/bin/
[root@chenguo bin]# mysqld --initialize-insecure --user=mysql

修改root用戶密碼的操作:

[root@chenguo ~]# mysql
mysql> use mysql
mysql> update user set password=PASSWORD("root") where User="root";

出現如下錯誤:

ERROR 1054 (42S22): Unknown column "password" in "field list"

原因:mysql資料庫中的user下已經沒有password這個欄位,而變成了authentication_string,因此修改語句為:

mysql> update user set authentication_string=PASSWORD("root") where User="root";

修改密碼後,使用:

[root@chenguo ~]# systemctl stop mysqld
[root@chenguo ~]# systemctl restart mysqld

然後使用:

[root@chenguo ~]# mysql -u root -p
Enter password:

才能登錄mysql。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

6.類似Object監視器方法的Condition介面
hadoop源碼解析之hdfs內部結構分析
對 Servlet 的改進——Struts2 引入
03 編譯安裝apache的簡易配置
圖像工具包VintaSoftImaging.NET SDK v8.5,新增獨立web服務

TAG:達人科技 |

您可能感興趣

Linux之RedHat7如何更換yum源