# 安装相关软件
yum install -y gcc gcc-c++ openssl openssl-devel ncurses ncurses-devel make cmake
# 获取MySQL源码,MySQL版本下载链接:https://dev.mysql.com/downloads/mysql/
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.13.tar.gz
tar zxvf mysql-8.0.13.tar.gz
cd mysql-8.0.13
# 添加操作系统的MySQL用户
groupadd -r mysql
mkdir -p /data/mysql/data
chown -R mysql.mysql /data/mysql/data/
# 添加配置文件目录
mkdir -p /etc/mysql
chown -R mysql.mysql /etc/mysql
# 编译MySQL
mkdir -p /data/mysql
chown -R mysql.mysql /data/mysql
# -DCMAKE_INSTALL_PREFIX:指定安装目录
# -DMYSQL_DATADIR:数据文件存放位置
# -DSYSCONFDIR:配置文件目录
# -DDOWNLOAD_BOOST:实时下载boost
# -DWITH_BOOST:下载boost保存目录
cmake . -DCMAKE_INSTALL_PREFIX=/data/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/etc/mysql -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/data/mysql/boost
make
make install
# 对安装录更改用户权限
chown root.mysql -R /data/mysql/
# 安装完成后,进入安装目录中,对mysql进行初始化操作
cd /data/mysql
bin/mysqld --initialize
# 拷贝服务脚本,赋权限并添加到启动列表中
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
# 修改配置文件
vi /etc/mysql/my.cnf
输入:
[mysqld]
basedir = /data/mysql
datadir = /data/mysql/data
port = 3306
server_id = 1
socket = /tmp/mysql.sock
user = mysql
[client]
port = 3306
socket = /tmp/mysql.sock
# 启动服务
bin/mysqld &
至此MySQL已经安装完成了,接下来是一些优化的操作
# 配置环境变量
echo "export PATH=/data/mysql/bin:$PATH" >/etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
# 删除系统安装完成后产生的匿名用户并给root设置密码
# 若登录mysql时,忘记了用户密码可以在启动mysqld服务时加--skip-grant-tables即可直接登录: mysqld --skip-grant-tables &
# 登录mysql
mysql -uroot -hlocalhost -p
# 删除所有匿名用户,以下是删除的命令
drop user 'username'@'localhost';
# 给用户设定密码的两种方式
1. UPDATE user SET password = PASSWORD('your_password') WHERE user = 'username';
FLUSH PRIVILEGES;
2. SET PASSWORD FOR [email protected] = PASSWORD('your_password');
# 创建用户并授权用户
create user 'username'@'localhost' identified by 'your_password';
create database database_name default character set = 'utf8' default collate = 'utf8_general_ci';
grant all privileges on database_name.* to 'username'@'localhost';
flush privileges;
原文地址:https://www.cnblogs.com/phonecom/p/11213346.html
时间: 2024-10-09 06:06:18