1、安装依赖

    yum install -y libaio
    yum -y install autoconf

2、下载和解压缩
#下载MySQL安装包,这里使用的是MySQL 5.5.58
mysql-5.5.58-linux-glibc2.12-x86_64.tar.gz (189.39 MB, 下载次数: 1)

    tar -zxvf mysql-5.5.58-linux-glibc2.12-x86_64.tar.gz
#将解压缩后的文件夹移动到/usr/local/并命名为mysql
    mv mysql-5.5.58-linux-glibc2.12-x86_64 /usr/local/mysql

3、授权和安装

#添加用户,将使用该用户执行MySQL进程
    useradd -s /bin/false -M mysql
#修改MySQL目录权限
    chown -R mysql:mysql /usr/local/mysql
#安装并指定用户和data文件夹位置
    /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --user=mysql

#看到如下界面说明成功,若出错请往下翻查看解决方法(依赖错误)

#更改所属用户和组,除了mysql目录下的data目录所属用户不变,其他所有文件的所属用户改回root
    chown -R root /usr/local/mysql/
    chown -R mysql /usr/local/mysql/data/

#拷贝配置文件,将mysql的配置文件拷贝为/etc/目录下的my.cnf,若提示已存在直接覆盖
    cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
#拷贝启动程序,将mysql的启动程序拷贝到/etc/init.d/目录下
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld


#安装完成

4、启动和后期设置

#启动服务
    service mysqld start

#启动成功

#测试连接
    /usr/local/mysql/bin/mysql -uroot

#加入环境变量,编辑/etc/profile,这样可以在任何地方用mysql命令了
    vim /etc/profile

#在文件最一行加入以下内容
export PATH=$PATH:/usr/local/mysql/bin

#保存之后执行source重新加载环境变量
    source /etc/profile
#首次修改密码
    mysqladmin -u'root' password 'PassWord'
#之后修改密码
     mysqladmin -u'root' -p'PassWord' password 'NewPassWord'
    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

#设置数据库表名不区分大小写(这个常用配置不要忘记),编辑 my.conf

``vim /etc/my.cnf``
\#加入以下内容
``lower_case_table_names = 1``
重启MySQL生效

5、常用命令

#启动
    service mysqld start
#关闭
    service mysqld stop
#重新启动
    service mysqld restart
#查看运行状态
    service mysqld status
#允许远程用户登录访问(登入mysql后操作)
    grant all on *.* to 'root'@'%' identified by '123' with grant option;
#刷新权限(登入mysql后操作)
    flush privileges;

6、安装时遇到错误:
错误1:

# ./scripts/mysql_install_db –user=mysql –datadir=/usr/local/mysql/data/mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data:: Dumper

解决:

yum -y install autoconf

错误2:

# ./scripts/mysql_install_db –user=mysql –datadir=/usr/local/mysql/data/mysql
Installing MySQL system tables…./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

解决:

yum install -y libaio

7、MySQL添加开机自启动

# 1、将服务文件拷贝到init.d下,并重命名为mysqld
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 2、赋予可执行权限
    chmod +x /etc/init.d/mysqld
# 3、添加服务
    chkconfig --add mysqld
# 4、显示服务列表
    chkconfig --list

如果看到mysql的服务,并且3,4,5都是on的话则成功,如果是off,则键入
chkconfig –level 345 mysqld on

8、创建用户和表并授权

# 1、创建用户
    CREATE USER 'username'@'host' IDENTIFIED BY 'password';
    CREATE USER 'zhangkuan'@'localhost' IDENTIFIED BY '123456';
# 2、创建库
    CREATE DATABASE databasename;
    CREATE DATABASE zhangkuan;
# 3、授权
    GRANT PRIVILEGES ON databasename.tablename TO 'username'@'host';
    GRANT SELECT, INSERT ON guitu.user TO 'guitu'@'%';
    GRANT ALL ON guitu.* TO 'guitu'@'%';
# 4、授权用户拥有授权权限
    GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
    GRANT ALL ON zhangkuan.* TO 'guitu'@'%' WITH GRANT OPTION;
# 5、撤销用户权限
    REVOKE privilege ON databasename.tablename FROM 'username'@'host';
Last modification:May 29th, 2020 at 03:42 pm