使用二进制安装包的方式单机部署MySQL8.0.13

           使用二进制安装包的方式单机部署MySQL8.0.13

                                       作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

  关于MySQL的介绍我这里就不多做赘述了,如何下载MySQL详情请参考:MySQL入门介绍(mysql-8.0.13)。本篇博客默认你已经将MySQL二进制tar包下载好并上传到服务器上了。

一.使用默认的安装路径部署MySQL服务

1>.安装上传工具

[root@node101 ~]# yum -y install lrzsz

2>.上传MySQL的二进制安装包到服务器上

[root@node101 ~]# cd /usr/local/
[root@node101 local]#
[root@node101 local]# ls
bin etc games include lib lib64 libexec sbin share src
[root@node101 local]#
[root@node101 local]# rz
rz waiting to receive.
开始 zmodem 传输。 按 Ctrl+C 取消。
% KB KB/s :: Errorss.xz... [root@node101 local]#
[root@node101 local]# ls
bin etc games include lib lib64 libexec mysql-8.0.-linux-glibc2.-x86_64.tar.xz sbin share src
[root@node101 local]#

3>.在“/usr/local"目录进行解压操作并创建符号链接

[root@node101 local]# ll
total
drwxr-xr-x. root root Aug bin
drwxr-xr-x. root root Aug etc
drwxr-xr-x. root root Aug games
drwxr-xr-x. root root Aug include
drwxr-xr-x. root root Aug lib
drwxr-xr-x. root root Aug lib64
drwxr-xr-x. root root Aug libexec
-rw-r--r--. root root Jan : mysql-8.0.-linux-glibc2.-x86_64.tar.xz
drwxr-xr-x. root root Aug sbin
drwxr-xr-x. root root Jan : share
drwxr-xr-x. root root Aug src
[root@node101 local]#
[root@node101 local]#
[root@node101 local]# tar -xf mysql-8.0.-linux-glibc2.-x86_64.tar.xz
[root@node101 local]#
[root@node101 local]# ll
total
drwxr-xr-x. root root Aug bin
drwxr-xr-x. root root Aug etc
drwxr-xr-x. root root Aug games
drwxr-xr-x. root root Aug include
drwxr-xr-x. root root Aug lib
drwxr-xr-x. root root Aug lib64
drwxr-xr-x. root root Aug libexec
drwxr-xr-x. root root Jan : mysql-8.0.-linux-glibc2.-x86_64
-rw-r--r--. root root Jan : mysql-8.0.-linux-glibc2.-x86_64.tar.xz
drwxr-xr-x. root root Aug sbin
drwxr-xr-x. root root Jan : share
drwxr-xr-x. root root Aug src
[root@node101 local]#

[root@node101 local]# tar -xf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz

[root@node101 local]# ll
total
drwxr-xr-x. root root Aug bin
drwxr-xr-x. root root Aug etc
drwxr-xr-x. root root Aug games
drwxr-xr-x. root root Aug include
drwxr-xr-x. root root Aug lib
drwxr-xr-x. root root Aug lib64
drwxr-xr-x. root root Aug libexec
drwxr-xr-x. root root Jan : mysql-8.0.-linux-glibc2.-x86_64
-rw-r--r--. root root Jan : mysql-8.0.-linux-glibc2.-x86_64.tar.xz
drwxr-xr-x. root root Aug sbin
drwxr-xr-x. root root Jan : share
drwxr-xr-x. root root Aug src
[root@node101 local]#
[root@node101 local]#
[root@node101 local]# ln -s /usr/local/mysql-8.0.-linux-glibc2.-x86_64 /usr/local/mysql
[root@node101 local]#
[root@node101 local]# ll
total
drwxr-xr-x. root root Aug bin
drwxr-xr-x. root root Aug etc
drwxr-xr-x. root root Aug games
drwxr-xr-x. root root Aug include
drwxr-xr-x. root root Aug lib
drwxr-xr-x. root root Aug lib64
drwxr-xr-x. root root Aug libexec
lrwxrwxrwx. root root Jan : mysql -> /usr/local/mysql-8.0.-linux-glibc2.-x86_64
drwxr-xr-x. root root Jan : mysql-8.0.-linux-glibc2.-x86_64
-rw-r--r--. root root Jan : mysql-8.0.-linux-glibc2.-x86_64.tar.xz
drwxr-xr-x. root root Aug sbin
drwxr-xr-x. root root Jan : share
drwxr-xr-x. root root Aug src
[root@node101 local]#

创建软连接([root@node101 local]# ln -s /usr/local/mysql-8.0.13-linux-glibc2.12-x86_64 /usr/local/mysql)

[root@node101 local]# du -h --max-depth=
./bin
./etc
./games
./include
./lib
./lib64
./libexec
./sbin
.0K ./share
./src
.0G ./mysql-8.0.-linux-glibc2.-x86_64    #加压后的大小是2个G
.3G .
[root@node101 local]#
[root@node101 local]#

4>.MySQL各个目录功能

[root@node101 local]# ll mysql/
total
drwxr-xr-x. root root Jan : bin               #存放MySQL可执行的脚本
drwxr-xr-x. root root Jan : docs              #存放文档信息
drwxr-xr-x. root root Jan : include            #由于MySQL是C和C++编写的,因次我们会在该目录下看到一些以"*.h”的头文件信息
drwxr-xr-x. root root Jan : lib               #存放依赖包
-rw-r--r--. Oct : LICENSE
-rw-r--r--. Oct : LICENSE.router
drwxr-xr-x. root root Jan : man               #存放man帮助的信息
-rw-r--r--. Oct : README
-rw-r--r--. Oct : README.router
drwxr-xr-x. root root Jan : share             #存放支持的字符集信息以及一些错误信息
drwxr-xr-x. root root Jan : support-files        #存放MySQL服务的管理脚本,比如我们想要将MySQL变成一个Linux开机自启动服务时,就得使用该目录下的mysql.service脚本!
[root@node101 local]#
[root@node101 local]#

5>.创建默认的数据目录

[root@node101 local]# mkdir /usr/local/mysql/data
[root@node101 local]#
[root@node101 local]# ll mysql/
total
drwxr-xr-x. root root Jan : bin
drwxr-xr-x. root root Jan : data
drwxr-xr-x. root root Jan : docs
drwxr-xr-x. root root Jan : include
drwxr-xr-x. root root Jan : lib
-rw-r--r--. Oct : LICENSE
-rw-r--r--. Oct : LICENSE.router
drwxr-xr-x. root root Jan : man
-rw-r--r--. Oct : README
-rw-r--r--. Oct : README.router
drwxr-xr-x. root root Jan : share
drwxr-xr-x. root root Jan : support-files
[root@node101 local]#

如下图所示,我们需要创建mysql默认的数据目录([root@node101 local]# mkdir /usr/local/mysql/data)

使用二进制安装包的方式单机部署MySQL8.0.13

6>.创建mysql用户(不建议使用root用户直接启动该服务)

[root@node101 local]# useradd mysql                #这条命令会默认帮我们创建一个mysql用户,并将该用户加入一个同名的组中
[root@node101 local]#
[root@node101 local]# id mysql
uid=(mysql) gid=(mysql) groups=(mysql)
[root@node101 local]#
[root@node101 local]#
[root@node101 local]# pwd
/usr/local
[root@node101 local]#
[root@node101 local]# ll
total
drwxr-xr-x. root root Aug bin
drwxr-xr-x. root root Aug etc
drwxr-xr-x. root root Aug games
drwxr-xr-x. root root Aug include
drwxr-xr-x. root root Aug lib
drwxr-xr-x. root root Aug lib64
drwxr-xr-x. root root Aug libexec
lrwxrwxrwx. root root Jan : mysql -> /usr/local/mysql-8.0.-linux-glibc2.-x86_64
drwxr-xr-x. root root Jan : mysql-8.0.-linux-glibc2.-x86_64
-rw-r--r--. root root Jan : mysql-8.0.-linux-glibc2.-x86_64.tar.xz
drwxr-xr-x. root root Aug sbin
drwxr-xr-x. root root Jan : share
drwxr-xr-x. root root Aug src
[root@node101 local]#
[root@node101 local]# chown mysql:mysql mysql
[root@node101 local]#
[root@node101 local]# ll
total
drwxr-xr-x. root root Aug bin
drwxr-xr-x. root root Aug etc
drwxr-xr-x. root root Aug games
drwxr-xr-x. root root Aug include
drwxr-xr-x. root root Aug lib
drwxr-xr-x. root root Aug lib64
drwxr-xr-x. root root Aug libexec
lrwxrwxrwx. root root Jan : mysql -> /usr/local/mysql-8.0.-linux-glibc2.-x86_64
drwxr-xr-x. mysql mysql Jan : mysql-8.0.-linux-glibc2.-x86_64
-rw-r--r--. root root Jan : mysql-8.0.-linux-glibc2.-x86_64.tar.xz
drwxr-xr-x. root root Aug sbin
drwxr-xr-x. root root Jan : share
drwxr-xr-x. root root Aug src
[root@node101 local]#

修改解压包的权限给咱们刚刚创建的mysql用户([root@node101 local]# chown mysql:mysql mysql)

7>.初始化MySQL数据目录

[root@node101 ~]# ll /usr/local/mysql/data/
total
[root@node101 ~]#
[root@node101 ~]# cd /usr/local/mysql/bin/
[root@node101 bin]#
[root@node101 bin]# ./mysqld --initialize --user=mysql --datadir /usr/local/mysql/data
--16T00::.880029Z [Warning] [MY-] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
--16T00::.880151Z [System] [MY-] [Server] /usr/local/mysql-8.0.-linux-glibc2.-x86_64/bin/mysqld (mysqld 8.0.) initializing of server in progress as process
--16T00::.772135Z [Note] [MY-] [Server] A temporary password is generated for root@localhost: XwRQr7)jF0%M
--16T00::.974116Z [System] [MY-] [Server] /usr/local/mysql-8.0.-linux-glibc2.-x86_64/bin/mysqld (mysqld 8.0.) initializing of server has completed
[root@node101 bin]#
[root@node101 bin]#
[root@node101 bin]# ll /usr/local/mysql/data/
total
-rw-r-----. mysql mysql Jan : auto.cnf
-rw-------. mysql mysql Jan : ca-key.pem
-rw-r--r--. mysql mysql Jan : ca.pem
-rw-r--r--. mysql mysql Jan : client-cert.pem
-rw-------. mysql mysql Jan : client-key.pem
-rw-r-----. mysql mysql Jan : ib_buffer_pool
-rw-r-----. mysql mysql Jan : ibdata1
-rw-r-----. mysql mysql Jan : ib_logfile0
-rw-r-----. mysql mysql Jan : ib_logfile1
drwxr-x---. mysql mysql Jan : #innodb_temp
drwxr-x---. mysql mysql Jan : mysql
-rw-r-----. mysql mysql Jan : mysql.ibd
drwxr-x---. mysql mysql Jan : performance_schema
-rw-------. mysql mysql Jan : private_key.pem
-rw-r--r--. mysql mysql Jan : public_key.pem
-rw-r--r--. mysql mysql Jan : server-cert.pem
-rw-------. mysql mysql Jan : server-key.pem
drwxr-x---. mysql mysql Jan : sys
-rw-r-----. mysql mysql Jan : undo_001
-rw-r-----. mysql mysql Jan : undo_002
[root@node101 bin]#
[root@node101 bin]#

[root@node101 bin]# ./mysqld --initialize --user=mysql --datadir /usr/local/mysql/data

使用二进制安装包的方式单机部署MySQL8.0.13

[root@node101 ~]# cd /usr/local/mysql/data/
[root@node101 data]# ll
total
-rw-r-----. mysql mysql Jan : auto.cnf
-rw-------. mysql mysql Jan : ca-key.pem
-rw-r--r--. mysql mysql Jan : ca.pem
-rw-r--r--. mysql mysql Jan : client-cert.pem
-rw-------. mysql mysql Jan : client-key.pem
-rw-r-----. mysql mysql Jan : ib_buffer_pool
-rw-r-----. mysql mysql Jan : ibdata1
-rw-r-----. mysql mysql Jan : ib_logfile0
-rw-r-----. mysql mysql Jan : ib_logfile1
drwxr-x---. mysql mysql Jan : #innodb_temp
drwxr-x---. mysql mysql Jan : mysql
-rw-r-----. mysql mysql Jan : mysql.ibd
drwxr-x---. mysql mysql Jan : performance_schema
-rw-------. mysql mysql Jan : private_key.pem
-rw-r--r--. mysql mysql Jan : public_key.pem
-rw-r--r--. mysql mysql Jan : server-cert.pem
-rw-------. mysql mysql Jan : server-key.pem
drwxr-x---. mysql mysql Jan : sys
-rw-r-----. mysql mysql Jan : undo_001
-rw-r-----. mysql mysql Jan : undo_002
[root@node101 data]# rm -rf *
[root@node101 data]#

多次初始化失败时,会报错说是数据目录已经有数据了,我们去对应的数据目录将其删除掉即可!

  如果同学们在初始化遇到如下图所示的报错信息,大家不要慌,安装“libaio”依赖包即可:

使用二进制安装包的方式单机部署MySQL8.0.13

[root@node105 data]# ll
total 0
[root@node105 data]#
[root@node105 data]# pwd
/yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64/data
[root@node105 data]#
[root@node105 data]#
[root@node105 data]# mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64 --datadir=/yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64/data &
[] 3852
[root@node105 data]# mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory []+ Exit 127 mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64 --datadir=/yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64/data
[root@node105 data]#
[root@node105 data]#
[root@node105 data]#
[root@node105 data]# yum -y install libaio
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.sjtu.edu.cn
* extras: mirror.lzu.edu.cn
* updates: ftp.sjtu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ========================================================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================================================
Installing:
libaio x86_64 0.3.109-13.el7 base 24 k Transaction Summary
========================================================================================================================================================================================================
Install 1 Package Total download size: 24 k
Installed size: 38 k
Downloading packages:
libaio-0.3.109-13.el7.x86_64.rpm | 24 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libaio-0.3.109-13.el7.x86_64 1/1
Verifying : libaio-0.3.109-13.el7.x86_64 1/1 Installed:
libaio.x86_64 0:0.3.109-13.el7 Complete!
[root@node105 data]#
[root@node105 data]#
[root@node105 data]#
[root@node105 data]# mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64 --datadir=/yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64/data &
[] 3858
[root@node105 data]#
[root@node105 data]#
[root@node105 data]#
[root@node105 data]# 2019-01-22T14:44:52.845268Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-01-22T14:44:52.845381Z 0 [System] [MY-013169] [Server] /yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.14) initializing of server in progress as process 3858
2019-01-22T14:45:07.024823Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: d_0_b?C%8?2Y
2019-01-22T14:45:11.392014Z 0 [System] [MY-013170] [Server] /yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.14) initializing of server has completed []+ Done mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64 --datadir=/yinzhengjie/softwares/mysql-8.0.14-linux-glibc2.12-x86_64/data
[root@node105 data]#
[root@node105 data]# ll
total 155708
-rw-r-----. 1 mysql mysql 56 Jan 22 06:45 auto.cnf
-rw-------. 1 mysql mysql 1676 Jan 22 06:45 ca-key.pem
-rw-r--r--. 1 mysql mysql 1112 Jan 22 06:45 ca.pem
-rw-r--r--. 1 mysql mysql 1112 Jan 22 06:45 client-cert.pem
-rw-------. 1 mysql mysql 1676 Jan 22 06:45 client-key.pem
-rw-r-----. 1 mysql mysql 5748 Jan 22 06:45 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jan 22 06:45 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jan 22 06:45 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jan 22 06:44 ib_logfile1
drwxr-x---. 2 mysql mysql 6 Jan 22 06:45 #innodb_temp
drwxr-x---. 2 mysql mysql 4096 Jan 22 06:45 mysql
-rw-r-----. 1 mysql mysql 25165824 Jan 22 06:45 mysql.ibd
drwxr-x---. 2 mysql mysql 4096 Jan 22 06:45 performance_schema
-rw-------. 1 mysql mysql 1680 Jan 22 06:45 private_key.pem
-rw-r--r--. 1 mysql mysql 452 Jan 22 06:45 public_key.pem
-rw-r--r--. 1 mysql mysql 1112 Jan 22 06:45 server-cert.pem
-rw-------. 1 mysql mysql 1676 Jan 22 06:45 server-key.pem
drwxr-x---. 2 mysql mysql 27 Jan 22 06:45 sys
-rw-r-----. 1 mysql mysql 10485760 Jan 22 06:45 undo_001
-rw-r-----. 1 mysql mysql 10485760 Jan 22 06:45 undo_002
[root@node105 data]#

上述报错的解决方案([root@node105 data]# yum -y install libaio)

8>.启动MySQL服务

[root@node101 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/   #将MySQL加入到服务自启动
[root@node101 ~]#
[root@node101 ~]# rm -rf /etc/my.cnf                            #删除它表示以mysql所有的默认参数去启动
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server start                     #启动mysql服务
Starting MySQL.Logging to '/usr/local/mysql/data/node101.yinzhengjie.org.cn.err'.
. SUCCESS!
[root@node101 ~]#
[root@node101 ~]# ps -ef | grep mysql                            #查看mysql的启动进程
root : pts/ :: /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/node101.yinzhengjie.org.cn.pid
mysql : pts/ :: /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=node101.yinzhengjie.org.cn.err --pid-file=/usr/local/mysql/data/node101.yinzhengjie.org.cn.pid
root : pts/ :: grep --color=auto mysql
[root@node101 ~]#
[root@node101 ~]# netstat -untalp | grep 3306                      #查看mysql启动的端口,默认是3306
tcp6 ::: :::* LISTEN /mysqld   
tcp6 ::: :::* LISTEN /mysqld   #这个33060是MySQL的守护进程
[root@node101 ~]#

9>.连接MySQL数据库

[root@node101 ~]# ll /usr/local/mysql/
total
drwxr-xr-x. root root Jan : bin
drwxr-xr-x. mysql mysql Jan : data
drwxr-xr-x. root root Jan : docs
drwxr-xr-x. root root Jan : include
drwxr-xr-x. root root Jan : lib
-rw-r--r--. Oct : LICENSE
-rw-r--r--. Oct : LICENSE.router
drwxr-xr-x. root root Jan : man
-rw-r--r--. Oct : README
-rw-r--r--. Oct : README.router
drwxr-xr-x. root root Jan : share
drwxr-xr-x. root root Jan : support-files
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# cat ~/.bash_profile
# .bash_profile # Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi # User specific environment and startup programs PATH=$PATH:$HOME/bin:/usr/local/mysql/bin export PATH
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# source ~/.bash_profile
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. Copyright (c) , , 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>

[root@node101 ~]# mysql -uroot -p

使用二进制安装包的方式单机部署MySQL8.0.13

10>.重置MySQL的密码(mysql8.0版本和以往的MySQL5.7有所不同)

[root@node101 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. Copyright (c) , , 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> alter user user() identified by 'yinzhengjie';          #重置MySQL的密码,将临时密码改为“yinzhengjie”
Query OK, rows affected (0.03 sec) mysql> exit
Bye
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -pyinzhengjie                #使用重置后的密码登录数据库
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. MySQL Community Server - GPL Copyright (c) , , 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.01 sec) mysql> quit
Bye
[root@node101 ~]#

二.迁移MySQL的安装目录

1>.迁移服务前,需要先关闭MySQL服务

[root@node101 ~]# /etc/init.d/mysql.server status
SUCCESS! MySQL running ()
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server stop
Shutting down MySQL... SUCCESS!
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server status
ERROR! MySQL is not running
[root@node101 ~]#

2>.将MySQL的数据目录迁移到指定的路径

[root@node101 ~]# mkdir -pv /yinzhengjie/data
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/data/
total
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# ll /usr/local/
total
drwxr-xr-x. root root Aug bin
drwxr-xr-x. root root Aug etc
drwxr-xr-x. root root Aug games
drwxr-xr-x. root root Aug include
drwxr-xr-x. root root Aug lib
drwxr-xr-x. root root Aug lib64
drwxr-xr-x. root root Aug libexec
-rw-r--r--. root root Jan : mysql-8.0.-linux-glibc2.-x86_64
drwxr-xr-x. root root Aug sbin
drwxr-xr-x. root root Jan : share
drwxr-xr-x. root root Aug src
[root@node101 ~]#
[root@node101 ~]# mv /usr/local/mysql-8.0.-linux-glibc2.-x86_64 /yinzhengjie/data/mysql      #停止服务后,我们将MySQL的数据目录移动到我们想要修改的指定位置
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/data/
total
drwxr-xr-x. mysql mysql Jan : mysql
[root@node101 ~]#

3>.编辑MySQL的配置文件(由于我们移动了MySQL的安装目录以及数据目录,因此默认的配置已经不生效了,需要手动指定!)

[root@node101 ~]#
[root@node101 ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/data/mysql/            #指定MySQL的安装目录
datadir=/yinzhengjie/data/mysql/data          #指定MySQL存放数据的目录
[root@node101 ~]#

4>.启动MySQL服务

[root@node101 ~]# /etc/init.d/mysql.server start
Starting MySQL. SUCCESS!
[root@node101 ~]#

三.安装MySQL是指定安装目录

1>.解压MySQL软件包到指定位置(指定你需要安装MySQL的路径,可以不是默认的"/usr/local"目录)

[root@node101 local]# mkdir -pv /yinzhengjie/softwares                                #创建存放MySQL的目录位置
mkdir: created directory ‘/yinzhengjie’
mkdir: created directory ‘/yinzhengjie/softwares’
[root@node101 local]#
[root@node101 local]# ll /yinzhengjie/softwares/
total
[root@node101 local]#
[root@node101 local]# tar -xf mysql-8.0.-linux-glibc2.-x86_64.tar.xz -C /yinzhengjie/softwares/     #创建MySQL的软件包 
[root@node101 local]#
[root@node101 local]# ll /yinzhengjie/softwares/
total
drwxr-xr-x. root root Jan : mysql-8.0.-linux-glibc2.-x86_64
[root@node101 local]#

2>.将解压后的安装目录指定给mysql用户

[root@node101 ~]# chown mysql:mysql -R /yinzhengjie/softwares/mysql-8.0.-linux-glibc2.-x86_64/
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/
total
drwxr-xr-x. mysql mysql Jan : mysql-8.0.-linux-glibc2.-x86_64
[root@node101 ~]#
[root@node101 ~]#

3>.配置MySQL的环境变量

[root@node101 ~]# ll /yinzhengjie/softwares/
total
drwxr-xr-x. mysql mysql Jan : mysql-8.0.-linux-glibc2.-x86_64
[root@node101 ~]#
[root@node101 ~]# ln -s /yinzhengjie/softwares/mysql-8.0.-linux-glibc2.-x86_64/ /yinzhengjie/softwares/mysql
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/
total
lrwxrwxrwx. root root Jan : mysql -> /yinzhengjie/softwares/mysql-8.0.-linux-glibc2.-x86_64/
drwxr-xr-x. mysql mysql Jan : mysql-8.0.-linux-glibc2.-x86_64
[root@node101 ~]#
[root@node101 ~]# cat ~/.bash_profile
# .bash_profile # Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi # User specific environment and startup programs PATH=$PATH:$HOME/bin:/yinzhengjie/softwares/mysql/bin/ export PATH
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# source ~/.bash_profile
[root@node101 ~]#

4>.初始化MySQL数据库

[root@node101 ~]# mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data &
[]
[root@node101 ~]# --16T14::.941001Z [System] [MY-] [Server] /yinzhengjie/softwares/mysql-8.0.-linux-glibc2.-x86_64/bin/mysqld (mysqld 8.0.) initializing of server in progress as process
--16T14::.851566Z [Note] [MY-] [Server] A temporary password is generated for root@localhost: bSaeurfCP5)?    #注意,记住这里的临时密码!
--16T14::.575983Z [System] [MY-] [Server] /yinzhengjie/softwares/mysql-8.0.-linux-glibc2.-x86_64/bin/mysqld (mysqld 8.0.) initializing of server has completed []+ Done mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/mysql/data/
total
-rw-r-----. mysql mysql Jan : auto.cnf
-rw-------. mysql mysql Jan : ca-key.pem
-rw-r--r--. mysql mysql Jan : ca.pem
-rw-r--r--. mysql mysql Jan : client-cert.pem
-rw-------. mysql mysql Jan : client-key.pem
-rw-r-----. mysql mysql Jan : ib_buffer_pool
-rw-r-----. mysql mysql Jan : ibdata1
-rw-r-----. mysql mysql Jan : ib_logfile0
-rw-r-----. mysql mysql Jan : ib_logfile1
drwxr-x---. mysql mysql Jan : #innodb_temp
drwxr-x---. mysql mysql Jan : mysql
-rw-r-----. mysql mysql Jan : mysql.ibd
drwxr-x---. mysql mysql Jan : performance_schema
-rw-------. mysql mysql Jan : private_key.pem
-rw-r--r--. mysql mysql Jan : public_key.pem
-rw-r--r--. mysql mysql Jan : server-cert.pem
-rw-------. mysql mysql Jan : server-key.pem
drwxr-x---. mysql mysql Jan : sys
-rw-r-----. mysql mysql Jan : undo_001
-rw-r-----. mysql mysql Jan : undo_002
[root@node101 ~]#
[root@node101 ~]#

5>.修改MySQL的配置文件

[root@node101 ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
[root@node101 ~]#

6>.拷贝MySQL的启动脚本到系统自启动路径下并启动MySQL服务

[root@node101 ~]#
[root@node101 ~]# cp /yinzhengjie/softwares/mysql/support-files/mysql.server /etc/init.d/
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server start
Starting MySQL.Logging to '/yinzhengjie/softwares/mysql/data/node101.yinzhengjie.org.cn.err'.
. SUCCESS!
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# /etc/init.d/mysql.server status
SUCCESS! MySQL running ()
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# ps -ef | grep mysql
root : pts/ :: /bin/sh /yinzhengjie/softwares/mysql//bin/mysqld_safe --datadir=/yinzhengjie/softwares/mysql/data/ --pid-file=/yinzhengjie/softwares/mysql/data//node101.yinzhengjie.org.cn.pid
mysql : pts/ :: /yinzhengjie/softwares/mysql/bin/mysqld --basedir=/yinzhengjie/softwares/mysql/ --datadir=/yinzhengjie/softwares/mysql/data --plugin-dir=/yinzhengjie/softwares/mysql//lib/plugin --user=mysql --log-error=node101.yinzhengjie.org.cn.err --pid-file=/yinzhengjie/softwares/mysql/data//node101.yinzhengjie.org.cn.pid
root : pts/ :: grep --color=auto mysql
[root@node101 ~]#
[root@node101 ~]#

7>.登录MySQL数据库

[root@node101 ~]# mysql -uroot -pyinzhengjie          #我这里想用“yinzhengjie”作为密码登录,很显然失败了,因为我还没有授权!
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR (): Access denied for user 'root'@'localhost' (using password: YES)
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -p
Enter password:   #注意,这里的密码是第4步骤初始化时的密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. Copyright (c) , , 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> show databases;  #注意,虽然我们使用临时密码登录成功了,但是我们不能执行任何的SQL语句,我们需要先修改密码才能执行具体的SQL。
ERROR (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql>
mysql> alter user user() identified by 'yinzhengjie';    #我们这里将当前用户的密码修改为“yinzhegjie”
Query OK, rows affected (0.01 sec) mysql> quit
Bye
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -pyinzhengjie      #此时,我们在用“yinzhegjie”这个字符串登录是成功的!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. MySQL Community Server - GPL Copyright (c) , , 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.01 sec) mysql> quit
Bye
[root@node101 ~]#

四.MySQL多实例安装

1>.将安装好的MySQL目录拷贝一份

[root@node101 ~]#
[root@node101 ~]# mv /yinzhengjie/softwares/mysql-8.0.-linux-glibc2.-x86_64/ /yinzhengjie/softwares/mysql
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/
total
drwxr-xr-x. mysql mysql Jan : mysql
[root@node101 ~]#
[root@node101 ~]# cp -r /yinzhengjie/softwares/mysql /yinzhengjie/softwares/mysql2
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/
total
drwxr-xr-x. mysql mysql Jan : mysql
drwxr-xr-x. root root Jan : mysql2
[root@node101 ~]#
[root@node101 ~]# chown mysql:mysql -R /yinzhengjie/softwares/mysql2
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/
total
drwxr-xr-x. mysql mysql Jan : mysql
drwxr-xr-x. mysql mysql Jan : mysql2      #该目录是我们拷贝后MySQL目录
[root@node101 ~]#
[root@node101 ~]#

2>.检查已经启动的MySQL服务

[root@node101 ~]# /etc/init.d/mysql.server status
SUCCESS! MySQL running ()
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. MySQL Community Server - GPL Copyright (c) , , 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> show variables like '%sock%';
+-----------------------------------------+------------------+
| Variable_name | Value |
+-----------------------------------------+------------------+
| mysqlx_socket | /tmp/mysqlx.sock |   ----->#这里指定的了mysqlx的socket文件
| performance_schema_max_socket_classes | |
| performance_schema_max_socket_instances | - |
| socket | /tmp/mysql.sock |   ------>#这里指定了mysql的默认socket文件
+-----------------------------------------+------------------+
rows in set (0.10 sec) mysql> quit
Bye
[root@node101 ~]#
[root@node101 ~]# netstat -untalp | grep
tcp6 ::: :::* LISTEN /mysqld     #当前MySQL的实例
tcp6 ::: :::* LISTEN /mysqld  #当前MySQL的守护进程
[root@node101 ~]#
[root@node101 ~]#

3>.启动新拷贝的MySQL实例

[root@node101 ~]# cat /etc/my3307.cnf                         #我们需要定义我们的配置文件
[mysqld]
basedir=/yinzhengjie/softwares/mysql2/
datadir=/yinzhengjie/softwares/mysql2/data/
port=
socket=/tmp/mysql3307.sock
mysqlx_port=
mysqlx_socket=/tmp/mysqlx33070.sock
[root@node101 ~]#
[root@node101 ~]# mysqld --defaults-file=/etc/my3307.cnf --user=mysql &    #启动咱们配置好的实例,此处我们需要指定相应的MySQL配置文件
[]
[root@node101 ~]# --16T14::.894011Z [System] [MY-] [Server] /yinzhengjie/softwares/mysql/bin/mysqld (mysqld 8.0.) starting as process
--16T14::.682348Z [System] [MY-] [Server] Starting crash recovery...
--16T14::.692916Z [System] [MY-] [Server] Crash recovery finished.
--16T14::.761907Z [Warning] [MY-] [Server] CA certificate ca.pem is self signed.
--16T14::.782447Z [System] [MY-] [Server] /yinzhengjie/softwares/mysql/bin/mysqld: ready for connections. Version: '8.0.13' socket: '/tmp/mysql3307.sock' port: MySQL Community Server - GPL.
--16T14::.808012Z [System] [MY-] [Server] X Plugin ready for connections. Socket: '/tmp/mysqlx33070.sock' bind-address: '::' port: [root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# ps -ef | grep mysql
root : pts/ :: /bin/sh /yinzhengjie/softwares/mysql//bin/mysqld_safe --datadir=/yinzhengjie/softwares/mysql/data/ --pid-file=/yinzhengjie/softwares/mysql/data//node101.yinzhengjie.org.cn.pid
mysql : pts/ :: /yinzhengjie/softwares/mysql/bin/mysqld --basedir=/yinzhengjie/softwares/mysql/ --datadir=/yinzhengjie/softwares/mysql/data --plugin-dir=/yinzhengjie/softwares/mysql//lib/plugin --user=mysql --log-error=node101.yinzhengjie.org.cn.err --pid-file=/yinzhengjie/softwares/mysql/data//node101.yinzhengjie.org.cn.pid
mysql : pts/ :: mysqld --defaults-file=/etc/my3307.cnf --user=mysql    #这里是我们刚刚启动的MySQL服务,想必大家对这个配置文件名称应该都眼熟吧!
root : pts/ :: grep --color=auto mysql
[root@node101 ~]#
[root@node101 ~]# netstat -untalp | grep       #这是我们新启动的MySQL实例端口
tcp6 ::: :::* LISTEN /mysqld
tcp6 ::: :::* LISTEN /mysqld
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# netstat -untalp | grep 3306      #这个是咱们之前就以及启动的端口
tcp6 ::: :::* LISTEN /mysqld
tcp6 ::: :::* LISTEN /mysqld
[root@node101 ~]#
[root@node101 ~]#

4>.连接我们启动的MySQL目录

[root@node101 ~]# mysql -uroot -pyinzhengjie -S /tmp/mysql3307.sock        #使用“-S”选项指定本地socket文件,我们知道使用的就是"/tmp/mysql.sock"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. MySQL Community Server - GPL Copyright (c) , , 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql> CREATE DATABASE yinzhengjie CHARACTER SET utf8;      #我们在3307端口的数据库实例创建一个MySQL数据库
Query OK, row affected, warning (0.02 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| yinzhengjie |
+--------------------+
rows in set (0.00 sec) mysql> quit
Bye
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. MySQL Community Server - GPL Copyright (c) , , 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> show databases;          #我们可以发现在3307端口的MySQL中创建的“yinzhengjie”数据库并没有影响当前的(3306端口)数据库信息!
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set (0.00 sec) mysql> quit
Bye
[root@node101 ~]#

  扩展小提示:

[root@node101 ~]# mysql -uroot -pyinzhengjie -S /tmp/mysql3307.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. MySQL Community Server - GPL Copyright (c) , , 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> show variables like '%sock%';
+-----------------------------------------+-----------------------+
| Variable_name | Value |
+-----------------------------------------+-----------------------+
| mysqlx_socket | /tmp/mysqlx33070.sock |
| performance_schema_max_socket_classes | |
| performance_schema_max_socket_instances | - |
| socket | /tmp/mysql3307.sock |
+-----------------------------------------+-----------------------+
rows in set (0.01 sec) mysql> quit
Bye
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 8.0. MySQL Community Server - GPL Copyright (c) , , 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> show variables like '%sock%';
+-----------------------------------------+------------------+
| Variable_name | Value |
+-----------------------------------------+------------------+
| mysqlx_socket | /tmp/mysqlx.sock |
| performance_schema_max_socket_classes | |
| performance_schema_max_socket_instances | - |
| socket | /tmp/mysql.sock |
+-----------------------------------------+------------------+
rows in set (0.01 sec) mysql> quit
Bye
[root@node101 ~]#

查看socket配置文件(mysql> show variables like '%sock%';)

[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/mysql/data/
total
-rw-r-----. mysql mysql Jan : auto.cnf
-rw-r-----. mysql mysql Jan : binlog.
-rw-r-----. mysql mysql Jan : binlog.index
-rw-------. mysql mysql Jan : ca-key.pem
-rw-r--r--. mysql mysql Jan : ca.pem
-rw-r--r--. mysql mysql Jan : client-cert.pem
-rw-------. mysql mysql Jan : client-key.pem
-rw-r-----. mysql mysql Jan : ib_buffer_pool
-rw-r-----. mysql mysql Jan : ibdata1
-rw-r-----. mysql mysql Jan : ib_logfile0
-rw-r-----. mysql mysql Jan : ib_logfile1
-rw-r-----. mysql mysql Jan : ibtmp1
drwxr-x---. mysql mysql Jan : #innodb_temp
drwxr-x---. mysql mysql Jan : mysql
-rw-r-----. mysql mysql Jan : mysql.ibd
-rw-r-----. mysql mysql Jan : node101.yinzhengjie.org.cn.err
-rw-r-----. mysql mysql Jan : node101.yinzhengjie.org.cn.pid
drwxr-x---. mysql mysql Jan : performance_schema
-rw-------. mysql mysql Jan : private_key.pem
-rw-r--r--. mysql mysql Jan : public_key.pem
-rw-r--r--. mysql mysql Jan : server-cert.pem
-rw-------. mysql mysql Jan : server-key.pem
drwxr-x---. mysql mysql Jan : sys
-rw-r-----. mysql mysql Jan : undo_001
-rw-r-----. mysql mysql Jan : undo_002
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]#
[root@node101 ~]# ll /yinzhengjie/softwares/mysql2/data/
total
-rw-r-----. mysql mysql Jan : auto.cnf
-rw-r-----. mysql mysql Jan : binlog.
-rw-r-----. mysql mysql Jan : binlog.
-rw-r-----. mysql mysql Jan : binlog.index
-rw-------. mysql mysql Jan : ca-key.pem
-rw-r--r--. mysql mysql Jan : ca.pem
-rw-r--r--. mysql mysql Jan : client-cert.pem
-rw-------. mysql mysql Jan : client-key.pem
-rw-r-----. mysql mysql Jan : ib_buffer_pool
-rw-r-----. mysql mysql Jan : ibdata1
-rw-r-----. mysql mysql Jan : ib_logfile0
-rw-r-----. mysql mysql Jan : ib_logfile1
-rw-r-----. mysql mysql Jan : ibtmp1
drwxr-x---. mysql mysql Jan : #innodb_temp
drwxr-x---. mysql mysql Jan : mysql
-rw-r-----. mysql mysql Jan : mysql.ibd
-rw-r-----. mysql mysql Jan : node101.yinzhengjie.org.cn.err
-rw-r-----. mysql mysql Jan : node101.yinzhengjie.org.cn.pid
-rw-r-----. mysql mysql Jan : node101.yinzhengjie.org.pid
drwxr-x---. mysql mysql Jan : performance_schema
-rw-------. mysql mysql Jan : private_key.pem
-rw-r--r--. mysql mysql Jan : public_key.pem
-rw-r--r--. mysql mysql Jan : server-cert.pem
-rw-------. mysql mysql Jan : server-key.pem
drwxr-x---. mysql mysql Jan : sys
-rw-r-----. mysql mysql Jan : undo_001
-rw-r-----. mysql mysql Jan : undo_002
drwxr-x---. mysql mysql Jan : yinzhengjie
[root@node101 ~]#

对比2个实例底层的MySQL存储情况([root@node101 ~]# ll /yinzhengjie/softwares/mysql/data/)

  推荐阅读1:在Mac OS环境下安装mysql服务

  推荐阅读2:Windows环境安装MySQL数据库

  推荐阅读3:使用yum源的方式单机部署MySQL8.0.13

上一篇:checked 选中


下一篇:LAMP开发之环境搭建(2014.12.7在ubuntu下)