Linux installs MySQL8 and configures boot self-starting

Table of contents

1. Download the mysql installation package

2. Upload and decompress mysql

3. Modify the mysql folder name

4. Create mysql user and user group

5. Data directory

(1) Create a directory

(2) Grant authority

6. Initialize mysql

(1) Configuration parameters

 (2) Configure environment variables

(3) Initialization

7. Start MySQL

(1) start mysql

(2) Check whether MySQL starts successfully

 8. Login to MySQL

(1) Login without password

(2) Modify password

(3) Set to allow remote login

(4) Test the connection on navicat

 9. Set mysql to start automatically at boot

(1) Edit a new file autostartmysql.sh in /etc/rc.d/init.d/

(2) Give permission to autostartmysql.sh

(3) Add autostartmysql.sh to chkconfig

(4) Check whether it starts automatically after booting 


1. Download the mysql installation package

The network disk resources are as follows

Link: https://pan.baidu.com/s/1qpChiXVAGZkrDFwlxsWMcg?pwd=f4wm Extraction code: f4wm

2. Upload and decompress mysql

The location of the uploaded and decompressed folder here does not have to be the same as mine, but if it is different, pay attention to modifying the path in the following steps

Upload to the /opt folder of Linux

Extract it to the /usr/local folder

use command

 tar -xvf mysql-8.0.30-linux-glibc2.12-x86_64.tar.xz  -C /usr/local
.tar.gz后缀:tar -zxvf 文件名
.tar.xz后缀:tar -Jxvf 文件名

3. Modify the mysql folder name

mv mysql-8.0.30-linux-glibc2.12-x86_64    mysql

4. Create mysql user and user group

groupadd mysql
useradd -r -g mysql mysql

5. Data directory

(1) Create a directory

mkdir -p /usr/local/mysql8/datas

(2) Grant authority

# 更改属主和数组
chown -R mysql:mysql /usr/local/mysql8/datas
# 更改模式
chmod -R 750 /usr/local/mysql8/datas

6. Initialize mysql

(1) Configuration parameters

Below/usr/local/mysql8/ , create my.cnfa configuration file for initializing the MySQL database

[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/mysql8
# 数据存放目录
datadir    = /usr/local/mysql8/datas/mysql
log-bin    = /usr/local/mysql8/datas/mysql/mysql-bin
innodb_data_home_dir      =/usr/local/mysql8/datas/mysql
innodb_log_group_home_dir =/usr/local/mysql8/datas/mysql
#日志及进程数据的存放目录
log-error =/usr/local/mysql8/datas/mysql/mysql.log
pid-file  =/usr/local/mysql8/datas/mysql/mysql.pid
# 服务端使用的字符集默认为8比特编码
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

 (2) Configure environment variables

vim /etc/profile 或
vi /etc/profile

add at the end

export PATH=$PATH:/usr/local/mysql8/bin

update configuration

 source /etc/profile

 

(3) Initialization

mysqld --defaults-file=/usr/local/mysql8/my.cnf --basedir=/usr/local/mysql8/ --datadir=/usr/local/mysql8/datas/mysql --user=mysql --initialize-insecure

parameter:

  • defaults-file: Specify the configuration file (to be placed before –initialize)
  • user: specified user
  • basedir: Specify the installation directory
  • datadir: Specify the initialization data directory
  • intialize-insecure: Initialize without password

7. Start MySQL

(1) start mysql

mysqld_safe --defaults-file=/usr/local/mysql8/my.cnf &

(2) Check whether MySQL starts successfully

ps -ef|grep mysql

If the following information appears, the startup is successful

 8. Login to MySQL

(1) Login without password

mysql -u root --skip-password

(2) Modify password

# 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
# 刷新权限
FLUSH PRIVILEGES;

(3) Set to allow remote login

mysql> use mysql
mysql> update user set user.Host='%'where user.User='root';
mysql> flush privileges;
mysql> quit

(4) Test the connection on navicat

 9. Set mysql to start automatically at boot

(1) Edit a new file autostartmysql.sh in /etc/rc.d/init.d/

cd /etc/rc.d/init.d/
vim ./autostartmysql.sh

autostartmysql.sh content:

#!/bin/sh

# chkconfig: 2345 10 90
# description: myservice...
/usr/local/mysql8/bin/mysqld_safe --defaults-file=/usr/local/mysql8/my.cnf &

(2) Give permission to autostartmysql.sh

chmod +x ./autostartmysql.sh

(3) Add autostartmysql.sh to chkconfig

chkconfig --add ./autostartmysql.sh 
chkconfig autostartmysql.sh on

Check if the addition is successful

chkconfig --list

The new file we created has been added successfully

(4) Check whether it starts automatically after booting 

reboot 或  shutdown -r now   立即重启

after reboot

ps -ef|grep mysql

Check whether mysql starts automatically 

Guess you like

Origin blog.csdn.net/jojo_oulaoula/article/details/132412202