特别注意: 文档中涉及到密码的都是用的是弱密码,是存在安全风险的,一定要根据自己的情况修改为复杂度更高
的密码!
- centos 7.6
- mysql 5.7.31
- 基础目录:
/srv/{app,data,logs}/mysql
准备安装
下载并解压
yum install libaio -y
useradd -c 'MySQL Server' -d /var/lib/mysql -s /bin/false -r -M mysql
cd /srv/app
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
tar mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.31-linux-glibc2.12-x86_64 mysql
mkdir -p /srv/{data,logs}/mysql /var/run/mysql
chown -Rf mysql:mysql /srv/{app,data,logs}/mysql /var/run/mysql
进行安装
配置my.conf
[mysqld]
basedir=/srv/app/mysql
datadir=/srv/data/mysql
socket=/srv/data/mysql/mysql.sock
default_password_lifetime = 0
max_allowed_packet = 128M
max_connections = 5000
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
federated
skip-name-resolve
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
innodb_buffer_pool_size = 2G
innodb_log_buffer_size=16M
innodb_log_file_size = 256M
innodb_log_files_in_group=2
innodb_max_dirty_pages_pct=75
innodb_flush_method=O_DIRECT
innodb_read_io_threads=8
innodb_write_io_threads=10
innodb_thread_concurrency=32
innodb_io_capacity=5000
read_buffer_size=8M
read_rnd_buffer_size=8M
sort_buffer_size=8M
join_buffer_size=4M
tmp_table_size=16M
query_cache_type=0
query_cache_size=0
thread_cache_size = 16
open_files_limit = 65535
innodb_flush_log_at_trx_commit = 2
skip-external-locking
key_buffer_size = 16M
table_open_cache = 64
net_buffer_length = 8K
myisam_sort_buffer_size = 8M
lower_case_table_names=1
bulk_insert_buffer_size = 1M
group_concat_max_len = 204800
symbolic-links=0
log_timestamps=SYSTEM
log-error=/srv/logs/mysql/mysqld.log
pid-file=/var/run/mysql/mysqld.pid
slow-query-log=On
slow_query_log_file=/srv/logs/mysql/slow-query.log
[mysql]
auto-rehash
socket=/srv/data/mysql/mysql.sock
[client]
default-character-set=utf8
socket=/srv/data/mysql/mysql.sock
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
初始化MySQL
cd /srv/app/mysql
./bin/mysqld --user=mysql --basedir=/srv/app/mysql --datadir=/srv/data/mysql --pid-file=/var/run/mysql/mysqld.pid --initialize
查看默认root用户的密码
grep 'temporary password' /srv/logs/mysql/mysqld.log
配置开机启动项
- 办法1(推荐使用)
[root@localhost ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/var/run/mysql/mysqld.pid
TimeoutSec=0
PermissionsStartOnly=true
ExecStartPre=/usr/bin/mkdir -p /var/run/mysql
ExecStartPre=/usr/bin/chown mysql:mysql /var/run/mysql
ExecStart=/srv/app/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysql/mysqld.pid
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
[Install]
WantedBy=multi-user.target
- 办法2:
cp -r /srv/app/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
以上两种办法任选一种就可以啦, 要想将mysql
加入到开机启动可以执行 systemctl enable mysqld
修改root用户的密码
可以使用上面初始化后的密码进行登录
shell>mysql -u root -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
创建可以远程的用户
因为不建议直接用root用户进行远程
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;