1、创建MySQL虚拟用户
groupadd mysql
useradd -g mysql -s /sbin/nologin -M mysql
2、创建目录
mkdir -p /data/file #创建文件目录
mkdir -p /opt/mysql #创建MySQL安装目录
mkdir -p /data/mysql/mysql3306/{data,logs} #创建MySQL数据及日志目录
3、安装MySQL
a、解压二进制包
tar xf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
b、将软件部分移动到指定目录
mv mysql-5.7.36-linux-glibc2.12-x86_64 /opt/mysql/mysql-5.7.36
c、创建软连接
ln -s /opt/mysql/mysql-5.7.36/ /usr/local/mysql
d、删除系统MySQL
rpm -e --nodeps mariadb-libs
e、配置文件整理
vi /data/mysql/mysql3306/my3306.cnf
[client]
#password = your_password
port = 3306
socket = /data/mysql/mysql3306/mysql.sock
default-character-set=utf8
[mysqld]
collation-server = utf8_general_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
binlog_cache_size=20M
thread_stack = 512K
join_buffer_size=20M
query_cache_type = 1
max_heap_table_size = 2048M
port=3306
socket=/data/mysql/mysql3306/mysql.sock
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/mysql3306/data
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
key_buffer_size = 1024M
max_allowed_packet=100G
table_open_cache = 4096
sort_buffer_size=4M
net_buffer_length = 4K
read_buffer_size = 4096K
read_rnd_buffer_size = 2048K
myisam_sort_buffer_size = 128M
thread_cache_size = 512
query_cache_size = 384M
tmp_table_size = 2048M
max_connections=10240
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server_id=1
expire_logs_days=7
slow_query_log=on
slow_query_log_file=/data/mysql/mysql3306/logs/slow.log
long_query_time=0.5
log_queries_not_using_indexes=1
log_error=/data/mysql/mysql3306/logs/error.log
log_bin=/data/mysql/mysql3306/logs/mysql-bin
gtid_mode=on
enforce_gtid_consistency=true
log_slave_updates=1
wait_timeout=1800
innodb_buffer_pool_size=8096M
innodb_log_file_size=256M
innodb_log_buffer_size=64M
innodb_flush_log_at_trx_commit=1
innodb_log_files_in_group=2
max_binlog_cache_size=8096M
max_binlog_size=8096M
secure_file_priv=/tmp/
binlog_group_commit_sync_delay=100 #主库
binlog_group_commit_sync_no_delay_count=20 #主库
slave_parallel_type=logical_clock #从库配置
slave_parallel_workers=4 #从库配置
transaction_write_set_extraction=XXHASH64 #从库配置
binlog_transaction_dependency_tracking=writeset #从库配置
slave_preserve_commit_order=1 #从库配置
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#备份MySQL数据库的工具,可以将数据库导出为SQL文件
[mysqldump]
quick
max_allowed_packet = 500M
#字符集默认设置及禁用自动补全功能
[mysql]
no-auto-rehash
default-character-set=utf8
#用于检查和修复MyISAM存储引擎的表的工具
[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 20M
read_buffer = 20M
write_buffer = 20M
#用于复制MyISAM数据库的工具,通常用于备份
[mysqlhotcopy]
interactive-timeout
f、安装MySQL依赖包
yum install libaio-devel net-tools perl wget numactl -y
g、更改MySQL相关目录的用户组
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mys*
h、初始化数据库
a、没有密码
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data
b、生成临时密码(临时密码生成在logs/error.log文件中)
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/mysql3306/data
**如果生成了临时密码,第一次是需要修改密码的,获取临时密码 grep 'temporary password' /data/mysql/mysql3306/logs/error.log
**alter user root@'localhost' identified by '7E%mAt8mbZXQC1xy'; //第一次修改密码
**mysqladmin -uroot -p123456 -S /data/mysql/mysql3306/mysql.sock password '7E%mAt8mbZXQC1xy' //知道密码而在修改密码
i、加入环境变量
vi /etc/profile
export PATH="/usr/local/mysql/bin:$PATH"
source /etc/profile
j、释放端口
查看开放的端口 firewall-cmd --list-ports
开启3306端口 firewall-cmd --zone=public --add-port=3306/tcp --permanent
刷新防火墙 firewall-cmd --reload
4、启动MySQL
mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &
5、查看是否启动成功
安装netstat命令 yum install net-tools
netstat -lntup | grep mysql
6、连接MySQL
mysql -uroot -p -S /data/mysql/mysql3306/mysql.sock
7、MySQL启动方式
a、service方式
第一步 vi /usr/local/mysql/support-files
if test -z "$basedir"
then
basedir=/usr/local/mysql #此处需要修改成对应的MySQL安装路径
bindir=/usr/local/mysql/bin #此处需要修改成对应的bin文件路径
if test -z "$datadir"
then
datadir=/data/mysql/mysql3306/data #此处需要修改成对应的数据文件路径
fi
sbindir=/usr/local/mysql/bin
libexecdir=/usr/local/mysql/bin
else
bindir="$basedir/bin"
if test -z "$datadir"
then
datadir="$basedir/data"
fi
sbindir="$basedir/sbin"
libexecdir="$basedir/libexec"
fi
第二步 cp ./mysql.server /etc/init.d/mysqld3306
启动命令 service mysqld3306 start
关闭命令 service mysqld3306 stop
b、systemctl方式
vi /etc/systemd/system/mysqld3306.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=netwoek.target
After=syslog.target
[Install]
wantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf
LimitNOFILE=5000
[Install]
WantedBy=multi-user.target
加入开机启动 systemctl enable mysqld3306.service
启动命令 systemctl start mysqld3306.service
关闭命令 systemctl stop mysqld3306.service
c、mysqld_safe方式
启动命令 mysqld_safe --defaults-file=/data/mysql/mysql3306/my3306.cnf &
关闭命令 mysqladmin -S /data/mysql/mysql3306/mysql.sock shutdown
d、mysqld方式
启动命令 mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &
关闭命令 mysqladmin -S /data/mysql/mysql3306/mysql.sock shutdown
e、启动顺序总结
service->mysqld_safe->mysqld-->启动MySQL
systemctl->mysqld-->启动MySQL
mysqld_safe->mysqld-->启动MySQL
mysqld-->启动MySQL
8、找回丢失的用户密码
a、关闭MySQL进程
b、启动MySQL进程
mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf --skip-grant-tables &
c、连接MySQL数据库实例
mysql -S /data/mysql/mysql3306/mysql.sock
flush privileges;
d、修改用户密码
alter user root@'localhost' identified by '7E%mAt8mbZXQC1xy';
e、关闭数据库
f、启动数据库进程
mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &
g、连接MySQL实例
mysql -uroot -p -S /data/mysql/mysql3306/mysql.sock