由于安装的mysql8.0和其他服务器的数据库(版本5.1.30)由于版本差异过大,无法通信,因此需要安装一个中间版本5.6,但是它的安装过程和mysql8.0安装略有不同。

解压文件

// 解压文件生成两个xz格式的压缩文件
$ tar -xzvf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz
// 为了方便查找,改个名字
mv mysql-5.6.42-linux-glibc2.12-x86_64 mysql5
// 为了使用mysql快速初始化,链接到指定目录
ln -s /home/work/lnmp/mysql5/ /usr/local/mysql

环境配置

我们需要专门的mysql进程启动用户和权限管理:

// 创建mysql系统用户和用户组
useradd -r mysql
// 给予安装目录mysql权限
chown mysql:mysql -R mysql5

配置自己的mysql配置文件,因为我有多个Mysql库,我手动指定很多参数:

[client]
socket=/home/work/lnmp/mysql5/tmp/mysql.sock
default-character-set=utf8

[mysql]
basedir=/home/work/lnmp/mysql5/
datadir=/home/work/lnmp/mysql5/data/
socket=/home/work/lnmp/mysql5/tmp/mysql.sock
port=3306
user=mysql
# 指定日志时间为系统时间
log_timestamps=SYSTEM
log-error=/home/work/lnmp/mysql5/log/mysql.err

[mysqld]
basedir=/home/work/lnmp/mysql5/
datadir=/home/work/lnmp/mysql5/data/
socket=/home/work/lnmp/mysql5/tmp/mysql.sock
port=3306
user=mysql
log_timestamps=SYSTEM
collation-server = utf8_unicode_ci
character-set-server = utf8

[mysqld_safe]
log-error=/home/work/lnmp/mysql5/log/mysqld_safe.err
pid-file=/home/work/lnmp/mysql5/tmp/mysqld.pid
socket=/home/work/lnmp/mysql5/tmp/mysql.sock

[mysql.server]
basedir=/home/work/lnmp/mysql5
socket=/home/work/lnmp/mysql5/tmp/mysql.sock

[mysqladmin]                                                                                                                                  
socket=/home/work/lnmp/mysql5/tmp/mysql.sock 

这个里面我指定了错误日志的路径,在接下来的操作中,如果出现错误,除了查看终端显示的错误,还要记得去错误日志里查看详细的信息。

因为我指定了一些文件,所以需要提前创建:

mkdir log
touch log/mysql.err
touch log/mysqld_safe.err
mkdir tmp
mkdir data
cd .. & chown mysql:mysql -R mysql5

数据库初始化

如果我们不初始化,直接使用bin/mysqld_safe启动会报错,因为我们需要初始化mysql环境,具体的操作可以参考官方文档

$ scripts/mysql_install_db --user=mysql
...
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  ./bin/mysqladmin -u root password 'new-password'
  ./bin/mysqladmin -u root -h szwg-cdn-ai-predict00.szwg01.baidu.com password 'new-password'

Alternatively you can run:

  ./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.

提示中提示我们已经创建了root的用户,需要修改临时密码,同时初始化成功。也告诉我们怎么启动一个数据库实例。

启动数据库

我们使用mysqld_safe 命令来启动:

$ bin/mysqld_safe
181217 14:55:08 mysqld_safe Logging to '/home/work/lnmp/mysql5/log/mysqld_safe.err'.
181217 14:55:08 mysqld_safe Starting mysqld daemon with databases from /home/work/lnmp/mysql5/data

链接全局命令

此时,我们调用mysql只能用路径/home/work/lnmp/mysql8/bin/mysql或相对路径,需要链接为全局命令:

$ ln -s /home/work/lnmp/mysql8/bin/mysql /usr/bin/
$ ln -s /home/work/lnmp/mysql8/bin/mysql_safe /usr/bin/

打开数据库

数据库进程已经启动,我们可以在新终端正常使用mysql数据库,但是直接使用mysql命令报错:

$ mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

我查看了官方安装多个数据库的文档,尝试了很多方法,依然没有办法指定mysql命令的默认socket路径(/tmp/mysql.sock)。但是根据mysql.sock的作用的说明,我们指定mysql.sock路径即可:

bin/mysql -S /home/work/lnmp/mysql8/tmp/mysql.sock -h localhost -uroot -p
Enter password: 

或者:

ln -s /home/work/lnmp/mysql8/tmp/mysql.sock /tmp/

然后我们再调用mysql命令就不会报错了。

修改初始密码

初始化的时候,命令行文本已经提示我们需要怎样更新root密码,并根据他的指示操作即可,要详细阅读输出的文本:

$ bin/mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

连接数据库,新密码已经更新。

参考文章

  1. mysql8.0安装:https://segmentfault.com/a/11...
  2. mysql8.0初始化:https://dev.mysql.com/doc/ref...

赵帅强
3.3k 声望380 粉丝

前端打工人