Linux-安装MySql

Linux安装MySql

– 下载MySQL7的版本 – https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
– 下载MySQL8的版本 – https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz
先下载Linux版本MySql的安装包

然后把安装包放进你需要解压的目录
在这里插入图片描述
我是放在了/usr/local/MySql下的
然后输入ll,发现是红色的,这是权限问题。需要修改
在这里插入图片描述
输入chomd 755(777) + 文件名(MySql的包名)
在这里插入图片描述
再ll发现绿色了就说明成功了,
然后输入命令解压。 tar -xzpvf + 文件名
在这里插入图片描述

发现我出错了,修改一下解压命令tar -xzpvf + 文件名 后就成功了
在这里插入图片描述
添加mysql组和mysql 用户:
添加mysql组:
在这里插入图片描述
添加mysql用户:
useradd -r -g mysql mysql
在这里插入图片描述
配置MySQL
进入MySql目录
查看MySQL对于配置文件的查找路径,越左优先查找
help | grep ‘Default options’ -A 1
在这里插入图片描述
创建配置文件
[root@localhost mysql] rm -rf /etc/my.cnf
[root@localhost mysql] touch /etc/my.cnf
[root@localhost mysql] vi /etc/my.cnf
用i进行配置
在这里插入图片描述

[mysqld] port=3306
datadir=/var/lib/mysql/data socket=/var/lib/mysql/data/mysql.sock
user=root max_connections=151
symbolic-links=0
#设置忽略大小写
lower_case_table_names = 1
#指定编码
character-set-server=utf8
collation-server=utf8_general_ci
#指定客户端连接mysql时的socket通信文件路径
[client]
socket=/var/lib/mysql/data/mysql.sock
在这里插入图片描述设置开机启动
复制启动脚本到资源目录增加 (注意目录问题和系统文件没有没有这个mysql这个文件夹)
在这里插入图片描述
mysqld 服务控制脚本执行权限
在这里插入图片描述
将 mysqld 服务加入到系统服务chkconfig add mysql
在这里插入图片描述

检查mysqld服务是否已经生效chkconfig list mysql
在这里插入图片描述
创建文件夹
在这里插入图片描述
配置环境变量
vi /etc/profile
添加如下,但是路径根据自己的来
export PATH = $PATH:/usr/local/mysql/bin

刷新
source /etc/profile

检测
echo $PATH

初始化:
8以前的
mysqld --user=root --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data --initialize
8:mysqld --initialize --console
在这里插入图片描述
打箭头的是初始化密码
启动mysql
service mysql start
– 修改登入密码 [root@localhost mysql] mysql -u root -p
mysql> alter user ‘root’@‘localhost’ identified by ‘root’; mysql> flush privileges; mysql> quit;
– 修改/usr/local/mysql/etc/my.cnf 文件 – 注释skip-grant-tables – 重启MySQL [root@localhost mysql] service mysql restart
– 测试 [root@localhost mysql] mysql -u root -p
– 配置远程访问 – 防火墙端口设置,便于远程访问 [root@localhost mysql] firewall-cmd --zone=public --add-port=3306/tcp --permanent [root@localhost mysql] firewall-cmd --reload – 查看端口
启动防火墙服务:systemctl ummask firewalld在这里插入代码片
启动防火墙:systemctl start firewalld
– 进入MySQL mysql> grant all privileges on . to root@’%’ identified by “password”; mysql> flush privileges;
– 如果没有效果可以尝试重启一下MySQL

在这里插入图片描述

[root@localhost bin]# ./mysqld --user=root --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data --initialize
2020-04-01T13:13:53.262325Z 0 [Warning] Ignoring user change to 'root' because the user was set to 'root max_connections=151' earlier on the command line

2020-04-01T13:13:53.262525Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-01T13:13:53.266149Z 0 [ERROR] Fatal error: Can't change to run as user 'root max_connections=151' ;  Please check that the user exists!

2020-04-01T13:13:53.266181Z 0 [ERROR] Aborting

[root@localhost bin]# mysqld --user=root --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data --initialize
-bash: mysqld: 未找到命令
[root@localhost bin]# ./mysqld --user=root --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data --initialize
2020-04-01T13:15:13.124243Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-01T13:15:13.432441Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-04-01T13:15:13.481649Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-04-01T13:15:13.538073Z 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: d25b2089-741a-11ea-9333-000c29ffbb58.
2020-04-01T13:15:13.539991Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-04-01T13:15:14.257272Z 0 [Warning] CA certificate ca.pem is self signed.
2020-04-01T13:15:14.827610Z 1 [Note] A temporary password is generated for root@localhost: wwFjKrrpy8#D
[root@localhost bin]# cd ..
[root@localhost mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost mysql]# cd support-files
[root@localhost support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@localhost support-files]# vi mysql.server
[root@localhost support-files]# ./mysql.server start
Starting MySQL.Logging to '/var/lib/mysql/data/localhost.localdomain.err'.
. ERROR! The server quit without updating PID file (/var/lib/mysql/data/localhost.localdomain.pid).
[root@localhost support-files]# cd ..
[root@localhost mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
-bash: ./scripts/mysql_install_db: 没有那个文件或目录
[root@localhost mysql]# cd bin
[root@localhost bin]# ls
innochecksum       mysqlcheck                  mysql_embedded             mysql_tzinfo_to_sql
lz4_decompress     mysql_client_test_embedded  mysqlimport                mysql_upgrade
myisamchk          mysql_config                mysql_install_db           mysqlxtest
myisam_ftdump      mysql_config_editor         mysql_plugin               perror
myisamlog          mysqld                      mysqlpump                  replace
myisampack         mysqld-debug                mysql_secure_installation  resolveip
my_print_defaults  mysqld_multi                mysqlshow                  resolve_stack_dump
mysql              mysqld_safe                 mysqlslap                  zlib_decompress
mysqladmin         mysqldump                   mysql_ssl_rsa_setup
mysqlbinlog        mysqldumpslow               mysqltest_embedded
[root@localhost bin]# cd
[root@localhost ~]# vi mysql.server
[root@localhost ~]# vi /etc/profile
[root@localhost ~]# vi /etc/my.cnf
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost mysql]# cd support-files
[root@localhost support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@localhost support-files]# ./mysql.server start
Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/data/localhost.localdomain.pid).
[root@localhost support-files]# ^C
[root@localhost support-files]# ./mysql.server start
Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/data/localhost.localdomain.pid).
[root@localhost support-files]# ^C
[root@localhost support-files]# cd./
-bash: cd./: 没有那个文件或目录
[root@localhost support-files]# cd ..
[root@localhost mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost mysql]# cd bin
[root@localhost bin]# ls
innochecksum       mysqlcheck                  mysql_embedded             mysql_tzinfo_to_sql
lz4_decompress     mysql_client_test_embedded  mysqlimport                mysql_upgrade
myisamchk          mysql_config                mysql_install_db           mysqlxtest
myisam_ftdump      mysql_config_editor         mysql_plugin               perror
myisamlog          mysqld                      mysqlpump                  replace
myisampack         mysqld-debug                mysql_secure_installation  resolveip
my_print_defaults  mysqld_multi                mysqlshow                  resolve_stack_dump
mysql              mysqld_safe                 mysqlslap                  zlib_decompress
mysqladmin         mysqldump                   mysql_ssl_rsa_setup
mysqlbinlog        mysqldumpslow               mysqltest_embedded
[root@localhost bin]# ./mysqld --user=root --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data --initialize
2020-04-01T14:12:29.520684Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-01T14:12:29.722222Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-04-01T14:12:29.752166Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-04-01T14:12:29.807768Z 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: d2884587-7422-11ea-bddf-000c29ffbb58.
2020-04-01T14:12:29.808683Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-04-01T14:12:30.148068Z 0 [Warning] CA certificate ca.pem is self signed.
2020-04-01T14:12:30.254163Z 1 [Note] A temporary password is generated for root@localhost: MRI.Z<9*k;aw
[root@localhost bin]# cd ./
[root@localhost bin]# cd ..
[root@localhost mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost mysql]# cd support-files
[root@localhost support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@localhost support-files]# ./mysql.server start
Starting MySQL.Logging to '/var/lib/mysql/data/localhost.localdomain.err'.
.. ERROR! The server quit without updating PID file (/var/lib/mysql/data/localhost.localdomain.pid).
[root@localhost support-files]# cd ./
[root@localhost support-files]# cd ..
[root@localhost mysql]# cd bin
[root@localhost bin]# ls
innochecksum       mysqlcheck                  mysql_embedded             mysql_tzinfo_to_sql
lz4_decompress     mysql_client_test_embedded  mysqlimport                mysql_upgrade
myisamchk          mysql_config                mysql_install_db           mysqlxtest
myisam_ftdump      mysql_config_editor         mysql_plugin               perror
myisamlog          mysqld                      mysqlpump                  replace
myisampack         mysqld-debug                mysql_secure_installation  resolveip
my_print_defaults  mysqld_multi                mysqlshow                  resolve_stack_dump
mysql              mysqld_safe                 mysqlslap                  zlib_decompress
mysqladmin         mysqldump                   mysql_ssl_rsa_setup
mysqlbinlog        mysqldumpslow               mysqltest_embedded
[root@localhost bin]# ./mysqld --user=root --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data --initialize
2020-04-01T14:15:11.457279Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-01T14:15:11.621358Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-04-01T14:15:11.652604Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-04-01T14:15:11.707721Z 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: 33083e63-7423-11ea-9460-000c29ffbb58.
2020-04-01T14:15:11.708359Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-04-01T14:15:12.200832Z 0 [Warning] CA certificate ca.pem is self signed.
2020-04-01T14:15:12.425396Z 1 [Note] A temporary password is generated for root@localhost: 9t3basjyP4:2
[root@localhost bin]# cd ..
[root@localhost mysql]# cd support-files
[root@localhost support-files]# ./mysql.server start
Starting MySQL.Logging to '/var/lib/mysql/data/localhost.localdomain.err'.
.. ERROR! The server quit without updating PID file (/var/lib/mysql/data/localhost.localdomain.pid).
[root@localhost support-files]# ./mysql.server start
Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/data/localhost.localdomain.pid).
[root@localhost support-files]# ./mysql.server start
Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/data/localhost.localdomain.pid).
[root@localhost support-files]# ./mysql.server start
Starting MySQL. SUCCESS! 
[1]+  已杀死               ./mysqld_safe --user=mysql(工作目录:/usr/local/mysql/bin)
(当前工作目录:/usr/local/mysql/support-files)
[root@localhost support-files]# cd ..
[root@localhost mysql]# ./bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28

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> alter user 'root'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges
    -> ;
ERROR 1146 (42S02): Table 'mysql.servers' doesn't exist
mysql> flush privileges;
ERROR 1146 (42S02): Table 'mysql.servers' doesn't exist
mysql> exit
Bye
[root@localhost mysql]# ./bin/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.28 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> exit
Bye
[root@localhost mysql]# firewall -cmd --zone=pubilc --add-port=3306/tcp --permanent
-bash: firewall: 未找到命令
[root@localhost mysql]# firewall-cmd --zone=pubilc --add-port=3306/tcp --permanent
Error: INVALID_ZONE: pubilc
[root@localhost mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@localhost mysql]# firewall-cmd --reload
success
[root@localhost mysql]# firewall-cmd --list-port
3306/tcp
[root@localhost mysql]# ./bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 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> select host,user from mysql.user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> update user set user.host='%' where user.user='root';
ERROR 1046 (3D000): No database selected
mysql> update mysql.user set user.host='%' where user.user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> grant all privileges on *.* to root@'%' identified by 'root'
    -> ;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> update mysql.user set user.host='%' where user.user='123';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> flush privileges;
ERROR 1146 (42S02): Table 'mysql.servers' doesn't exist
mysql> exit
Bye
[root@localhost mysql]# ./bin/mysql -u root -p -h
mysql: [ERROR] ./bin/mysql: option '-h' requires an argument
[root@localhost mysql]# ./bin/mysql -u root -p -h192.168.27.2
Enter password: 

123
;
^H^HERROR 1045 (28000): Access denied for user 'root'@'192.168.27.2' (using password: YES)
[root@localhost mysql]# 
[root@localhost mysql]# 123
-bash: 123: 未找到命令
[root@localhost mysql]# ;
-bash: 未预期的符号 `;' 附近有语法错误
[root@localhost mysql]# ./bin/mysql -u root -p -h192.168.27.2
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'192.168.27.2' (using password: YES)
[root@localhost mysql]# ./bin/mysql -u root -p -h192.168.27.2
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.28 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.

-- 下载目录 [root@localhost ~] cd /. [root@localhost ~] mkdir study [root@localhost ~] cd study -- 下载MySQL7的版本 -- https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz -- 下载MySQL8的版本 -- https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz -- 解压 [root@localhost study] tar -zvxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -- /usr/local 目录下创建文件夹存 [root@localhost study] mkdir /usr/local/msyql -- 移动 [root@localhost study] mv mysql-5.7.28-linux-glibc2.12-x86_64/* /usr/lcoal/mysql -- 添加mysql组和mysql 用户: -- 添加mysql组: [root@localhost study] groupadd mysql -- 添加mysql用户: [root@localhost study] useradd -r -g mysql mysql -- 配置MySQL [root@localhost study] cd /usr/local/mysql -- 主:从5.7.18开始不在二进制包中提供my-default.cnf文件 -- 进入MySQL的bin目录 -- 查看MySQL对于配置文件的查找路径,越左优先查找 [root@localhost mysql] ./bin/mysql --help | grep 'Default options' -A 1 -- 创建配置文件 [root@localhost mysql] rm -rf /etc/my.cnf [root@localhost mysql] touch /etc/my.cnf [root@localhost mysql] vi /etc/my.cnf 添加内容 [mysqld] port=3306 datadir=/var/lib/mysql/data socket=/var/lib/mysql/data/mysql.sock user=root max_connections=151 symbolic-links=0 # 设置忽略大小写 lower_case_table_names = 1 # 指定编码 character-set-server=utf8 collation-server=utf8_general_ci #指定客户端连接mysql时的socket通信文件路径 [client] socket=/var/lib/mysql/data/mysql.sock -- 设置开机启动 -- 复制启动脚本到资源目录 [root@localhost mysql] cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql -- 增加 mysqld 服务控制脚本执行权限 [root@localhost mysql] chmod +x /etc/rc.d/init.d/mysql -- 将 mysqld 服务加入到系统服务 [root@localhost mysql] chkconfig --add mysql -- 检查mysqld服务是否已经生效 [root@localhost mysql] chkconfig --list mysql [root@localhost mysql] mkdir /var/lib/mysql/ [root@localhost mysql] mkdir /var/lib/mysql/data [root@localhost mysql] chown -R mysql:mysql /var/lib/mysql/ -- 环境变量配置 [root@localhost mysql] vi /etc/profile 添加如下内容 大概在52行左右 export PATH = $PATH:/usr/local/mysql/bin -- 刷新 [root@localhost mysql] source /etc/profile -- 检测 [root@localhost mysql] echo $PATH [root@localhost mysql] mysqld --user=root --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data --initialize -- 注意显示的最后这一行代码的意思就是密码 -- 2019-10-26T08:14:54.539744Z 0 [Warning] CA certificate ca.pem is self signed. -- 2019-10-26T08:14:54.696247Z 1 [Note] A temporary password is generated for -- root@localhost: i#%RHdF?r8e. -- 启动MySQL [root@localhost mysql] service mysql start -- 修改登入密码 [root@localhost mysql] mysql -u root -p mysql> alter user 'root'@'localhost' identified by 'root'; mysql> flush privileges; mysql> quit; -- 修改/usr/local/mysql/etc/my.cnf 文件 -- 注释skip-grant-tables -- 重启MySQL [root@localhost mysql] service mysql restart -- 测试 [root@localhost mysql] mysql -u root -p -- 配置远程访问 -- 防火墙端口设置,便于远程访问 [root@localhost mysql] firewall-cmd --zone=public --add-port=3306/tcp --permanent [root@localhost mysql] firewall-cmd --reload -- 查看端口 启动防火墙服务:systemctl ummask firewalld 六星教育 SIXSTAREDU.COM
六星教育 SIXSTAREDU.COM 启动防火墙:systemctl start firewalld -- 进入MySQL mysql> grant all privileges on *.* to root@'%' identified by "password"; mysql> flush privileges; -- 如果没有效果可以尝试重启一下MySQL
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值