# cd /opt/
# tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
# groupadd mysql
# useradd -d /home/mysql -s /bin/bash -g mysql -m mysql
# chown -R mysql:mysql /opt/mysql
# mkdir /data01/mysql-data
# mkdir /data01/mysql-data/tmp
# mkdir /data01/mysql-data/log
# chown -R mysql:mysql /data01/mysql-data
# chgrp -R mysql /data01/mysql-data
# vi /opt/mysql/my.cnf
输入i进入编辑模式,按如下要求修改文件内容,修改完成后按Esc退出编辑模式,执行:wq!保存并退出。其中,“bind-address”参数请修改为MySQL服务器的地址。
[mysqld] basedir = /opt/mysql bind-address = 10.10.10.30 datadir = /data01/mysql-data/workdbs tmpdir = /data01/mysql-data/tmp/ port = 3306 socket =/opt/mysql/lib/mysql.sock lower_case_table_names=1 character-set-server = utf8 max_allowed_packet = 150M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,STRICT_ALL_TABLES log-error=/data01/mysql-data/log/mysql_3306.log max_connections=1000 event_scheduler=ON [mysql] default-character-set = utf8 socket =/opt/mysql/lib/mysql.sock
# chown mysql:mysql /opt/mysql/my.cnf
# cp -fr /opt/mysql/my.cnf /etc/my.cnf
若etc目录下已有my.cnf,会提示如下信息,请输入yes,替换原有文件。
cp: overwrite ‘/etc/my.cnf‘?
# vi /etc/profile
输入i进入编辑模式,在文件末尾添加如下内容:
export PATH=$PATH:/opt/mysql/bin export PATH=$PATH:/etc/init.d
添加完成后按Esc退出编辑模式,执行:wq!保存并退出。
# source /etc/profile
# cd /opt/mysql
# cp -a ./support-files/mysql.server /etc/init.d/mysql.server
如果执行cp -a ./support-files/mysql.server /etc/init.d/mysqld命令,即重命名mysql.server文件为mysqld,则将使用mysqld作为MySQL的服务名,而不是mysql.server。
# cd /opt/mysql
# ./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql/ --datadir=/data01/mysql-data/workdbs
命令执行后,如正确,则不会有显示信息。
# cat /data01/mysql-data/log/mysql_3306.log
2018-03-17T08:10:25.707388Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more deta ils). 2018-03-17T08:10:25.707430Z 0 [Warning] ‘NO_ZERO_DATE‘, ‘NO_ZERO_IN_DATE‘ and ‘ERROR_FOR_DIVISION_BY_ZERO‘ sql modes should be used with strict mode. They will be merged with strict mo de in a future release. 2018-03-17T08:10:25.707434Z 0 [Warning] ‘NO_AUTO_CREATE_USER‘ sql mode was not set. 2018-03-17T08:10:26.864589Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-03-17T08:10:27.068801Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-03-17T08:10:27.154126Z 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: a6a8df86-29b a-11e8-9a31-286ed48958fb. 2018-03-17T08:10:27.161747Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened. 2018-03-17T08:10:27.703898Z 0 [Warning] CA certificate ca.pem is self signed. 2018-03-17T08:10:28.083190Z 1 [Note] A temporary password is generated for root@localhost: 9hkqi,g9ejD.
获取临时密码,如:“9hkqi,g9ejD.”。
# ln -s /opt/mysql /usr/local/mysql
# ln -s /opt/mysql/lib/mysql.sock /tmp/mysql.sock
# systemctl enable mysql.server.service
# cd /opt/mysql/support-files
# mysql.server start
# mysql.server status
系统显示如下类似信息表示MySQL状态正常:
MySQL running (70442) done mysql.server.service - LSB: start and stop MySQL Loaded: loaded (/etc/init.d/mysql.server; bad; vendor preset: disabled) Active: active (running) since Fri 2018-03-16 17:56:01 CST; 6s ago Docs: man:systemd-sysv-generator(8) Process: 62130 ExecStop=/etc/init.d/mysql.server stop (code=exited, status=0/SUCCESS) Process: 70128 ExecStart=/etc/init.d/mysql.server start (code=exited, status=0/SUCCESS) Tasks: 28 (limit: 512) CGroup: /system.slice/mysql.server.service ├─70153 /bin/sh /opt/mysql/bin/mysqld_safe --datadir=/data01/mysql-data/workdbs --pid-file=/data01/mysql-data/workdbs/NKG1000132347.pid └─70442 /opt/mysql/bin/mysqld --basedir=/opt/mysql --datadir=/data01/mysql-data/workdbs --plugin-dir=/opt/mysql/lib/plugin --user=mysql --log-error=NKG1000132347.err --pid-file=/data01/mysql-data/workdbs/NKG1000132347.pid --socket=/opt/mysql/lib/mysql.sock --port=3306
# cd /opt/mysql/bin
# mysql -u root -p
按照提示信息输入记录的临时密码。
Enter Password:
登录成功后系统显示如下类似信息:
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.24-enterprise-commercial-advanced Copyright (c) 2000, 2018, 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=password(‘Password‘);
其中,单引号中的Password由用户自定义。
mysql> grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘Password‘ with grant option;
其中,单引号中的Password由用户自定义。
mysql> flush privileges;
mysql> use mysql;
mysql> select host,user from user;
系统显示如下类似信息,表示数据库已正常安装和运行。
+-----------+-----------+ | host | user | +-----------+-----------+ | % | root | | localhost | mysql.sys | | localhost | root | +-----------+-----------+ 3 rows in set (0.01 sec)
mysql> exit
# cp /opt/mysql/bin/mysql /usr/bin
原文:https://www.cnblogs.com/sxck/p/12579317.html