【mysql】liunx服务器安装mysql服务器
一、下载mysql安装包
https://www.mysql.com/downloads/
二、安装包进行解压
2.1、解压安装包
在目录/usr/local/目录下
mysql-8.0.39-linux-glibc2.28-x86_64.tar
tar -xvf mysql-8.0.39-linux-glibc2.28-x86_64.tar tar -xvf mysql-8.0.39-linux-glibc2.28-x86_64.tar.xz
将解压出来的文件重名为mysql文件
mv mysql-8.0.39-linux-glibc2.28-x86_64 mysql
2.2、创建mysql用户组和mysqlu用户,并授权
#创建用户组 groupadd mysql #创建用户组 # -r 参数表示 mysql 用户是系统用户,不可用于登录系统,创建用户 mysql 并将其添加到用户组 mysql 中 useradd -r -g mysqlu mysql #分配用户组 sudo chown -R mysqlu /usr/local/mysql/ # 将文件的所有属性改为 mysql 用户 sudo chgrp -R mysql /usr/local/mysql/ # 将组属性改为 mysql 组 #创建数据目录并赋予权限 sudo mkdir -p /data/mysql #数据目录 sudo chown mysqlu:mysql -R /data/mysql
2.3、编辑/etc/my.cnf并保存配置文件内容
[mysqld] # 设置3306端口 port=3306 #启动用户 user=mysqlu # 设置mysql的安装目录 basedir=/usr/local/mysql # 设置mysql数据库的数据的存放目录 datadir=/usr/local/mysql/data #为MySQL客户端程序和服务器之间的本地通讯指定一个套接字文件 socket =/usr/local/mysql/mysql.sock #pid文件所在目录 pid-file =/usr/local/mysql/mysql.pid # 允许最大连接数 max_connections=200 # 允许连接失败的次数。 max_connect_errors=10 # 服务端使用的字符集默认为utf8mb4 character-set-server=utf8mb4 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB # 默认使用“mysql_native_password”插件认证 #mysql_native_password #default_authentication_plugin=mysql_native_password [mysql] # 设置mysql客户端默认字符集 default-character-set=utf8mb4 [client] # 设置mysql客户端连接服务端时默认使用的端口 port=3306 default-character-set=utf8mb4 socket =/usr/local/mysql/mysql.sock
2.4、创建数据存储目录并授权
2.5、初始化MySQL数据库
关注初始化密码: alGijw.Gh6we
cd /usr/local/mysql/bin/
sudo ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --user=mysqlu --initialize
sudo ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --user=mysqlu --initialize
2024-07-30T13:00:35.813404Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.39) initializing of server in progress as process 1712646
2024-07-30T13:00:35.823399Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-07-30T13:00:36.633252Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-07-30T13:00:39.650550Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: alGijw.Gh6we
2.5、启动mysql数据库服务
加入到系统服务
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
启动mysql服务
sudo service mysql start Starting MySQL.Logging to '/usr/local/mysql/data/VM-212-227-tencentos.err'. . SUCCESS!
登录数据库并修改初始密码
[xxshang@VM-212-227-tencentos mysql]$ mysql -u root ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [xxshang@VM-212-227-tencentos mysql]$ 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.39 Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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 "shangxiaofei"; Query OK, 0 rows affected (0.02 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql>