1. 删除centos7 自带的mysql数据库
# find / -name mariadb* 查询已安装的mysql
查询结果: /etc/ld.so.conf.d/mariadb-x86_64.conf
/usr/share/doc/mariadb-libs-5.5.56
# rpm -e mariadb-libs-5.5.56 --nodeps 卸载mysql
# find / -name mariadb* 再次 查询已安装的mysql 无结果,说明已删除自带mysql
2.安装依赖包
yum -y install make gcc-c++ cmake bison-devel ncurses-devel
3.下载安装包
下载源码包并解压,这里下载的是带boost的包,MySQL5.7.5之后需要boost库支持,所以这里直接下载带boost的源码包 mysql-boost-5.7.22.tar.gz
tar --zxvf mysql-boost-5.7.22.tar.gz
mkdir /data/mysql 创建目录
进入解压之后的,生成的mysql源代码目录中
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #[MySQL安装的根目录]
-DMYSQL_DATADIR=/data/mysql \ #[MySQL数据库文件存放目录]
-DSYSCONFDIR=/etc \ #[MySQL配置文件所在目录]
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ #[MySQL的sock文件目录]
-DEXTRA_CHARSETS=all \ #[使MySQL支持所有的扩展字符]
-DDEFAULT_CHARSET=utf8 \ #[设置默认字符集为utf8]
-DDEFAULT_COLLATION=utf8_general_ci \ #[设置默认字符校对]
-DWITH_BOOST=boost \ #[指定boost安装路径]
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DMYSQL_TCP_PORT=3306 \ #[MySQL的监听端口]
-DENABLED_LOCAL_INFILE=1 \ #[启用加载本地数据]
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_EMBEDDED_SERVER=OFF \
-DWITH_SYSTEMD=1 #[用systemd管理mysql]
make && make install
4.创建mysql用户,并更改权限
groupadd mysql
useradd -g mysql mysql
chown -R mysql: /usr/local/mysql/
chown -R mysql: /data/mysql
将mysqld.servie复制到/usr/lib/systemd/system/
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
5.创建配置文件
vim /etc/my.cnf
内容:
[mysqld]v
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/usr/local/mysql/mysql.sock
log_error=/log/mysql/mysql.log
server-id=1
explicit_defaults_for_timestamp=true
6.创建log目录与pid目录,并修改权限
mkdir -p /log/mysql
chown -R mysql: /log/mysql
mkdir -p /var/run/mysqld
ls -ld /var/run/mysqld/
chown -R mysql: /var/run/mysqld #可以通过grep pid /usr/lib/systemd/system/mysqld.service查看pid文件位置
8.添加环境变量
echo "export PATH=/usr/local/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh
source /etc/profile
9.初始化 mysql 数据库
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
注释:--initialize 会生成一个随机密码,而 -–initialize-insecure 不会生成密码,在MySQL安全配置向导mysql_secure_installation设置密码时,可自由选择 mysql 密码等级。--datadir目标目录下不能有数据文件。
10. 启动数据库
systemctl start mysqld
systemctl status mysqld
9.配置安全向导
开始: mysql_secure_installation
[root@MySql ~]# mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
10.测试链接
[root@MySql ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22 Source distribution
Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
11. 开放 Root 远程连接权限
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> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by 'password';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)
12.开放3306端口
firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --reload
update user set Host = '%' where User="root";
GRANT ALL PRIVILEGES ON * TO 'root'@'%' IDENTIFIED BY '723945031' WITH GRANT OPTION;