官网下载mysql-5.5.61-linux-glibc2.12-x86_64.tar.gz安装包
下载地址:https://dev.mysql.com/downloads/mysql/5.5.html#downloads
第一种方式,安装到默认目录
1、这里安装到默认目录/usr/local
#解压
tar -zxvf mysql-5.5.61-linux-glibc2.12-x86_64.tar.gz
#重命名
mv mysql-5.5.61-linux-glibc2.12-x86_64 mysql
#移动到默认目录
mv mysql /usr/local
2、 创建mysql免登录用户并授权
#创建用户
useradd -s /sbin/nologin -M mysql
#授权
chown -R mysql:mysql /usr/local/mysql
chmod -R 775 /usr/local/mysql
3、 安装
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
出现下面错误
安装依赖
yum -y install perl
yum -y install libaio
再次执行安装命令
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
安装成功,记住初始密码
4、 修改配置脚本
#修改脚本
vim ./support-files/mysql.server
#修改下面内容
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
#拷贝脚本
cp ./support-files/mysql.server /etc/init.d/mysqld
#授权
chmod 775 /etc/init.d/mysqld
5、配置修改
vim /etc/my.cnf
修改如下
[client]
port=3306
default-character-set=utf8
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/usr/local/mysql/log/error.log
pid-file=/usr/local/mysql/data/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
6、设置开机启动和环境变量
#将数据库服务添加到开机启动
chkconfig --add mysqld
chkconfig mysqld on
#编辑环境变量
vim /etc/profile
#最后加入下面一句
export PATH=$PATH:/usr/local/mysql/bin
#环境变量生效
source /etc/profile
7、启动
service mysqld start
成功
第二种方式,安装自定义目录
1、这里安装到/opt
#解压
tar -zxvf mysql-5.5.61-linux-glibc2.12-x86_64.tar.gz
#重命名
mv mysql-5.5.61-linux-glibc2.12-x86_64 mysql
#移动到默认目录
mv mysql /opt
2、 创建mysql免登录用户并授权
#创建用户
useradd -s /sbin/nologin -M mysql
#授权
chown -R mysql:mysql /opt/mysql
chmod -R 775 /opt/mysql
3、 安装
#安装依赖
yum -y install perl
yum -y install libaio
./scripts/mysql_install_db --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
安装成功,记住临时密码
4、 修改配置脚本
#修改脚本
vim ./support-files/mysql.server
#修改下面内容
basedir=/opt/mysql-5.5.61
datadir=/opt/mysql-5.5.61/data
#拷贝脚本
cp ./support-files/mysql.server /etc/init.d/mysqld
#授权
chmod 775 /etc/init.d/mysqld
5、配置修改
vim /etc/my.cnf
修改如下
[client]
port=3306
default-character-set=utf8
[mysqld]
basedir=/opt/mysql
datadir=/opt/mysql/data
socket=/opt/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/opt/mysql/log/error.log
pid-file=/opt/mysql/data/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
6、设置开机启动和环境变量
#将数据库服务添加到开机启动
chkconfig --add mysqld
chkconfig mysqld on
#编辑环境变量
vim /etc/profile
#最后加入下面一句
export PATH=$PATH:/opt/mysql/bin
#环境变量生效
source /etc/profile
7、启动
service mysqld start
报错,日志文件不存在:
创建文件,并保存
cd /opt/mysql
mkdir log
vim error.log
:wq!保存文件
再次启动
service mysqld start
成功
8:用上面临时密码连接数据库
mysql -u root -p -S /opt/mysql/mysql.sock
修改密码:
#修改密码
set password for root@localhost = password('root');
#设置远程连接
grant all privileges on *.* to root@'%' identified by 'root';
#刷新权限
flush privileges;