一、准备环境
Mysql 安装包
下载地址
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
二、解压安装包
2.1我这里使用的目录/home/ap
tar -zvxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C
2.2修改名称
mv mysql-5.7.26-linux-glibc2.12-x86_64 mysql
2.3创建data 和 log目录(我这里没有创建,初始化失败,可以不创建初始化尝试一下)
三、创建Mysql用户
useradd mysql
3.1 赋予mysql 目录 Mysql权限
chown mysql.mysql -R /home/ap/mysql
3.2初始化
/home/ap/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/home/ap/mysql --datadir=/home/ap/mysql/data --innodb_undo_tablespaces=3 --explicit_defaults_for_timestamp
3.2查询登录密码
grep 'password' /home/ap/mysql/log/mysql-error.log
3.3修改启动服务指定data 和 base目录(可以忽略这步)
vi /home/ap/mysql/support-files/mysql.server basedir=/home/ap/mysql datadir=/home/ap/mysql/data
3.4拷贝启动配置文件
cp /home/ap/mysql/support-files/mysql.server /etc/init.d/mysql
3.5赋予执行权限
chmod +x /etc/init.d/mysql
四、编辑/etc/my.cnf配置文件
4.1
vi /etc/my.cnf [client] port = 31061 socket = /home/ap/mysql/mysql.sock [mysqld] server_id=10 port = 3306 user = mysql character-set-server = utf8mb4 default_storage_engine = innodb log_timestamps = SYSTEM socket = /home/ap/mysql/mysql.sock basedir =/home/ap/mysql datadir = /home/ap/mysql/data pid-file = /home/ap/mysql/mysql.pid max_connections = 1000 max_connect_errors = 1000 table_open_cache = 1024 max_allowed_packet = 128M open_files_limit = 65535 server-id=1 gtid_mode=on enforce_gtid_consistency=on log-slave-updates=1 log-bin=master-bin log-bin-index = master-bin.index relay-log = relay-log relay-log-index = relay-log.index binlog_format=row log_error = /home/ap/mysql/log/mysql-error.log skip-name-resolve log-slave-updates=1 relay_log_purge = 0 slow_query_log = 1 long_query_time = 1 slow_query_log_file = /home/ap/mysql/log/mysql-slow.log
4.1启动Mysql
/etc/init.d/mysql start
4.2登录mysql
mysql -uroot -p查询的密码
4.3修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Aipf@123';
4.4成功
脚本
#!/bin/bash
WORK_DIR=/home/ap
MYSQL_FILE=$WORK_DIR/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
MYSQL_NAME=$WORK_DIR/mysql-5.7.26-linux-glibc2.12-x86_64
MY_MYSQL=$WORK_DIR/mysql
MYSQL_DATA=$MY_MYSQL/data
MYSQL_LOG=$MY_MYSQL/log
#解压Mysql安装包
tar -zxvf $MYSQL_FILE -C $WORK_DIR
mv $MYSQL_NAME $MY_MYSQL
mkdir -p $MYSQL_DATA $MYSQL_LOG
#创建mysql用户
useradd mysql
chown mysql.mysql -R $WORK_DIR
#初始化
$MY_MYSQL/bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql --basedir=/home/ap/mysql --datadir=/home/ap/mysql/data --innodb_undo_tablespaces=3 --explicit_defaults_for_timestamp
#备份原来的my.cnf
mv /etc/my.cnf /etc/my.cnf.bak
#
echo > /etc/my.cnf
cat >> /etc/my.cnf << EOF
[client]
port = 31061
socket = /home/ap/mysql/mysql.sock
[mysqld]
server_id=10
port = 3306
user = mysql
character-set-server = utf8mb4
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /home/ap/mysql/mysql.sock
basedir =/home/ap/mysql
datadir = /home/ap/mysql/data
pid-file = /home/ap/mysql/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
server-id=1
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row
log_error = /home/ap/mysql/log/mysql-error.log
skip-name-resolve
log-slave-updates=1
relay_log_purge = 0
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /home/ap/mysql/log/mysql-slow.log
EOF
cat >> ~/.bashrc << EOF
#ADD mysql
export PATH=\$PATH:/home/ap/mysql/bin
EOF
#拷贝配置文件
cp $MY_MYSQL/support-files/mysql.server /etc/init.d/mysql
#启动mysql
/etc/init.d/mysql start
#查询密码
grep 'password' $MYSQL_LOG/mysql-error.log
echo -e "Run: mysql -uroot -p"