Linux系统安装Mysql5.7

系统版本centos7.4,mysql64位版本5.7.22

快速开始

下载mysql文件包

wget方式下载:

1
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

同样可以手动先下载好然后上传到服务器。

解压文件包

1
tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

移动位置

可以移动到自己需要的位置

1
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/

重命名

1
mv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql

新建data目录

1
mkdir /usr/local/mysql/data

新建mysql用户、mysql用户组

1
groupadd mysql
1
useradd mysql -g mysql

修改所有者和组

1
chown -R mysql.mysql /usr/local/mysql

配置

1
/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

如果出现以下错误

1
2
3
[WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
[ERROR] Child process: /usr/local/mysql/bin/mysqldterminated prematurely with errno= 32
[ERROR] Failed to execute /usr/local/mysql/bin/mysqld --bootstrap --datadir=/usr/local/mysql/data --lc-messages-dir=/usr/local/mysql/share --lc-messages=en_US --basedir=/usr/local/mysql

则使用以下命令

1
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

如果出现以下错误

1
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

则执行以下命令

1
yum -y install numactl

安装完成后再次执行前面失败的命令

1
/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

编辑my.cnf

1
vi /etc/my.cnf

将以下内容复制进去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密码验证
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

如果忽略大小写,在[mysqld]下方增加lower_case_table_names =1即可,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[mysqld]
lower_case_table_names =1
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密码验证
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

将mysql加入服务

1
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

开机启动

1
chkconfig mysql on

启动数据库

1
service mysql start

设置密码

由于my.cnf中我们设置取消密码验证,不需要密码就能进入数据库

1
/usr/local/mysql/bin/mysql -u root -p

上面命令执行后,再次回车就能进入数据库

1
use mysql;

修改密码

1
update user set authentication_string=password('你的密码') where user='root';

1
flush privileges;

退出

1
exit;

注释/etc/my.cnf的skip-grant-tables

找到skip-grant-tables 前面加个#号即可

再次设置密码

登录再次设置密码(不知道为啥如果不再次设置密码就操作不了数据库了)

1
/usr/local/mysql/bin/mysql -u root -p

1
ALTER USER 'root'@'localhost' IDENTIFIED BY '修改后的密码';

退出

1
exit;

允许远程连接

1
/usr/local/mysql/bin/mysql -u root -p
1
use mysql;
1
update user set host='%' where user = 'root';
1
flush privileges;

退出

1
exit;

添加快捷方式

1
ln -s /usr/local/mysql/bin/mysql /usr/bin

参考资料

linux上安装mysql5.7

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×