1.環(huán)境準(zhǔn)備
1.mysql-5.7.29下載
#提前準(zhǔn)備mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz安裝包耿芹,或者采用wget下載:
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
2.解壓MySQL
#解壓
tar -zxvf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
#移動mysql
mv mysql-5.7.29-linux-glibc2.12-x86_64 mysql
3.創(chuàng)建實例數(shù)據(jù)目錄
#進入mysql目錄,創(chuàng)建data數(shù)據(jù)文件夾
cd mysql
mkdir data
4.在mysql目錄下創(chuàng)建my.cnf配置文件挪哄,內(nèi)容如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
# 配置端口號 默認(rèn)3306吧秕,這里配置為3305
character-set-server = utf8
port=3305
# cluster 用戶名目錄,需要改為自己的用戶名
basedir=/home/cluster/mysql
datadir=/home/cluster/mysql/data
socket=/home/cluster/mysql/mysql.sock
log_error=/home/cluster/mysql/error.log
pid-file=/home/cluster/mysql/mysql.pid
[client]
port=3305
default-character-set=utf8
socket=/home/cluster/mysql/mysql.sock
5.初始化Mysql數(shù)據(jù)庫
# cluster改為自己的用戶名
bin/mysqld --defaults-file=/home/cluster/mysql/my.cnf --initialize --user=cluster --basedir=/home/cluster/mysql --datadir=/home/cluster/mysql/data
#初始化完成后中燥,檢查/home/cluster/mysql/error.log日志是否初始化成功
6.通過mysqld_safe模式啟動數(shù)據(jù)庫寇甸,然后修改默認(rèn)密碼
bin/mysqld_safe --defaults-file=/home/cluster/mysql/my.cnf --user=cluster &
進入用戶的mysql目錄,獲取數(shù)據(jù)庫生成的默認(rèn)密碼:
cat error.log | grep root@localhost
2020-03-02T07:30:32.189865Z 1 [Note] A temporary password is generated for root@localhost: wcl4=)Tn2,HV
登陸mysql:
$ bin/mysql -u root -p
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
不存在/tmp/mysql.sock疗涉,我們在my.cnf配置的mysql.sock路徑是/home/cluster/mysql/mysql.sock拿霉,通過軟連接在/tmp/連接mysql.sock文件:
ln -s /home/cluster/mysql/mysql.sock /tmp/mysql.sock
再次登錄,輸入之前拿到的密碼wcl4=)Tn2,HV后登陸成功:
bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
修改密碼:
mysql> set password for 'root'@'localhost' = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
7.停止mysqld_safe模式改為正常啟動
#通過查找mysql進程
ps -ef|grep mysql
cluster 25169 23838 0 15:31 pts/0 00:00:00 /bin/sh bin/mysqld_safe --defaults-file=/home/cluster/mysql/my.cnf --user=cluster
cluster 30397 25169 2 16:02 pts/0 00:00:00 /home/cluster/mysql/bin/mysqld --defaults-file=/home/cluster/mysql/my.cnf --basedir=/home/cluster/mysql --datadir=/home/cluster/mysql/data --plugin-dir=/home/cluster/mysql/lib/plugin --log-error=/home/cluster/mysql/error.log --pid-file=/home/cluster/mysql/mysql.pid --socket=/home/cluster/mysql/mysql.sock --port=3305
#結(jié)束進程
kill -9 30379
8.正常啟動mysql服務(wù)
bin/mysqld --defaults-file=/home/cluster/mysql/my.cnf --user=cluster &