此方法是在centos7中通过源码安装mysql5.7。可定制性最强,根据需求和功能定制。安装麻烦,需要手动初始化数据库。
1.下载源码包
1、去官网下载安装包,在用xftp传到centos /usr/local/mysql目录中
http://mirrors.sohu.com/mysql/
选择mysql-boost的包下载mysql-boost-5.7.38.tar.gz
2.也可以在命令行中输入下面语句下载
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.38.tar.gz
备注:这两种方式任选其一。
2.yum安装环境
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake
3、添加用户
useradd -s /sbin/nologin mysql
4、解压安装包
tar xf mysql-boost-5.7.38.tar.gz
5、cd到mysql的配置目录中,然后编译安装
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
在执行编译安装
make && make install
6、加权限
chown -R mysql.mysql /usr/local/mysql/
7、修改配置文件
编辑etc/my.cnf文件,在末尾添加如下代码
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
skip-grant-tables
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
8、修改权限
chown mysql:mysql /etc/my.cnf
9、让其可以识别mysql中的命令
echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile
source /etc/profile
10、初始化数据库
cd到mysql的目录中,在执行如下代码
cd /usr/local/mysql/
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
11、复制启动脚本模板
cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
12、刷新启动脚本并启动
systemctl daemon-reload
systemctl start mysqld
netstat -anpt | grep 3306
systemctl enable mysqld
13、无密码登录数据库
mysql -u root -p
14、给root设置密码
use mysql;
update user set authentication_string = password('root') where user = 'root';
flush privileges;
15、授权远程登录
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
flush privileges;
16、navicat连接问题
请关闭防火墙或者在防火墙中放开3306端口
1、查看CentOS 7防火墙状态
systemctl status firewalld.service
运行上述命令后,如果看到有绿色字样标注的“active(running)”,说明防火墙是开启状态。
2、关闭运行的防火墙
systemctl stop firewalld.service
关闭后,可查看防火墙状态,当显示disavtive(dead)的字样,说明CentOS 7防火墙已经关闭。
3、开机禁用防火墙服务
systemctl stop firewalld.service