mysql官网介绍
主页https://www.oracle.com/mysql/index.html
下载主页面https://www.mysql.com/downloads/
社区资源下载页面https://dev.mysql.com/downloads/
MySQL社区版下载页面https://dev.mysql.com/downloads/mysql/
MySQL相关产品:
商业付费软件有:Oracle MySQL Cloud Service (commercial)、MySQL Enterprise Edition(commercial)、
MySQL Cluster CGE(commercial)
免费社区软件:MySQL Community Edition、MySQL Community Server(最流行)
MySQL安装-准备(1)
一、检查操作系统和MySQL版本的适配度。
同一个操作系统,不同版本对MySQL版本适配度不一样。
二、选择安装的MySQL版本
1、首先判断是否要和公司其他已经安装好的MySQL保持版本一致
2、如果没有上述要求,则一般会安装最新版本(目前是5.7)
3、如果不是实验新功能性质,则不要选择development release,而要安装
注:General Availability (GA) release(代表稳定版本,可在生产系统使用)
三、选择安装MySQL的方式
1、二进制安装包的方式(RPM,ZIP,Tar等)
2、源码方式(source code)
注:一般会选择二进制安装方式
如果有特殊需求,比如修改一部分源码或修改MySQL深层次的配置,则会选择源码
MySQL Linux安装
1、下载正确的tar包
2、上传tar包到服务器并解压tar包到/usr/local/目录
[root@localhost mysql_package]# tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz [root@localhost mysql_package]# ls mysql-8.0.13-linux-glibc2.12-x86_64 mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz [root@localhost mysql_package]# mv mysql-8.0.13-linux-glibc2.12-x86_64 mysql [root@localhost mysql_package]# cp -rf mysql /usr/local/mysql [root@localhost mysql_package]# rm -rf mysql [root@localhost local]# pwd /usr/local [root@localhost local]# ll mysql/ total 448 drwxr-xr-x. 2 root root 4096 Jun 26 12:59 bin drwxr-xr-x. 2 root root 86 Jun 26 12:59 docs drwxr-xr-x. 3 root root 266 Jun 26 12:59 include drwxr-xr-x. 6 root root 4096 Jun 26 12:59 lib -rw-r--r--. 1 root root 335809 Jun 26 12:59 LICENSE -rw-r--r--. 1 root root 101807 Jun 26 12:59 LICENSE.router drwxr-xr-x. 4 root root 30 Jun 26 12:59 man -rw-r--r--. 1 root root 687 Jun 26 12:59 README -rw-r--r--. 1 root root 700 Jun 26 12:59 README.router drwxr-xr-x. 28 root root 4096 Jun 26 12:59 share drwxr-xr-x. 2 root root 90 Jun 26 12:59 support-files [root@localhost local]#
解压后目录有2G
[root@localhost local]# du -h --max-depth=1 0 ./bin 0 ./etc 0 ./games 0 ./include 0 ./lib 0 ./lib64 0 ./libexec 0 ./sbin 4.0K ./share 0 ./src 2.0G ./mysql 2.0G .
mysql目录结构
[root@localhost mysql]# pwd /usr/local/mysql [root@localhost mysql]# ll total 448 drwxr-xr-x. 2 root root 4096 Jun 26 12:59 bin #存放可执行文件, mysql自带的客户端, mysql服务端等 drwxr-xr-x. 2 root root 6 Jun 26 19:46 data #存放数据文件和日志文件 drwxr-xr-x. 2 root root 86 Jun 26 12:59 docs #存放一些文档 drwxr-xr-x. 3 root root 266 Jun 26 12:59 include #存储包含的头文件, 如:mysql.h、mysql_ername.h等 drwxr-xr-x. 6 root root 4096 Jun 26 12:59 lib #用于放置一系列库文件 -rw-r--r--. 1 root root 335809 Jun 26 12:59 LICENSE -rw-r--r--. 1 root root 101807 Jun 26 12:59 LICENSE.router drwxr-xr-x. 4 root root 30 Jun 26 12:59 man -rw-r--r--. 1 root root 687 Jun 26 12:59 README -rw-r--r--. 1 root root 700 Jun 26 12:59 README.router drwxr-xr-x. 28 root root 4096 Jun 26 12:59 share #用于存放字符集、语言等信息 drwxr-xr-x. 2 root root 90 Jun 26 19:50 support-files #里面比较重要的有个mysql.server脚本,启动时会执行 [root@localhost mysql]#
3、创建运行MySQL的用户和组(名字可以任意)
Shell> groupadd mysql
Shell> useradd mysql -g mysql
4、切换到mysql目录
Shell > mkdir data
5、修改解压包的权限
Shell> chown –R mysql .
Shell> chgrp –R mysql .
6、安装MySQL
Shell> bin/mysqld --initialize --user=mysql --datadir /usr/local/mysql/data ##初始化数据目录
shell> cp -f support-files/my-default.cnf /etc/my.cnf ##将默认配置文件复制到指定目录
shell> bin/mysqld_safe --datadir=/usr/local/mysql/data --user=mysql & ##启动MySQL服务
shell> cp support-files/mysql.server /etc/init.d/mysql.server ##将MySQL加入到服务自启动
Shell> /etc/init.d/mysql.server start ##通过服务启动
[root@localhost local]# cd mysql/ [root@localhost mysql]# ls bin data docs include lib LICENSE LICENSE.router man README README.router share support-files [root@localhost mysql]# chown -R mysql . [root@localhost mysql]# chgrp -R mysql . [root@localhost mysql]# ll total 448 drwxr-xr-x. 2 mysql mysql 4096 Jun 26 12:59 bin drwxr-xr-x. 2 mysql mysql 6 Jun 26 19:46 data drwxr-xr-x. 2 mysql mysql 86 Jun 26 12:59 docs drwxr-xr-x. 3 mysql mysql 266 Jun 26 12:59 include drwxr-xr-x. 6 mysql mysql 4096 Jun 26 12:59 lib -rw-r--r--. 1 mysql mysql 335809 Jun 26 12:59 LICENSE -rw-r--r--. 1 mysql mysql 101807 Jun 26 12:59 LICENSE.router drwxr-xr-x. 4 mysql mysql 30 Jun 26 12:59 man -rw-r--r--. 1 mysql mysql 687 Jun 26 12:59 README -rw-r--r--. 1 mysql mysql 700 Jun 26 12:59 README.router drwxr-xr-x. 28 mysql mysql 4096 Jun 26 12:59 share drwxr-xr-x. 2 mysql mysql 90 Jun 26 19:50 support-files [root@localhost mysql]#
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --datadir /usr/local/mysql/data 2020-06-27T07:51:03.490770Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 2020-06-27T07:51:03.512364Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.13) initializing of server in progress as process 68531 2020-06-27T07:51:08.300288Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ,8kEClgiEhnH 2020-06-27T07:51:10.263352Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.13) initializing of server has completed
上面有临时密码,如果初始化的时候忘了保存,可以重新初始化,也就重新执行初始化命令,不过在这之前,需要删除data目录下数据
[root@localhost support-files]# cp mysql.server /etc/init init.d/ inittab [root@localhost support-files]# cp mysql.server /etc/init.d/mysql #把mysql启动脚本拷贝到init里方便启动 [root@localhost support-files]# cat /etc/my.cnf #看下etc下有没my.cnf启动配置文件 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [mysqld_safe] log-error=/var/log/mariadb/mariadb.log pid-file=/var/run/mariadb/mariadb.pid # # include all files from the config directory # !includedir /etc/my.cnf.d [root@localhost support-files]# rm /etc/my.cnf #暂时先删除,删掉的作用是说明我们以mysql的所有默认参数启动 rm: remove regular file ‘/etc/my.cnf’? y [root@localhost support-files]#
[root@localhost support-files]# /etc/init.d/mysql start Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'. .. SUCCESS! #出现success说明启动成功。 [root@localhost support-files]# ps -ef|grep mysql #可以看到有两个进程,一个父进程 一个子进程 root 69385 1 0 01:13 pts/2 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/localhost.localdomain.pid mysql 69470 69385 4 01:13 pts/2 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/usr/local/mysql/data/localhost.localdomain.pid root 69526 11777 0 01:13 pts/2 00:00:00 grep --color=auto mysql [root@localhost support-files]#
[root@localhost mysql]# mysql -uroot -p #启动成功后,发现进入交互界面会提示mysql命令不存在,这是因为bin目录还没加入到环境变量 bash: mysql: command not found... [root@localhost mysql]# cd bin/ [root@localhost bin]# ls ibd2sdi myisamchk mysql mysql_config_editor mysqldump mysqlrouter_plugin_info mysql_tzinfo_to_sql zlib_decompress innochecksum myisam_ftdump mysqladmin mysqld mysqldumpslow mysql_secure_installation mysql_upgrade libcrypto.so.1.0.0 myisamlog mysqlbinlog mysqld-debug mysqlimport mysqlshow perror libssl.so.1.0.0 myisampack mysqlcheck mysqld_multi mysqlpump mysqlslap resolveip lz4_decompress my_print_defaults mysql_config mysqld_safe mysqlrouter mysql_ssl_rsa_setup resolve_stack_dump [root@localhost bin]# pwd /usr/local/mysql/bin [root@localhost bin]# vim ~/.bash_profile [root@localhost bin]# source ~/.bash_profile [root@localhost bin]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 8 Server version: 8.0.13 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> show databases;#执行命令会报错,提示重置初始密码 ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> set password=password('mysql');#在mysql5.7之前可以用这个命令修改,不过在8.0后不行了 ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password('mysql')' at line 1 mysql> alter user user() identified by 'mysql';#得用这个命令修改 Query OK, 0 rows affected (0.06 sec) mysql> exit Bye [root@localhost bin]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 9 Server version: 8.0.13 MySQL Community Server - GPL 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)