一.环境声明
代码语言:javascript复制[mysql-Server]
主机名 = host-1
系统 = centos-7.3
地址 = 1.1.1.1
软件 = mysql-5.7 3306
使用二进制包部署会下载比较大,大约500M左右,而源码包就几十M。但使用二进制包不用编译,部署较快,相比于yum可以自定义目录,方便维护。
二.程序部署
1.安装依赖
yum -y install autoconf libaio bison ncurses-devel
2.创建用户
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
3.清理centos7 默认自带数据库,一定要清理干净
yum -y remove mariadb mariadb-server
4.下载二进制包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
tar -xf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
移动过去,后面是存放路径
mv mysql-5.7.21-linux-glibc2.12-x86_64 /usr/local/mysql
mkdir /var/log/mysql
mkdir /usr/local/mysql/data
mysql即使root启动还是mysql用户运行的,所以要给权限
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /var/log/mysql
5.进去删除所有行,添加新的
vim /etc/my.cnf
[client]
port =
3306
socket =
/tmp/mysql.sock
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid
6.初始化
cd /usr/local/mysql/bin
初始化数据库,没有反应则正确,这里可以自定义目录
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --lc_messages_dir=/usr/local/mysql/share --lc_messages=en_US
7.加入systemctl控制
vim /usr/lib/systemd/system/mysql.service
[Unit]
Description=mysql
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/usr/local/mysql/support-files/mysql.server restart
ExecStop=/usr/local/mysql/support-files/mysql.server stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
8.设置环境变量
echo 'PATH=$PATH':/usr/local/mysql/bin >> /etc/profile
source /etc/profile
9.启动
systemctl daemon-reload
systemctl start mysql
systemctl enable mysql
三.更改初始密码
1.查看密码
cat /var/log/mysqld.log
最后一行:root@localhost:
这里就是初始密码
需要进入数据库用如下命令修改密码才能让数据库正常使用
mysql -uroot -p
ALTER USER USER() IDENTIFIED BY '123456';