cnetos7下mysql-8.0.20安装
1、下载安装包
进入官网(https://dev.mysql.com/downloads/mysql/)下载指定版本的mysql安装包。(下载的是mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz压缩文件包。)
2、解压压缩文件
xz -d mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
tar xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar
然后将解压后的文件夹移动到指定的目录下:
sudo mv mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local/mysql-8.0.20
cd /usr/local/mysql-8.0.20/
新建log文件
mkdir log
3、初始化mysql
在初始化前先创建mysql的独立用户,并创建初始化配置文件。
yum -y install libaio
sudo useradd -M -s /sbin/nologin mysql
sudo chown -R mysql:mysql /usr/local/mysql-8.0.20/
vim /etc/my.cnf
然后在my.cnf中添加内容:
#dvice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
port=3306
datadir=/usr/local/mysql-8.0.20/data
#socket=/data/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
log-error=/usr/local/mysql-8.0.20/log/error.log
pid-file=/usr/local/mysql-8.0.20/log/mysqld.pid
# 忘记密码后设置登录不需要密码
#skip-grant-tables
default-authentication-plugin=mysql_native_password
character_set_server=utf8mb4
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
# 忽略表名大小写。0:表名存储为给定的大小和比较是区分大小写的 1: 表名存储在磁盘是小写的,但是比较的时候是不区分大小写 2: 表名存储为给定的大小写但是比较的时候是小写的
lower_case_table_names=1
log-bin-trust-function-creators=1
# 开启慢查询日志
#log-bin=mysql-bin
slow_query_log=on
slow_query_log_file=/usr/local/mysql-8.0.20/log/slow_query.log
long_query_time=10
max_connections=10000
interactive_timeout = 500
wait_timeout = 300
[client]
default-character-set = utf8mb4
保存退出文件编辑。
执行初始化操作:
sudo ./bin/mysqld --user=mysql --basedir=/usr/local/mysql-8.0.20/ --datadir=/usr/local/mysql-8.0.20/data/ --initialize
初始化操作完成后,通过日志查看初始化时的密码:
sudo cat log/error.log
4、配置mysql服务
首先将mysql添加到环境变量,这样可以在磁盘任何地方进行调用mysql命令:
sudo vim /etc/profile
# 在文件末尾处进行添加
PATH=$PATH:/usr/local/mysql-8.0.20/bin
export PATH
source /etc/profile
配置mysql服务:
sudo vim /usr/lib/systemd/system/mysqld.service
# 在文件中进行添加
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql-8.0.20/bin/mysqld
LimitNOFILE = 65536
LimitNPROC = 65536
sudo systemctl daemon-reload
启动mysql前,查看my.cnf文件,修改socket路径
mkdir /var/lib/mysql
cd /var/lib/mysql
touch my.sock
chmod -R 777 /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
建立软链接
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
测试服务是否正确配置:
sudo systemctl start mysqld
sudo systemctl status mysqld
sudo systemctl stop mysqld
systemctl restart mysqld
ps -ef|grep mysql
ps:可能会出现sock占用,去把tmp下的sock文件删除即可
设置mysql开机自启动:
sudo systemctl enable mysqld
5、修改mysql初始密码
mysql的初始密码必须进行修改,否则无法进行其他操作。登录mysql修改密码,初始密码即为上面日志文件中的密码:
mysql> ALTER USER root@'localhost' IDENTIFIED BY '123456';
mysql> flush privileges;