1、新建用户和组
groupadd -g 500 mysql useradd -g 500 -u 500 -d /dev/null -s /sbin/nologin -M mysql
2、下载
cd /usr/local/src wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ cd /usr/local/ mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql-5.7.18 ln -s /usr/local/mysql-5.7.18 /usr/local/mysql cd mysql chown -R mysql:mysql *
3、新建数据目录和日志目录
日志目录: mkdir -p /home/logs/mysql chown -R mysql.mysql /home/logs/mysql 数据目录: mkdir -p /home/mysql/mysql3306 chown -R mysql.mysql /home/mysql/mysql3306
4、初始化数据库
cd /usr/local/mysql/bin/ ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/mysql3306 --explicit_defaults_for_timestamp
输出内容如下:
重点是最后一行的root密码
2020-04-13T13:51:35.964708Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-04-13T13:51:36.068639Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-04-13T13:51:36.124853Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e43c70cd-7d8d-11ea-b8ba-00163e328f78. 2020-04-13T13:51:36.125628Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-04-13T13:51:36.126152Z 1 [Note] A temporary password is generated for root@localhost: b5J6)&&f;dJN
5、修改my.cnf配置文件
# more /etc/my.cnf [mysqld] datadir=/home/mysql/mysql3306 socket=/tmp/mysql3306.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/home/logs/mysql/mysqld3306.log pid-file=/home/mysql/mysql3306/mysqld3306.pid
6、启动mysql并登陆数据库修改root密码
启动mysql /usr/local/mysql/bin/mysqld_safe & 登陆mysql /usr/local/mysql/bin/mysql -uroot -p -S /tmp/mysql3306.sock 输入4中的输出密码“b5J6)&&f;dJN” SET PASSWORD = PASSWORD('your_new_password'); flush privileges; exit
7、配置mysql开机自启动,并使用新密码登陆
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig mysqld on mysql -uroot -p -S /tmp/mysql3306.sock 输入新的密码登陆成功。
8、验证mysqld服务是否启停正常
service mysqld status service mysqld start service mysqld stop service mysqld restart