一、先安装依赖库:

yum install autoconf libaio -y

常见报错:

1、如果提示下列报错:

[root@centos7 mysql]# scripts/mysql_install_db --user=mysql
FATAL ERROR: please install the following Perl modules before executing scripts/mysql_install_db:
Data::Dumper

安装autoconf

# yum install autoconf -y

2、如果提示下列报错:

[root@centos7 mysql]# scripts/mysql_install_db --user=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

安装libaio

# yum install libaio -y

3、如果提示下列报错:

[root@localhost mysql]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

解决办法:
检查my.cnf文件是否配置正确 systemctl start mysql文件是否配置正确
源码安装不要用systemctl start mysql 用./mysql.server start

[root@localhost support-files]# ./mysql.server start
Starting MySQL SUCCESS!

二、编写mysql安装脚本

# vim mysql.sh
#!/bin/bash
tar xzf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
useradd -r -s /sbin/nologin mysql
chown -R mysql.mysql /usr/local/mysql
cd /usr/local/mysql
rpm -e --nodeps mariadb-libs
scripts/mysql_install_db --user=mysql
cp support-files/mysql.server /etc/init.d/mysql
service mysql start
# 追加/usr/local/mysql/bin目录到环境变量
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile
# source mysql.sh

三、初始化mysql(配置MySQL密码以及安全策略)

# mysql_secure_installation

测试密码(生产环境中一定要用复杂密码)

# mysql -uroot -p
Enter password:666
# mysql -h 服务器IP地址 -P 端口号 -uroot -p    # 远程登录

1、编写mysql.service服务脚本

第一步:创建一个mysql.service文件

# vim /usr/lib/systemd/system/mysql.service

第二步:写入内容

[Unit]
Description=MySQL Server
After=network.target
After=syslog.target

[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf
LimitNOFILE = 5000
PrivateTmp=false

[Install]
WantedBy=multi-user.targe

2、重载配置文件

[root@localhost ~]# systemctl daemon-reload

3、重启服务

# systemctl restart mysql

4、开机自启

# systemctl enable mysql

5、开启远程:

  • 改表法
    登入mysql,更改"mysql"数据库里的"user"表里的"host"项,从"localhost"改称"%"
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> 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 host = '%' where user = 'zabbix';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select host,user from user;
+-----------+--------+
| host      | user   |
+-----------+--------+
| %         | root   |
| %         | zabbix |
| 127.0.0.1 | root   |
| ::1       | root   |
+-----------+--------+
4 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye
  • 授权法
    在安装mysql的机器上运行:
    1、d:\mysql\bin\>mysql -h localhost -u root
    这样应该可以进入MySQL服务器
    2、mysql>GRANT ALL PRIVILEGES ON . TO 'root'@'%'WITH GRANT OPTION
    赋予任何主机访问数据的权限
    例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。
    GRANT ALL PRIVILEGES ON . TO 'myuser'@'%'IDENTIFIED BY 'mypassword' WI TH GRANT OPTION;
    如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码
    GRANT ALL PRIVILEGES ON . TO 'myuser'@'192.168.1.3'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;
    3、mysql> FLUSH PRIVILEGES
    修改生效
    4、mysql> EXIT
    退出MySQL服务器,这样就可以在其它任何的主机上以root身份登录