阿里云安装Mysql
1、下载mysql包,并解压包
下载:mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar
解压:tar -xf mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar
备注:其余的2个tar.xz没得用,rm删除掉
2、继续解压文件
tar -Jxf mysql-8.0.28-linux-glibc2.17-x86_64-minimal.tar.xz
3、拷贝到安装目录
mv ./mysql-8.0.28-linux-glibc2.17-x86_64-minimal /usr/local/mysql8
4、创建mysql组和用户
groupadd mysql useradd -r -g mysql mysql
5、创建mysql数据目录(根目录)
cd / && mkdir -p /data/mysql8_data/
6、赋予权限
chown mysql:mysql -R /data/mysql8_data
chmod 750 /data/mysql8_data/ -R
7、加入路径
export PATH=$PATH:/usr/local/mysql8/bin
8、配置文件顺序:
1. /etc/my.cnf
2. 2./etc/mysql/my.cnf
3. 3./usr/local/mysql/etc/my.cnf
4. 4.~/.my.cnf
创建my.cnf文件:
vi /etc/my.cnf
powershell
复制代码
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
server-id = 3306
user = mysql
socket = /tmp/mysql.sock
# 设置mysql的安装目录
basedir = /usr/local/mysql8
# 设置mysql数据库的数据的存放目录
datadir = /data/mysql8_data/mysql
log-bin = /data/mysql8_data/mysql/mysql-bin
innodb_data_home_dir =/data/mysql8_data/mysql
innodb_log_group_home_dir =/data/mysql8_data/mysql
#设置mysql数据库的日志及进程数据的存放目录
log-error =/data/mysql8_data/mysql/mysql.log
pid-file =/data/mysql8_data/mysql/mysql.pid
# 服务端使用的字符集默认为8比特编码
character-set-server=utf8mb4
lower_case_table_names=1
autocommit =1
##################以上要修改的########################
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 1024
sort_buffer_size = 4M
net_buffer_length = 8K
read_buffer_size = 4M
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 64M
thread_cache_size = 128
#query_cache_size = 128M
tmp_table_size = 128M
explicit_defaults_for_timestamp = true
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
binlog_format=mixed
binlog_expire_logs_seconds =864000
# 创建新表时将使用的默认存储引擎
default_storage_engine = InnoDB
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 1024M
innodb_log_file_size = 256M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
transaction-isolation=READ-COMMITTED
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 4M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
9、初始化mysql
cd /usr/local/mysql8/bin
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql8 --datadir=/data/mysql8_data/mysql --user=mysql --initialize
10、启动mysql(结尾加"&"为后台启动)
./mysqld_safe --defaults-file=/etc/my.cnf &
11、查看启动log中生成mysql密码
cat /data/mysql8_data/mysql/mysql.log
2022-03-22T11:05:18.173249Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: %lgAeFSY&9NC
12、修改新密码
#首次改密推荐使用本地密码插件with mysql_native_password
ALTER USER 'root'@'localhost' IDENTIFIED with mysql_native_password BY '12345678';
#刷新权限
flush privileges;
13、创建远程访问授权
use mysql;
select user,host,plugin,authentication_string from user;
CREATE user 'root'@'%'; #创建用户任意远程访问
alter user 'root'@'%' identified with mysql_native_password by '12345678'; #修改密码
grant all privileges on *.* to "root"@"%"; #给用户授权
flush privileges; #刷新权限
14、添加mysql服务
#确保my.cnf在路径/etc/my.cnf
cd /usr/local/mysql8/
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on
启动:service mysqld start
停止:service mysqld stop
重启:service mysqld restart
重载配置:service mysqld reload
15、(忽略)防火墙:开启3306端口,--permanent永久生效,没有此参数重启后失效。
firewall-cmd --zone=public --add-port=3306/tcp --permanent