安装前检查是否之前安装过,删除历史版本
1. 检查历史版本
rpm -qa | grep -i mysql
rpm -qa | grep -i mariadb
mariadb为mysql项目一个分支,在使用上很多方面与mysql非常类似,像centos7,mariadb已经替代mysql作为默认yum安装的数据库,故检查历史版本时会一起检查。
2. 卸载历史版本(如已安装过)
yum remove -y mysql*
yum remove -y mariadb*
3. 清理残留数据目录及文件
- 删除安装目录(示例)
# 获取安装目录
whereis mysql
# 删除安装目录
rm -rf /usr/lib64/mysql /usr/share/mysql
# 删除数据目录(示例)
rm -rf /var/lib/mysql
#删除配置文件
rm -rf /etc/my.cnf
# 删除日志文件
rm -rf /var/log/mysql
rm -rf /var/log/mysqld.log
#删除临时文件
rm -rf /tmp/mysql*
#删除服务和启动脚本
rm -rf /etc/init.d/mysql
rm -rf /usr/lib/systemd/system/mysql.service
开始安装
1. 下载安装包
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.30-el7-x86_64.tar
2. 解压
tar -xvf mysql-8.0.30-el7-x86_64.tar
tar -zxvf mysql-8.0.30-el7-x86_64.tar.gz
mv mysql-8.0.30-el7-x86_64/ /usr/local/mysql
cd /usr/local/mysql
#存储数据文件**
mkdir data
5.设置用户组并赋权
#创建用户组
groupadd mysql
#创建用户
#-r:创建系统用户
#-g:指定用户组
useradd -r -g mysql mysql
#更改属主和数组
chown -R mysql:mysql /usr/local/mysql/
#更改权限
chmod -R 755 /usr/local/mysql/
配置文件
vi /usr/local/etc/my.cnf
[mysql]
# 默认字符集
default-character-set=utf8mb4
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
port = 3306
server-id = 3306
user = mysql
socket = /tmp/mysql.sock
# 安装目录
basedir = /usr/local/mysql
# 数据存放目录
datadir = /home/mysql/data
log-bin = /home/mysql/data/mysql-bin
innodb_data_home_dir =/home/mysql/data/mysql
innodb_log_group_home_dir =/home/mysql/data/mysql
# 日志及进程数据的存放目录
log-error =/home/mysql/data/mysql/mysql.log
pid-file =/home/mysql/data/mysql/mysql.pid
# 服务端字符集
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
6.初始化MySQL
进入MySQL的bin目录
cd /usr/local/mysql/bin/
#初始化
./mysqld --defaults-file=/usr/local/etc/my.cnf --initialize --user=mysql --datadir=/home/mysql/data --basedir=/usr/local/mysql
注: 初始化完成后会打印随机密码,如: A temporary password is generated for root@localhost: 3c-<%>rtsltA
如提示异常:./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory 执行: yum install libaio
7. 启动 mysql 服务
#### 设置软连接,并重启MySQL
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/mysql.sock /var/mysql.sock
service mysql start
8. 设置开机启动
systemctl enable mysql
9. 登录mysql
# 密码为上一步获取的初始密码
mysql -u root -p
10. 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '8@123456';
11. 设置远程访问(可选)
CREATE USER 'root'@'%' IDENTIFIED BY '8@123456';
GRANT ALL ON *.* TO 'root'@'%';
FLUSH PRIVILEGES;
至此,服务安装完毕。
其他命令
SELECT User, Host FROM mysql.user WHERE User = 'root';
ALTER USER 'root'@'%' IDENTIFIED BY '8@123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;