16
Linux下安装MySQL
发表时间:2022-03-18
分类:数据库
阅读数:918
评论数:0
Linux下安装MySQL
安装前准备
官网下载地址:https://downloads.mysql.com/archives/community/
也可以直接访问该链接下载:https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
这里选择的5.7.26,
Operating System选择的:LInux -Generic
选择第一个Compressed TAR Archive下载即可
上安装包传至服务器并解压
下载后使用ftp软件,将该安装包上传至服务器
# 进入上传至安装包的目录中
[root@VM-12-16-centos /]# cd /usr/local/
[root@VM-12-16-centos local]# ls
bin games lib libexec etc include lib64
mysql-5.7.26-linux-glibc2.12-x86_64.tar_2.gz sbin src
#对安装包进行解压操作
[root@VM-12-16-centos local]# tar -xvf mysql-5.7.26-linux-glibc2.12-x86_64.tar_2.gz
mysql-5.7.26-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-5.7.26-linux-glibc2.12-x86_64/bin/myisamchk
mysql-5.7.26-linux-glibc2.12-x86_64/bin/myisamlog
mysql-5.7.26-linux-glibc2.12-x86_64/bin/myisampack
mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysql
mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysql_client_test_embedded
mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysql_config_editor
mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysql_embedded
mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysql_install_db
mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysql_plugin
mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysql_secure_installation
mysql-5.7.26-linux-glibc2.12-x86_64/bin/mysql_ssl_rsa_setup
# 没有错误提示的话即安装成功,输入ll或ls查看是否解压成功
[root@VM-12-16-centos local]# ll
total 629808
drwxr-xr-x. 2 root root 4096 Nov 3 2020 bin
drwxr-xr-x. 2 root root 4096 Nov 3 2020 etc
drwxr-xr-x. 2 root root 4096 Nov 3 2020 games
drwxr-xr-x. 2 root root 4096 Nov 3 2020 include
drwxr-xr-x. 3 root root 4096 Nov 3 2020 lib
drwxr-xr-x. 4 root root 4096 Jun 18 2021 lib64
drwxr-xr-x. 2 root root 4096 Nov 3 2020 libexec
drwxr-xr-x 9 root root 4096 Mar 18 11:11 mysql-5.7.26-linux-glibc2.12-x86_64
-rw-r--r-- 1 root root 644869837 Mar 18 11:07 mysql-5.7.26-linux-glibc2.12-x86_64.tar_2.gz
# 这里发现名字太长,这里使用mv指令更改一下名字
[root@VM-12-16-centos local]# mv mysql-5.7.26-linux-glibc2.12-x86_64 mysql
[root@VM-12-16-centos local]# ll
total 629808
drwxr-xr-x. 2 root root 4096 Nov 3 2020 bin
drwxr-xr-x. 2 root root 4096 Nov 3 2020 etc
drwxr-xr-x. 2 root root 4096 Nov 3 2020 games
drwxr-xr-x. 2 root root 4096 Nov 3 2020 include
drwxr-xr-x. 3 root root 4096 Nov 3 2020 lib
drwxr-xr-x. 4 root root 4096 Jun 18 2021 lib64
drwxr-xr-x. 2 root root 4096 Nov 3 2020 libexec
drwxr-xr-x 9 root root 4096 Mar 18 11:11 mysql
-rw-r--r-- 1 root root 644869837 Mar 18 11:07 mysql-5.7.26-linux-glibc2.12-x86_64.tar_2.gz
# 查看mysql文件是否完整
[root@VM-12-16-centos local]# ll mysql
total 52
drwxr-xr-x 2 root root 4096 Mar 18 11:11 bin
-rw-r--r-- 1 7161 31415 17987 Apr 13 2019 COPYING
drwxr-xr-x 2 root root 4096 Mar 18 11:11 docs
drwxr-xr-x 3 root root 4096 Mar 18 11:11 include
drwxr-xr-x 5 root root 4096 Mar 18 11:11 lib
drwxr-xr-x 4 root root 4096 Mar 18 11:11 man
-rw-r--r-- 1 7161 31415 2478 Apr 13 2019 README
drwxr-xr-x 28 root root 4096 Mar 18 11:11 share
drwxr-xr-x 2 root root 4096 Mar 18 11:11 support-files
进行mysql的权限配置
- 创建mysql用户组和用户并修改权限
[root@VM-12-16-centos local]# groupadd mysql
[root@VM-12-16-centos local]# useradd -r -g mysql mysql
-
创建data目录并赋予权限
[root@VM-12-16-centos local]# cd mysql/ [root@VM-12-16-centos mysql]# ls bin COPYING docs include lib man README share support-files [root@VM-12-16-centos mysql]# mkdir -p /data/mysql # 创建data目录 [root@VM-12-16-centos mysql]# chown mysql:mysql -R /data/mysql/ # 给予权限
查看是否拥有权限
[root@VM-12-16-centos mysql]# cd /data/mysql/ [root@VM-12-16-centos mysql]# ll total 0 [root@VM-12-16-centos mysql]# cd .. [root@VM-12-16-centos data]# ll total 4 drwxr-xr-x 2 mysql mysql 4096 Mar 18 11:21 mysql
进行mysql的相关配置
-
配置my.cnf
[root@VM-12-16-centos data]# vim /etc/my.cnf # my.cnf文件内容 [mysqld] bind-address=0.0.0.0 port=3306 user=mysql basedir=/usr/local/mysql datadir=/data/mysql socket=/tmp/mysql.sock log-error=/data/mysql/mysql.err pid-file=/data/mysql/mysql.pid #character config character_set_server=utf8mb4 symbolic-links=0 explicit_defaults_for_timestamp=true # 按esc 输入wq退出保存
初始化数据库
# 进入mysql的bin目录
[root@VM-12-16-centos data]# cd /usr/local/mysql/bin/
[root@VM-12-16-centos bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/ --user=mysql --initialize
# 没有报错即完成
查看密码
[root@VM-12-16-centos bin]# cat /data/mysql/mysql.err
2022-03-18T03:34:26.488211Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-03-18T03:34:26.549695Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-03-18T03:34:26.621619Z 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: 4fbea6c1-a66c-11ec-87b3-525400274234.
2022-03-18T03:34:26.625675Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-03-18T03:34:26.626057Z 1 [Note] A temporary password is generated for root@localhost: fwefaw>feaa
# 最后的 fwefaw>feaa 就是密码
启动mysql
[root@VM-12-16-centos bin]# service mysql start
[root@VM-12-16-centos bin]# ps -ef|grep mysql
登录
# 如果是centos7会正常启动
# 我使用的centos8,这里会报错没有libncurses.so.5
[root@VM-12-16-centos bin]# ./mysql -u root -p
./mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
# 输入指令,下载libncurses.so.5
[root@VM-12-16-centos bin]# yum install libncurses*
# 碰到提示输入y即可,安装完成后再进行登录即可登录
[root@VM-12-16-centos bin]# ./mysql -uroot -p
Enter password:
# 我这里输入密码时出现该错误
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
# 进入/usr/local/mysql目录输入指令
[root@VM-12-16-centos bin]# cd ..
[root@VM-12-16-centos mysql]# ls
bin COPYING docs include lib man README share support-files
[root@VM-12-16-centos mysql]# sudo ./support-files/mysql.server start
Starting MySQL. SUCCESS!
# 再次登录即可进入
[root@VM-12-16-centos bin]# ./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26
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>
修改密码并允许远程链接
# 修改密码
mysql> SET PASSWORD = PASSWORD('123456');
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
mysql> FLUSH PRIVILEGES;
# 设置远程链接
mysql> use mysql #访问mysql库
mysql> update user set host = '%' where user = 'root'; #使root能再任何host访问
mysql> FLUSH PRIVILEGES;
配置mysql服务
每次打开mysql都要进到/usr/local/mysql/bin中启动,设置一个mysql服务,使我们不需要每次都要进入mysql目录中启动。
# 直接输入mysql会报错
[root@VM-12-16-centos tmp]# mysql -uroot -p
-bash: mysql: command not found
# 设置服务
[root@VM-12-16-centos tmp]# ln -s /usr/local/mysql/bin/mysql /usr/bin
[root@VM-12-16-centos tmp]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)
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>
配置开机自启
如果服务器重启后,我们还要每次打开Mysql,有时候可能还会忘记,所以配置开机自启后就解决了该问题
[root@VM-12-16-centos tmp]# vi /etc/rc.d/rc.local
# 文件内容如下,添加一行/usr/local/mysql/support-files/mysql.server start即可
# 输入i进行编辑
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/usr/local/mysql/support-files/mysql.server start
# 按ESC,输入wq保存退出
结语
至此,在Linux下安装MySQL完成~
--------------------END--------------------
评论