linux安装mysql

1、下载tar包:

wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-linux-glibc2.12-x86_64.tar.gz

2、解压安装包

 tar -xvf mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz
  • 报错解决
    如果报如下错误:
tar (child): xz: Cannot exec: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

请安装组件:

yum install -y xz 

3、将解压的文件重命名mysql,并移动到/usr/local目录下

mv mysql-8.0.13-linux-glibc2.12-x86_64 mysql
mv mysql /usr/local/

4、进入/usr/local目录,创建用户和用户组并授权

cd /usr/local/
groupadd mysql
useradd -r -g mysql mysql
cd mysql/ #注意:进入mysql文件下授权所有的文件
chown -R mysql:mysql ./

5、在/usr/local/mysql目录下,创建data文件夹

cd /usr/local/mysql
mkdir data

6、初始化数据库,这里会生成随机密码,不用记(个人觉得太麻烦),后面直接改密码

bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

7、修改/usr/local/mysql当前目录的用户

chown -R root:root ./
chown -R mysql:mysql data

8、创建my.conf文件

cd support-files/
touch my-default.cnf
chmod 777 ./my-default.cnf 
cd ../
cp support-files/my-default.cnf /etc/my.cnf 

9、配置my.cnf

vim /etc/my.cnf 
[mysqld]
 
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
 
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
 
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid
tmpdir = /tmp
port = 3306
#lower_case_table_names = 1
# server_id = .....
# socket = .....
#lower_case_table_names = 1
max_allowed_packet=32M
default-authentication-plugin = mysql_native_password
#lower_case_file_system = on
#lower_case_table_names = 1
log_bin_trust_function_creators = ON
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

10、开机自启

cd support-files/
cp mysql.server /etc/init.d/mysql 
chmod +x /etc/init.d/mysql

11、注册服务

chkconfig --add mysql

12、查看是否成功

chkconfig --list mysql
image.png

13、etc/ld.so.conf要配置路径

vim /etc/ld.so.conf

添加如下内容:

/usr/local/mysql/lib

14、配置环境变量

vim /etc/profile

添加如下内容:

#MYSQL ENVIRONMENT
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
image.png

15、修改密码

/etc/init.d/mysql stop
mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
use mysql; 
 
update user set authentication_string='' where user='root';--将字段置为空
 
ALTER user 'root'@'localhost' IDENTIFIED BY 'root';--修改密码为root

如果出错:

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> GRANT ALL PRIVILEGES ON *.* TO IDENTIFIED BY '123' WITH GRANT OPTION;

执行:

flush privileges;

再执行

ALTER user 'root'@'localhost' IDENTIFIED BY 'root';--修改密码为root

16、开启navicat远程连接

mysql -uroot -p #进入数据库
> use mysql;#进入数据库
> select host, user, authentication_string, plugin from user;#查看用户信息
> GRANT ALL ON *.* TO 'root'@'%';#授权root用户可以远程登陆
> flush privileges;#立即生效
> exit;#退出
# service mysql restart#重启mysql服务

如果navicat还是无法连接,检查防火墙,将3306放通

firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload

你可能感兴趣的:(linux安装mysql)