mysql 服务器安装

一、安装mysql
1.下载mysql
wget https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

2.解压

cd /usr/local/env/mysql
tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

3.添加用户组mysql和用户mysql,并将其添加到mysql用户组中

groupadd mysql
useradd -r -g mysql mysql

3.检查是否安装了 libaio

rpm -qa | grep libaio

没有安装的安装以下
4.配置my.cnf文件

vi /etc/my.cnf
[mysql]  
# 设置mysql客户端默认字符集  
default-character-set=utf8   
socket=/tmp/mysql.sock
[mysqld]  
#skip-name-resolve  
#设置3306端口  
port=3306   
socket=/tmp/mysql.sock
# 设置mysql的安装目录 
basedir=/usr/local/env/mysql/mysql  
# 设置mysql数据库的数据的存放目录 
datadir=/usr/local/env/mysql/mysql/data  
# 允许最大连接数  
max_connections=200  
# 服务端使用的字符集默认为8比特编码的latin1字符雿 
character-set-server=utf8  
# 创建新表时将使用的默认存储引擿 
default-storage-engine=INNODB  
#lower_case_table_name=1  
max_allowed_packet=16M  

5.创建data文件夹

cd /usr/local/env/mysql/mysql
mkdir data

6.将mysql目录的所属用户和组改为mysql

chown -R mysql:mysql ./

7.初始化mysqld 生成初始化密码

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

控制台输出的最后结尾的就是密码,记住他。

二、配置
1.复制mysql.server脚本到资源目录,并赋予执行权限

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

2.将 mysqld 服务加入到系统服务并检测是否生效

chkconfig --add mysqld
chkconfig --list mysqld

出现如下结果说明生效:

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
 
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
 
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

3.启动mysqld

service mysqld start

如果报错log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
执行以下3个命令,再启动即可。

mkdir /var/log/mariadb 
touch /var/log/mariadb/mariadb.log 
chown -R mysql:mysql  /var/log/mariadb/

三、配置环境变量
1.打开/etc/profile配置文件,添加如下内容

    #mysql环境变量
    PATH=$PATH:/usr/local/env/mysql/mysql/bin
    export PATH

2.执行命令,使其生效

source /etc/profile

校验是否成功echo $PATH

3.初次登录 修改访问密码

mysql -uroot -p

此时的登陆密码就是上面所记住的密码。
4.修改登录密码

mysql> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> update user set authentication_string=PASSWORD('123456') where User='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1
 
mysql>

5.允许远程访问

mysql> grant all privileges on *.* to root@"%" identified by "password" with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql>

5.使用navicate远程连接报错
解决方法:
5.1 关闭mysql服务

service mysql stop

5.2 修改/etc/my.cnf修改为无密码登录

# mysql无密码登录
skip-grant-tables

5.3 重启mysql服务

service mysql restart

5.4无密码登录mysql

[root@baidu64 mysql]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2017, 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>

5.5 再次修改mysql密码

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> update mysql.user set authentication_string=password('123456') where user='root' ;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 2  Changed: 1  Warnings: 1
 
mysql>

5.6 然后将my.cnf无密码登录配置去掉
5.7 退出mysql,并重启
5.8 再次连接,连接成功。

ok。
原文链接:https://blog.csdn.net/qq_35206261/article/details/81321201

你可能感兴趣的:(mysql 服务器安装)