最近预研一个新项目,需要装一个mysql,因为以前装过,原本以为很简单,大概半小时搞定,但是没想到搞了一晚上,记录一下完整步骤和遇到的问题。
1、软件版本和安装包准备
- centos7
- mysql5.7
官网下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
- 版本选择如下:
- 安装包选择如下:
2、首先卸载系统中自带的mysql并删除配置文件
# 查看系统自带的Mariadb和msql
rpm -qa|grep mariadb
rpm -qa | grep mysql
# 卸载系统自带的Mariadb
rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 删除etc目录下的my.cnf
rm /etc/my.cnf
3、创建mysql用户和用户组
# 创建mysql用户组
groupadd mysql
# 创建一个用户名为mysql的用户,并加入mysql用户组
useradd -g mysql mysql
4、安装准备
这里我们安装在/usr/local下面
cd /usr/local/
上传
rz 安装包
解压
tar -zxvf 安装包名
mv 安装包名 mysql
修改权限并创建data目录
chown -R mysql mysql/
chgrp -R mysql mysql/
cd mysql/
mkdir data
chown -R mysql:mysql data
5、准备配置文件
cd /usr/local/mysql
vi my.cnf
[mysql]
socket=/var/lib/mysql/mysql.sock
# set mysql client default chararter
default-character-set=utf8
[mysqld]
socket=/var/lib/mysql/mysql.sock
# set mysql server port
port = 3306
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=200
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
explicit_defaults_for_timestamp=true
# 取消密码验证
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysql.server]
user=mysql
basedir=/usr/local/mysql
6、进入mysql文件夹,并安装mysql
cd /usr/local/mysql
这里注意不要使用mysql_install_db的初始化方法
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
如果报以下错误:
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or director
则执行:
yum -y install numactl
然后在执行:
usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
7、开启服务
# 将mysql加入服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# 开机自启
chkconfig mysql on
# 开启
service mysql start
8、设置密码
# 登录(由于/etc/my.cnf中设置了取消密码验证,所以此处密码任意)
/usr/local/mysql/bin/mysql -u root -p
# 操作mysql数据库
use mysql;
# 修改密码
update user set authentication_string=password(\'你的密码\') where user=\'root\';
flush privileges;
exit;
重设密码
将my.cnf中的skip-grant-tables删除;
/usr/local/mysql/bin/mysql -u root -p
ALTER USER \'root\'@\'localhost\' IDENTIFIED BY \'修改后的密码\';
exit;
允许远程连接
/usr/local/mysql/bin/mysql -u root -p
use mysql;
update user set host=\'%\' where user = \'root\';
flush privileges;
eixt;
最后添加快捷方式
ln -s /usr/local/mysql/bin/mysql /usr/bin