1.下载MySQL5.7.24
2.安装mysql所需的依赖
[root@CentOS6 ~]# yum install -y gcc-c++ readline-devel zlib-devel bison cmake
3.解压mysql源码包,更改目录名称
3.1创建/usr/mysql目录:
[root@CentOS6 ~]# mkdir /usr/mysql
3.2 将下载后的MySQL源码文件通过Xftp上传到/usr/mysql目录下
[root@CentOS6 mysql]# tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
4.创建用户组
[root@CentOS6 ~]# groupadd mysql [root@CentOS6 ~]# useradd -g mysql -s /bin/nologin -M mysql [root@CentOS6 ~]# id mysql //查看用户是否创建成功 uid=500(mysql) gid=500(mysql) groups=500(mysql)
5.创建数据库目录和日志目录
[root@CnetOS6 ~]# mkdir /mysql /mysql/data /mysql/log
6.创建MySQL配置文件my.cnf
[root@CnetOS6 ~]# vi /etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock [mysqld] server_id=10 port = 3306 user = mysql socket = /tmp/mysql.sock basedir = /usr/mysql/XXX #解压后的mysql目录(不要照抄) datadir = /mysql/data pid-file = /mysql/data/mysql.pid max_connections = 1000 max_connect_errors = 1000 table_open_cache = 1024 max_allowed_packet = 128M open_files_limit = 65535 # [innodb] innodb_buffer_pool_size = 1024M innodb_file_per_table = 1 innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_purge_threads = 2 innodb_flush_log_at_trx_commit = 1 innodb_log_file_size = 512M innodb_log_files_in_group = 2 innodb_log_buffer_size = 16M innodb_max_dirty_pages_pct = 80 innodb_lock_wait_timeout = 30 innodb_data_file_path=ibdata1:1024M:autoextend ##log log_error = /mysql/log/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /mysql/log/mysql-slow.log sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
7.更改目录权限
[root@CnetOS6 ~]# chown -R mysql:mysql /usr/local/mysql /mysql
8.初始化数据库
一定要进入mysql解压后的文件
[root@CnetOS6 mysql-5.7.24]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mysql/data
执行linux 中执行mysql命令 出现-bash: mysql: command not found
原因:因为系统默认会查找/usr/bin下的命令,由于mysql没有在这个目录下,所以出现not found。因此需要做一个软连接到/usr/bin目录下 。 执行命令: ln -s /usr/mysql/mysql-5.7.24/bin/mysql /usr/bin 其中/usr/mysql/mysql-5.7.24 为:mysql的安装路径
9.查看日志文件,读取数据库初始密码
[root@CnetOS6 mysql-5.7.24]# cat /mysql-5.7.24/log/mysql-error.log 2018-08-15T06:31:26.790588Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 100 200 300 400 500 600 700 800 900 1000 100 200 300 400 500 100 200 300 400 500 2018-08-15T06:31:35.868595Z 0 [Warning] InnoDB: New log files created, LSN=45791 2018-08-15T06:31:36.000802Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-08-15T06:31:36.379668Z 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: dc04c1e2-a054-11e8-b0c8-080027a8bc50. 2018-08-15T06:31:36.402324Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-08-15T06:31:36.418716Z 1 [Note] A temporary password is generated for root@localhost: rbQpyfaUh2!N
10.MySQL数据库加密
[root@CentOS6 mysql-5.7.24]# bin/mysql_ssl_rsa_setup --datadir=/mysql/data
11.配置环境变量,并使其生效
[root@CentOS6 mysql-5.7.24]# echo 'export PATH=/usr/mysql/mysql-5.7.24/bin:$PATH' >> /etc/profile [root@CentOS6 mysql-5.7.24]# source /etc/profile
12.配置MySQL服务
[root@CentOS6 mysql-5.7.24]# cp support-files/mysql.server /etc/init.d/mysql
13.启动数据库服务、进入到数据库中
[root@CentOS6 mysql-5.7.24]# /etc/init.d/mysql start Starting MySQL.. SUCCESS! [root@CentOS6 mysql-5.7.24]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.24-log Copyright (c) 2000, 2016, 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>
14.重置数据库密码
mysql> set password=password('root'); Query OK, 0 rows affected, 1 warning (0.00 sec)
15.配置远程连接
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
16.修改数据库字符编码
[root@CentOS6 mysql-5.7.24]# vi /etc/my.cnf [mysqld] character-set-server=utf8 [root@CentOS6 mysql-5.7.24]# /etc/init.d/mysql restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@CentOS6 mysql-5.7.24]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> \s -------------- mysql Ver 14.14 Distrib 5.7.24, for linux-glibc2.5 (x86_64) using EditLine wrapper Connection id: 3 Current database: Current user: root@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.24-log MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /tmp/mysql.sock Uptime: 18 sec Threads: 1 Questions: 6 Slow queries: 0 Opens: 110 Flush tables: 1 Open tables: 64 Queries per second avg: 0.333 --------------