目录
(1)在/etc/rc.d/init.d/编辑一个新文件autostartmysql.sh
(3) 将autostartmysql.sh添加到 chkconfig 中
1.下载 mysql 安装包
网盘资源如下
链接:https://pan.baidu.com/s/1qpChiXVAGZkrDFwlxsWMcghttps://blog.csdn.net/jojo_oulaoula/article/details/?pwd=f4wm 提取码: f4wm
2.上传并解压 mysql
这里的上传和解压的文件夹位置不必和我这一样,但是如果不一样要注意修改下面步骤中的路径
上传至 Linux 的/opt文件夹
解压到/usr/local文件夹下
使用命令
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.修改 mysql 文件夹名
mv mysql-8.0.30-linux-glibc2.12-x86_64 mysql
4.创建mysql 用户和用户组
groupadd mysqluseradd -r -g mysql mysql
5.数据目录
(1)创建目录
mkdir -p /usr/local/mysql8/datas
(2)赋予权限
# 更改属主和数组chown -R mysql:mysql /usr/local/mysql8/datas# 更改模式chmod -R 750 /usr/local/mysql8/datas
6.初始化mysql
(1)配置参数
在/usr/local/mysql8/
下,创建my.cnf
配置文件,用于初始化MySQL数据库
[mysql]# 默认字符集default-character-set=utf8mb4[client]port = 3306socket = /tmp/mysql.sock[mysqld]port = 3306server-id = 3306user = mysqlsocket = /tmp/mysql.sock# 安装目录basedir = /usr/local/mysql8# 数据存放目录datadir = /usr/local/mysql8/datas/mysqllog-bin = /usr/local/mysql8/datas/mysql/mysql-bininnodb_data_home_dir =/usr/local/mysql8/datas/mysqlinnodb_log_group_home_dir =/usr/local/mysql8/datas/mysql#日志及进程数据的存放目录log-error =/usr/local/mysql8/datas/mysql/mysql.logpid-file =/usr/local/mysql8/datas/mysql/mysql.pid# 服务端使用的字符集默认为8比特编码character-set-server=utf8mb4lower_case_table_names=1autocommit =1 ##################以上要修改的########################skip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 1024sort_buffer_size = 4Mnet_buffer_length = 8Kread_buffer_size = 4Mread_rnd_buffer_size = 512Kmyisam_sort_buffer_size = 64Mthread_cache_size = 128 #query_cache_size = 128Mtmp_table_size = 128Mexplicit_defaults_for_timestamp = truemax_connections = 500max_connect_errors = 100open_files_limit = 65535 binlog_format=mixed binlog_expire_logs_seconds =864000 # 创建新表时将使用的默认存储引擎default_storage_engine = InnoDBinnodb_data_file_path = ibdata1:10M:autoextendinnodb_buffer_pool_size = 1024Minnodb_log_file_size = 256Minnodb_log_buffer_size = 8Minnodb_flush_log_at_trx_commit = 1innodb_lock_wait_timeout = 50transaction-isolation=READ-COMMITTED [mysqldump]quickmax_allowed_packet = 16M [myisamchk]key_buffer_size = 256Msort_buffer_size = 4Mread_buffer = 2Mwrite_buffer = 2M [mysqlhotcopy]interactive-timeout
(2)配置环境变量
vim /etc/profile 或vi /etc/profile
在末尾添加
export PATH=$PATH:/usr/local/mysql8/bin
更新配置
source /etc/profile
(3)初始化
mysqld --defaults-file=/usr/local/mysql8/my.cnf --basedir=/usr/local/mysql8/ --datadir=/usr/local/mysql8/datas/mysql --user=mysql --initialize-insecure
参数:
defaults-file
:指定配置文件(要放在–initialize 前面)user
: 指定用户basedir
:指定安装目录datadir
:指定初始化数据目录intialize-insecure
:初始化无密码
7.启动 MySQL
(1)启动 mysql
mysqld_safe --defaults-file=/usr/local/mysql8/my.cnf &
(2)查看 MySQL 是否启动成功
ps -ef|grep mysql
出现下列信息则启动成功
8.登录 MySQL
(1)无密码方式登录
mysql -u root --skip-password
(2)修改密码
# 修改密码ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';# 刷新权限FLUSH PRIVILEGES;
(3)设置允许远程登录
mysql> use mysqlmysql> update user set user.Host='%'where user.User='root';mysql> flush privileges;mysql> quit
(4)在 navicat上测试连接
9.设置 mysql开机自启动
(1)在/etc/rc.d/init.d/编辑一个新文件autostartmysql.sh
cd /etc/rc.d/init.d/vim ./autostartmysql.sh
autostartmysql.sh内容:
#!/bin/sh# chkconfig: 2345 10 90# description: myservice.../usr/local/mysql8/bin/mysqld_safe --defaults-file=/usr/local/mysql8/my.cnf &
(2)为autostartmysql.sh赋予权限
chmod +x ./autostartmysql.sh
(3) 将autostartmysql.sh添加到 chkconfig 中
chkconfig --add ./autostartmysql.sh chkconfig autostartmysql.sh on
查看是否添加成功
chkconfig --list
我们创建的新文件已经添加成功
(4)检查是否开机自启动
reboot 或 shutdown -r now 立即重启
重启后
ps -ef|grep mysql
查看 mysql 是否自启动
来源地址:https://blog.csdn.net/jojo_oulaoula/article/details/132412202