建站不啰嗦,上手跟我做(二十)mysql8.0 安装

检查是否安装 mysql

[root@localhost tmp]# rpm -qa|grep -i mysql
mysql-libs-5.1.71-1.el6.x86_64
mysql-community-server-8.0.13-1.el6.x86_64

卸载已安装的 mysql

[root@localhost tmp]# rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64
[root@localhost tmp]# rpm -e --nodeps mysql-community-server-8.0.13-1.el6.x86_64
[root@localhost tmp]# rpm -qa|grep -i mysql

解压 mysql8.0 安装包

[root@localhost tmp]# tar -xvf mysql-8.0.13-1.el6.x86_64.rpm-bundle.tar 
mysql-community-libs-compat-8.0.13-1.el6.x86_64.rpm
mysql-community-test-8.0.13-1.el6.x86_64.rpm
mysql-community-common-8.0.13-1.el6.x86_64.rpm
mysql-community-devel-8.0.13-1.el6.x86_64.rpm
mysql-community-server-8.0.13-1.el6.x86_64.rpm
mysql-community-client-8.0.13-1.el6.x86_64.rpm
mysql-community-libs-8.0.13-1.el6.x86_64.rpm

安装(按顺序来,别插队)

[root@localhost tmp]# rpm -ivh mysql-community-common-8.0.13-1.el6.x86_64.rpm 
warning: mysql-community-common-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-common ########################################### [100%]
[root@localhost tmp]# rpm -ivh mysql-community-libs-8.0.13-1.el6.x86_64.rpm 
warning: mysql-community-libs-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-libs   ########################################### [100%]
[root@localhost tmp]# rpm -ivh mysql-community-client-8.0.13-1.el6.x86_64.rpm 
warning: mysql-community-client-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-client ########################################### [100%]
[root@localhost tmp]# rpm -ivh mysql-community-libs-compat-8.0.13-1.el6.x86_64.rpm 
warning: mysql-community-libs-compat-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-libs-co########################################### [100%]
[root@localhost tmp]# rpm -ivh mysql-community-server-8.0.13-1.el6.x86_64.rpm 
warning: mysql-community-server-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-server ########################################### [100%]
[root@localhost tmp]# rpm -ivh mysql-community-devel-8.0.13-1.el6.x86_64.rpm 
warning: mysql-community-devel-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 50
error: Failed dependencies:
	pkgconfig(openssl) is needed by mysql-community-devel-8.0.13-1.el6.x86_64
[root@localhost tmp]# yum install openssl-devel.x86_64 openssl.x86_64 -y 
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
[root@localhost tmp]# rpm -ivh mysql-community-devel-8.0.13-1.el6.x86_64.rpm 
warning: mysql-community-devel-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-devel  ########################################### [100%]
[root@localhost tmp]# rpm -ivh mysql-community-test-8.0.13-1.el6.x86_64.rpm 
warning: mysql-community-test-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
	perl(JSON) is needed by mysql-community-test-8.0.13-1.el6.x86_64
	perl(Time::HiRes) is needed by mysql-community-test-8.0.13-1.el6.x86_64
[root@localhost tmp]# yum install  perl-JSON.noarch -y 
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
[root@localhost tmp]# yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL
Loaded plugins: fastestmirror, refresh-packagekit, security
[root@localhost tmp]# rpm -ivh mysql-community-test-8.0.13-1.el6.x86_64.rpm 
warning: mysql-community-test-8.0.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
   1:mysql-community-test   ########################################### [100%]

查看相关部件安装情况

[root@localhost tmp]# rpm -qa | grep -i mysql
mysql-community-libs-8.0.13-1.el6.x86_64
mysql-community-devel-8.0.13-1.el6.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
mysql-community-common-8.0.13-1.el6.x86_64
mysql-community-server-8.0.13-1.el6.x86_64
mysql-community-test-8.0.13-1.el6.x86_64
mysql-community-libs-compat-8.0.13-1.el6.x86_64
mysql-community-client-8.0.13-1.el6.x86_64

启动服务

[root@localhost tmp]# service mysql start
mysql: unrecognized service
[root@localhost tmp]# service mysqld start
MySQL Daemon failed to start.
Starting mysqld:                                           [FAILED]

/var/run/mysqld/ 的属主和属组是 root,mysql 并不能在其中创建文件,修改该目录的属主和属组,启动 OK。

[root@spark01 ~]# mkdir -p /var/run/mysqld/
[root@spark01 ~]# ls -ld /var/run/mysqld/
drwxr-xr-x 2 root root 40 Jan 20 18:28 /var/run/mysqld/
[root@spark01 ~]# chown mysql.mysql /var/run/mysqld/
[root@localhost /]# service mysqld start
Starting mysqld:                                           [  OK  ]

设置密码

[root@localhost log]# mysql -u root -p
Enter password: 
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';  
Query OK, 0 rows affected (0.10 sec)

修改设置使 root 账号可以远程访问

mysql> use mysql;
mysql>update user set host = '%' where user ='root';
mysql>select host, user from user; 
mysql>flush  privileges;

修改防火墙设置,3306 可以通过防火墙

[root@localhost etc]# iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

永久生效,修改 /etc/sysconfig/iptables
增加下面一行(注意:增加的开放 3306 端口的语句一定要在 icmp-host-prohibited 之前)

*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

重启之后启动 mysql 服务

[root@localhost /]# service mysqld start
Starting mysqld:                                           [  OK  ]

开机启动

修改 mysqld 文件

/var/run/ 目录下建立文件夹是在内存中,故每次重启后内存被清空导致 /var/run/mysqld 也被清除,从而导致无法启动 mysql。

[root@localhost init.d]#  vim /etc/init.d/mysqld
get_mysql_option datadir "/var/lib/mysql" mysqld
datadir="$result"
get_mysql_option socket "$datadir/mysql.sock" mysqld
socketfile="$result"
get_mysql_option log-error "/var/log/mysqld.log" mysqld mysqld_safe
errlogfile="$result"
get_mysql_option pid-file "/var/run/mysqld/mysqld.pid" mysqld mysqld_safe
mypidfile="$result"

修改为

[root@localhost init.d]# vim /etc/init.d/mysqld
get_mysql_option datadir "/var/lib/mysqld" mysqld
datadir="$result"
get_mysql_option socket "$datadir/mysql.sock" mysqld
socketfile="$result"
get_mysql_option log-error "/var/log/mysqld.log" mysqld mysqld_safe
errlogfile="$result"
get_mysql_option pid-file "/var/lib/mysqld/mysqld.pid" mysqld mysqld_safe
mypidfile="$result"

修改 /etc/my.cnf

[root@localhost etc]# vim my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/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 the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It's default setting is log_bin=binlog
# disable_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
#
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password

datadir=/var/lib/mysql
socket=/var/lib/mysqld/mysql.sock

log-error=/var/log/mysqld.log
pid-file=/var/lib/mysqld/mysqld.pid
user=mysql

创建 /var/lib/mysqld/ 目录

[root@spark01 ~]# mkdir -p /var/lib/mysqld/
[root@spark01 ~]# ls -ld /var/lib/mysqld/
drwxr-xr-x. 2 root root 6 Aug  9 10:04 /var/lib/mysqld/
[root@spark01 ~]# chown mysql.mysql /var/lib/mysqld/
[root@localhost ~]# cd /var/lib/mysqld  
[root@localhost mysqld]# ls  
[root@localhost mysqld]# touch mysql.sock
[root@localhost mysqld]# chown mysql.mysql /var/lib/mysqld/mysql.sock 
[root@localhost run]# mkdir -p /var/run/mysqld/
[root@localhost run]# chown -R mysql:mysql /var/run/mysqld/

设置开机启动

systemctl daemon-reload   //重构进程

chmod +x /etc/rc.d/init.d/mysqld    

chkconfig --add mysqld

chkconfig mysqld on


上一篇 建站不啰嗦,上手跟我做(十九)在 Linux 系统安装 Nodejs
目录
下一篇 建站不啰嗦,上手跟我做(二十一)FastDFS+FastDHT 安装