Linux下二进制文件安装MySQL

时间:2022-09-13 21:06:14

MySQL 下载地址:https://dev.mysql.com/downloads/mysql/

并按如下方式选择来下载安装包。

Linux下二进制文件安装MySQL



1. 设置配置文件/etc/my.cnmore /etc/my.cnf

[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8

[mysqld]
user=mysql
port = 3306
server_id = 1
socket=/tmp/mysql.sock
basedir =/usr/local/mysql
datadir =/usr/local/mysql/data
pid-file=/usr/local/mysql/data/mysqld.pid
log-error=/usr/local/mysql/log/mysql-error.log
character_set_server=utf8
max_connections=1000

 

 

2.新建用户和用户组

groupadd mysql
useradd  -g mysql mysql

 


3.解压mysql二进制文件到目录/usr/local目录下,并重命名为mysql

cd /usr/local
tar -xf /root/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql
chown -R mysql:mysql mysql
cd /usr/local/mysql
mkdir log
chown -R mysql:mysql log
chmod 755 log
cd /usr/local/mysql/log
touch mysql-error.log
chmod 755 mysql-error.log
chown mysql:mysql mysql-error.log

mkdir -p /usr/local/mysql/socketfile
chown -R mysql:mysql /usr/local/mysql/socketfile

 

 

4.初始化数据库

cd /usr/local/mysql
bin/mysqld --initialize --user=mysql

 

初始化数据库后的密码存放在mysql-error.log文件中

[mysql@SaltStack-Minion log]$ more mysql-error.log 
2017-09-25T01:02:36.182707Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentat
ion for more details).
2017-09-25T01:02:42.076463Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-09-25T01:02:42.464764Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-09-25T01:02:42.916407Z 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 U
UID: 3c1e3858-a18d-11e7-b261-000c299cd645.
2017-09-25T01:02:43.104917Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-09-25T01:02:43.124083Z 1 [Note] A temporary password is generated for root@localhost: O4Cfoig_gXui 2017-09-25T01:02:53.184964Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentat
ion for more details).

 

 


 

5.设置环境变量(root用户和mysql用户都添加)

在~/.bash_profile文件中添加mysql的安装目录,添加后的结果如下:

[root@CentOS6 mysql]# vi ~/.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

 

#使修改生效

source  ~/.bash_profile

 

 


6.设置service服务自启动

cd  /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysql.server
service mysql.server start
service mysql.server restart
service mysql.server stop

 

 

 

7.修改MySQL的root密码

 

set password for root@localhost = password('mysql'); 
flush privileges;

 

 

8.当启动MySQL数据库报错时,尝试采用如下方式解决:

报错信息:

Starting MySQL.. ERROR! The server quit without updating PID file

 

解决方法:

(1)首先检查mysql日志文件中报错信息(一般mysql日志文件会写在/etc/my.cnf这个文件中);

(2)找到报错信息之后,然后逐步确诊原因即可;

(3)上述问题,我是通过删除mysql.sock文件(在/etc/my.cnf文件中会记录该文件的所在目录),然后再次数据库,问题解决;