centos7.5安装MySQL

1.下载MySQL

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

2.解压MySQL

tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
# 重命名
mv mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql

3.创建目录

cd /usr/local/mysql
mkdir -p {arch,data,tmp,relay_log}

4.安装依赖

yum -y install libaio

5.修改mysql.cfg

vi /etc/my.cnf

清空my.cnf所有内容,粘贴一下内容:

[client]
port= 3306
socket= /usr/local/mysql/data/mysql.sock
default-character-set=utf8mb4
 
[mysqld]
port= 3306
socket= /usr/local/mysql/data/mysql.sock
 
skip-slave-start
 
skip-external-locking
key_buffer_size=256M
sort_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=4M
query_cache_size= 32M
max_allowed_packet=16M
myisam_sort_buffer_size=128M
tmp_table_size=32M
 
table_open_cache=512
thread_cache_size=8
wait_timeout=86400
interactive_timeout=86400
max_connections=600
 
# Try number of CPU's*2 for thread_concurrency
#thread_concurrency=32 
 
#isolation level and default engine 
default-storage-engine=INNODB
transaction-isolation=READ-COMMITTED
 
server-id= 1
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/data/hostname.pid
 
#open performance schema
log-warnings
sysdate-is-now
 
binlog_format=ROW
log_bin_trust_function_creators=1
log-error= /usr/local/mysql/data/hostname.err
log-bin=/usr/local/mysql/arch/mysql-bin
expire_logs_days=7
 
innodb_write_io_threads=16
 
relay-log= /usr/local/mysql/relay_log/relay-log
relay-log-index=/usr/local/mysql/relay_log/relay-log.index
relay_log_info_file= /usr/local/mysql/relay_log/relay-log.info
 
log_slave_updates=1
gtid_mode=OFF
enforce_gtid_consistency=OFF
 
# slave
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=4
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
 
#other logs
#general_log =1
#general_log_file= /usr/local/mysql/data/general_log.err
#slow_query_log=1
#slow_query_log_file=/usr/local/mysql/data/slow_log.err
 
#for replication slave
sync_binlog=500
 
 
#for innodb options 
innodb_data_home_dir=/usr/local/mysql/data/
innodb_data_file_path=ibdata1:10M;ibdata2:10M:autoextend
#由于我是在本机的虚拟上安装的,磁盘空间有限,所以这里设置的非常小    
#innodb_data_file_path=ibdata1:1G;ibdata2:1G:autoextend
 
innodb_log_group_home_dir=/usr/local/mysql/arch
innodb_log_files_in_group=4
innodb_log_file_size=1G
innodb_log_buffer_size=200M
 
#根据生产需要,调整pool size 
innodb_buffer_pool_size=2G
#innodb_additional_mem_pool_size=50M #deprecated in 5.6
tmpdir=/usr/local/mysql/tmp
 
innodb_lock_wait_timeout=1000
#innodb_thread_concurrency=0
innodb_flush_log_at_trx_commit=2
 
innodb_locks_unsafe_for_binlog=1
 
#innodb io features: add for mysql5.5.8
performance_schema
innodb_read_io_threads=4
innodb-write-io-threads=4
innodb-io-capacity=200
#purge threads change default(0) to 1 for purge
innodb_purge_threads=1
innodb_use_native_aio=on
 
#case-sensitive file names and separate tablespace
innodb_file_per_table=1
lower_case_table_names=1
 
[mysqldump]
quick
max_allowed_packet=128M
 
[mysql]
no-auto-rehash
default-character-set=utf8mb4
 
[mysqlhotcopy]
interactive-timeout
 
[myisamchk]
key_buffer_size=256M
sort_buffer_size=256M
read_buffer=2M
write_buffer=2M

6.配置环境变量

vim /etc/profile
#配置内容
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin

#执行使配置生效
source /etc/profile

7.创建MySQL用户、MySQL组

groupadd -g 101 mysql
useradd -u 514 -g mysql -G root -d /usr/local/mysql mysql
usermod -u 514 -g mysql -G root -d /usr/local/mysql mysql
cp /etc/skel/.bash* /usr/local/mysql

8.修改.bash_profile配置文件

#是/usr/local/mysql目录下的.bash_profile文件
vi .bash_profile

清空文件内容,替换内容:

# .bash_profile
# Get the aliases and functions

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs
export MYSQL_BASE=/usr/local/mysql
export PATH=${MYSQL_BASE}/bin:$PATH

unset USERNAME

#stty erase ^H
set umask to 022
umask 022
PS1=`uname -n`":"'$USER'":"'$PWD'":>"; export PS1

9.赋权限和用户组

chown  mysql:mysql /etc/my.cnf
chmod  640 /etc/my.cnf
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql

10.初始化MySQL

#切换到mysql用户
su - mysql

#初始化mysql的db
bin/mysqld --defaults-file=/etc/my.cnf --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --initialize
#在初始化时如果加上 –initial-insecure,则会创建空密码的 root@localhost 账号,否则会创建带密码#的 root@localhost 账号,密码直接写在 log-error 日志文件中
#(在5.6版本中是放在 ~/.mysql_secret 文件里,更加隐蔽,不熟悉的话可能会无所适从)

11.查看默认root密码

cat data/hostname.err |grep password

将要密码记录下来

12.启动mysql

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &

13.修改默认密码

#使用默认密码登录MySQL
bin/mysql -uroot -p'0PlKjot.5)gL'

#修改密码
alter user root@localhost identified by '123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;
exit;
#并退出MySQL账号

14.MySQL开机启动

#将服务文件拷贝到init.d下,并重命名为mysql
cp support-files/mysql.server /etc/rc.d/init.d/mysqld 
#赋予可执行权限
chmod +x /etc/rc.d/init.d/mysqld
#删除服务
chkconfig --del mysqld
#添加服务
chkconfig --add mysqld
chkconfig --level 345 mysqld on

15.重启MySQL

#重启mysql
service mysqld restart
#登录mysql
mysql -uroot -p123456

本文参考:点击

如果觉得我的文章对你有用,请随意赞赏