mysql8.0环境搭建linux
本文主要介绍如何在linux环境(64位)下搭建mysql8.0的数据库环境
1、到指定目录下下载安装包
1 2 | [root@minio3 ~] # cd /usr/local/src [root@minio3 src] # wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz |
或者:
(1)登录-注册:http://resources.kittytiger.cn/
(2)签到获取积分
(3)搜索:mysql8.0 linux安装包
获得安装包
2、解压mysql8, 通过xz命令解压出tar包(这里时间可能会久一点根据服务器性能决定请耐心等待) 然后通过tar命令解压出文件夹
1 2 | [root@minio3 src] # xz -d mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz [root@minio3 src] # tar xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar |
3、将/usr/local/src下的mysql-8.0.20-linux-glibc2.12-x86_64文件夹内容移动到/usr/local/mysql下
1 2 3 4 5 | [root@minio3 local ] # cd /usr/local [root@minio3 local ] # mkdir mysql [root@minio3 local ] # cd /usr/local/src [root@minio3 src] # mv /usr/local/src/mysql-8.0.20-linux-glibc2.12-x86_64/* /usr/local/mysql [root@minio3 src] # cd /usr/local/mysql |
4、创建用户组及用户和密码并授权
1 2 3 | [root@minio3 mysql] # groupadd mysql [root@minio3 mysql] # useradd -g mysql mysql [root@minio3 mysql] # chown -R mysql.mysql /usr/local/mysql |
5、编辑my.cnf文件
[root@minio3 mysql]# vim /etc/my.cnf
清空默认的配置将如下内容复制到配置文件中
[mysqld]
user=root
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
port=3306
max_connections=200
max_connect_errors=10
character-set-server=utf8
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
lower_case_table_names=1
group_concat_max_len=102400
wait_timeout=31536000
interactive_timeout=31536000
[mysql]
default-character-set=utf8
[client]
port=3306
default-character-set=utf8
6、初始化基础信息,最后一行后面会有个随机的初始密码保存下来一会登录要用(如果忘记了就删掉data重新初始化)
1 2 3 4 5 6 7 8 9 10 | [root@minio3 mysql] # cd bin [root@minio3 bin] # ./mysqld --initialize 2022-04-27T08:57:45.274724Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 150492 2022-04-27T08:57:45.277480Z 0 [Warning] [MY-013242] [Server] --character- set -server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 2022-04-27T08:57:45.291420Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2022-04-27T08:57:46.124824Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2022-04-27T08:57:47.882182Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: OLOkz_e)c8Jw 注意上面最后一行生成的默认密码:OLOkz_e)c8Jw 后面登录要用 |
7、添加mysqld服务到系统,授权以及添加服务
1 2 3 4 | [root@minio3 bin] # cd .. [root@minio3 mysql] # cp -a ./support-files/mysql.server /etc/init.d/mysql [root@minio3 mysql] # chmod +x /etc/init.d/mysql [root@minio3 mysql] # chkconfig --add mysql |
8、启动mysql
1 2 3 4 | 注:任何路径都可以 [root@minio3 opt] # service mysql start Starting MySQL.Logging to '/usr/local/mysql/data/minio3.err' . . SUCCESS! |
9、将mysql添加到命令服务
1 | ln -s /usr/local/mysql/bin/mysql /usr/bin |
10、登录mysql
1 2 3 4 5 6 | 注:输入第6步中生成的初始密码 [root@minio3 opt] # 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.20 |
11、更改密码
1 2 3 4 | mysql> ALTER USER 'root' @ 'localhost' IDENTIFIED WITH mysql_native_password BY 'pass' ; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) |
12、更改root连接权限,创建数据库devdb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | mysql> use mysql mysql> create database devdb; Query OK, 1 row affected (0.00 sec) mysql> update user set host= '%' where user = 'root' ; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) --查看数据 mysql> show databases; +--------------------+ | Database | +--------------------+ | devdb | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.01 sec) --查看数据库版本 mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.20 | +-----------+ 1 row in set (0.00 sec) --查看数据库字符集 mysql> SHOW VARIABLES LIKE 'character%' ; +--------------------------+----------------------------------+ | Variable_name | Value | +--------------------------+----------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql/share/charsets/ | +--------------------------+----------------------------------+ 8 rows in set (0.01 sec) |
13、退出数据库
1 2 | mysql> exit ; Bye |
14、再次登录数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 注:要输入修改后的密码,此时已经可以用客户端工具连接mysql数据库 [root@minio3 opt] # mysql -uroot -ppass; mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 8.0.20 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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> |
15、创建用户并赋权
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #进入到mysql.db mysql> use mysql; Database changed #查看当前的用户以及可访问的地址还有授权能力 mysql> select user,host,grant_priv from user; +------------------+-----------+------------+ | user | host | grant_priv | +------------------+-----------+------------+ | root | % | Y | | mysql.infoschema | localhost | N | | mysql.session | localhost | N | | mysql.sys | localhost | N | +------------------+-----------+------------+ 4 rows in set (0.00 sec) #如果root用户无授权能力(grant_priv = 'N'),则可以开起来 update mysql.user set Grant_priv= 'Y' where user= 'root' ; #创建用户,最后的参数是密码。‘localhost’表示只能本机登录,也可以设置你期望的登录ip,不设置的话默认是‘%’,也就是都可以登录 mysql> create user 'testuser' @ '%' identified by 'testpwd' ; Query OK, 0 rows affected (0.00 sec) #给新用户授权,这里授予的权限是只能操作一个数据库(数据库名为:testdb,把数据库名变成 *.* 就是给所有数据库的权限) #如果root用户只允许localhost登录,而这里授权的时候又给用户授予不管任何地址都可以登录的能力,则会报无权限的错。 #此时需要把root用户也改成‘%’的登录限制才行.也就是说,授权者的可登录源ip范围应该 ≥ 被授权者的可登录源ip范围 #grant all privileges on happy_db.* to 'testdb'@'localhost'; mysql> grant all privileges on testdb.* to 'testuser' @ '%' ; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #删除对应的用户 drop user testuser;<br><br><br> |
如果启动时工程报错:Fatal error occurred in the transaction branch-check your data for consistency
解决:
mysql> GRANT XA_RECOVER_ADMIN ON *.* TO 'testuser'@'%';
mysql> flush privileges;
补充:
查看数据库默认字符集
mysql> show create database mytestdb;
修改数据库默认字符集
mysql> alter database mytestdb charset utf8mb4;
资源丰富的的网盘资源:网盘资源大全! 推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!