Centos部署MySQL 5.7



01.下载安装程序并安装依赖包:

cd /root/Downloads
wget http://download.wenjun1984.cn/MySQL/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
yum install libaio

02.添加用户和用户组:

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

03.解压mysql安装包并且修改其名称:

tar -zxvf /root/Downloads/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local
mv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64/ /usr/local/mysql
chown -R mysql.mysql /usr/local/mysql

04.执行初始化脚本:

/usr/local/mysql/bin/mysqld --initialize \
--user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

注:执行初始化脚本,之后会生成一个临时密码,比如:07.ex%rnVadf,妥善保存它。

05.最小化编辑my.cnf:

vim /etc/my.cnf,输入以下配置:
[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/

06.设置服务启动脚本:

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

07.启动服务并加入开机自启:

chkconfig mysql on
service mysql start

08.建立对应mysql启动程序的软链接,并建立mysql链接:

ln -s /usr/local/mysql/bin/mysql /usr/bin
mysql -uroot -p07.ex%rnVadf

注:此处的密码为之前生成的临时密码。

09.修改mysql中的root用户密码并删除多余用户:

alter user user() identified by "xxxxxxxxx";
create user root@127.0.0.1 identified by 'xxxxxxxxx';
create user root@'%' identified by 'xxxxxxxxx';
grant all privileges on *.* to root@127.0.0.1 WITH GRANT OPTION;
grant all privileges on *.* to root@'%' WITH GRANT OPTION;
flush privileges;
exit;

注: 需要添加一个本地管理员,避免禁止反解后不能登录。

10.修改mysql配置文件并重启:

vim /etc/my.cnf,覆盖全部配置:
[mysql]
user='root'
password='xxxxxxxxx'
host='localhost'
socket=/usr/local/mysql/mysql.sock

[client]
default-character-set = utf8mb4

[mysqld]
character_set_server=utf8mb4
init-connect='SET NAMES utf8mb4'
skip-character-set-client-handshake=1
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
socket=/usr/local/mysql/mysql.sock
skip-name-resolve
max_connections=1024
wait_timeout=300
interactive_timeout=300
max_allowed_packet=16M
back_log=300

server-id=1

log-bin=/usr/local/mysql/data/mysql-binlog
binlog_format=ROW
binlog_row_image=full
general_log=1
general_log_file=/usr/local/mysql/data/mysql-general.log
slow_query_log=1
slow_query_log_file=/usr/local/mysql/data/mysql-slow.log
long_query_time=10
sync_binlog=1
expire_logs_days=7

table_open_cache=256
thread_cache_size=16
thread_stack=512K
query_cache_size=128M
query_cache_limit=4M 

default-storage-engine=INNODB
innodb_buffer_pool_size=2048M
innodb_file_per_table=1
innodb_support_xa = 1
innodb_buffer_pool_dump_at_shutdown=1
innodb_rollback_on_timeout=on
innodb_flush_log_at_trx_commit=2 

sort_buffer_size=256K
join_buffer_size=256K
symbolic-links=0 

[mysqld_safe]
log-error=/usr/local/mysql/data/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid

[mysqldump]
user='root'
password='xxxxxxxxx'
host='localhost'
socket=/usr/local/mysql/mysql.sock

11.重新启动mysql并使用客户端连接:

service mysql restart
mysql
文档更新时间: 2023-05-24 17:01   作者:闻骏