Linux安装mysql
前言:这个博客还是我去年大四实习的时候开通的,本来打算记录一些学习和工作上遇到的问题,但是人太懒了,开通之后,就一直空着。不知不觉已经毕业快一年了,也参加工作了,现在打算抽空记录一些和java相关的知识点,希望能坚持下去,不要半途而废。
链接:https://pan.baidu.com/s/1oBUNKcaunmDtTObzwOVvfw
提取码:1wxn
官网下载:https://dev.mysql.com/downloads/mysql/
1:上传mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz安装包到/usr/local目录下
2:cd/usr/local 进入local目录
ls查看local目录下的文件
[root@myLinux local]# ls
aegis bin etc games include lib lib64 libexec mysoft mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz sbin share src
3:tar -xvf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz 解压我们上传的mysql安装包
[root@myLinux local]# tar -xvf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz mysql-8.0.18-linux-glibc2.12-x86_64/bin/ mysql-8.0.18-linux-glibc2.12-x86_64/bin/myisam_ftdump mysql-8.0.18-linux-glibc2.12-x86_64/bin/myisamchk mysql-8.0.18-linux-glibc2.12-x86_64/bin/myisamlog
4:ls 再次查看local目录下的文件
aegis etc include lib64 mysoft mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz share
bin games lib libexec mysql-8.0.18-linux-glibc2.12-x86_64 sbin src
5:mv mysql-8.0.18-linux-glibc2.12-x86_64 mysql 重命名解压后的安装包为mysql
[root@myLinux local]# mv mysql-8.0.18-linux-glibc2.12-x86_64 mysql
[root@myLinux local]# ls
aegis bin etc games include lib lib64 libexec mysoft mysql mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz sbin share src
6:groupadd mysql 创建用户组
[root@myLinux local]# groupadd mysql
7:useradd -r -g mysql mysql 添加用户
[root@myLinux local]# useradd -r -g mysql mysql
8:cd mysql 进入mysql目录
[root@myLinux local]# cd mysql
[root@myLinux mysql]# ls
bin docs include lib LICENSE LICENSE.router man README README.router run share support-files var
9:mkdir data 在mysql目录下创建data文件夹
[root@myLinux mysql]# mkdir data
[root@myLinux mysql]# ls
bin data docs include lib LICENSE LICENSE.router man README README.router run share support-files var
10:chown mysql:mysql -R /usr/local/mysql/data 给data文件夹赋予权限
[root@myLinux mysql]# chown mysql:mysql -R /usr/local/mysql/data
11:vim /etc/my.cnf 编辑/etc下的mysql配置文件
[root@myLinux mysql]# vim /etc/my.cnf
12:编辑my.cnf的内容如下
[mysqld] bind-address=0.0.0.0 port=3306 user=mysql basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/tmp/mysql.sock log-error=/usr/local/mysql/data/mysql.err pid-file=/usr/local/mysql/data/mysql.pid #character config character_set_server=utf8mb4 symbolic-links=0 explicit_defaults_for_timestamp=true
13:cd bin 进入mysql的bin目录下
[root@myLinux mysql]# cd bin/ [root@myLinux bin]# ls ibd2sdi myisam_ftdump mysqlbinlog mysqld_multi mysqlrouter mysqlslap innochecksum myisamlog mysqlcheck mysqld_safe mysqlrouter_keyring mysql_ssl_rsa_setup libcrypto.so.1.1 myisampack mysql_config mysqldump mysqlrouter_passwd mysql_tzinfo_to_sql libssl.so.1.1 my_print_defaults mysql_config_editor mysqldumpslow mysqlrouter_plugin_info mysql_upgrade lz4_decompress mysql mysqld mysqlimport mysql_secure_installation perror myisamchk mysqladmin mysqld-debug mysqlpump mysqlshow zlib_decompress
14:./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql --initialize 初始化数据库
[root@myLinux bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql --initialize
15:cd .. 返回mysql目录,cat data/mysql.err 查看日志文件,获取随机生成的mysql登录密码(最后一行root@localhost:后面的字符串即为自动生成的密码,我的密码是:>ahfjdeK1RSs)
[root@myLinux bin]# cd .. [root@myLinux mysql]# cat data/mysql.err 2020-04-27T05:51:03.492531Z 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-04-27T05:51:03.492640Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.18) initializing of server in progress as process 7941 2020-04-27T05:51:09.621611Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: >ahfjdeK1RSs
16:cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql 将mysql.server放置到/etc/init.d/mysql中
[root@myLinux mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
17:service mysql start 启动mysql服务(出现Starting MySQL... SUCCESS!代表启动成功)
[root@myLinux mysql]# service mysql start
Starting MySQL... SUCCESS!
18:ps -ef|grep mysql 查看mysql状态(出现下面情形说明我们的mysql已经安装并启动成功)
[root@myLinux mysql]# ps -ef|grep mysql root 8015 1 0 13:54 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/mysql.pid mysql 8242 8015 0 13:54 pts/0 00:00:14 /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=/usr/local/mysql/data/mysql.err --pid-file=/usr/local/mysql/data/mysql.pid --socket=/tmp/mysql.sock --port=3306 root 8629 25601 0 15:21 pts/0 00:00:00 grep --color=auto mysql
19:cd bin 进入mysql的bin目录
[root@myLinux mysql]# cd bin/ [root@myLinux bin]# ls ibd2sdi myisamchk mysql mysql_config_editor mysqldump mysqlrouter_keyring mysqlslap zlib_decompress innochecksum myisam_ftdump mysqladmin mysqld mysqldumpslow mysqlrouter_passwd mysql_ssl_rsa_setup libcrypto.so.1.1 myisamlog mysqlbinlog mysqld-debug mysqlimport mysqlrouter_plugin_info mysql_tzinfo_to_sql libssl.so.1.1 myisampack mysqlcheck mysqld_multi mysqlpump mysql_secure_installation mysql_upgrade lz4_decompress my_print_defaults mysql_config mysqld_safe mysqlrouter mysqlshow perror
20:./mysql -u root -p 登录mysql,密码为上面随机生成的密码(出现下面情形说明我们登录成功)
[root@myLinux bin]# ./mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.18 Copyright (c) 2000, 2019, 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>
21:show databases; 查看数据库(发现报错,因为它要求我们先更改密码才能执行sql)
mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
22:ALTER USER 'root'@'localhost' IDENTIFIED BY '244622'; 更改数据库密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '244622';
Query OK, 0 rows affected (0.11 sec)
mysql>
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
23:show databases; 此时可以执行sql命令,但是要Navicat连接mysql时报错
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
24:为了解决Navicat连接mysql失败问题,进入mysql数据库执行以下sql(3,4两条sql语句执行的顺序不唯一,根据自己的实际情况来,多试一下)
1:mysql> use mysql
2:mysql> update user set host = '%' where user = 'root';
3:mysql> ALTER USER 'root'@'%' IDENTIFIED BY '244622' PASSWORD EXPIRE NEVER; 4:mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '244622';
5:mysql> FLUSH PRIVILEGES;
25:测试连接
26:结语,安装软件时,大家的环境各不相同,出现的问题也不尽相同,大家尽量探索出适合自己的安装方法,把它贴出来,帮助他人,充实自己。