分享生活,记录成长
表达自我,拥抱世界

MYSQL安装及配置

检查是否安装过mysql或mariadb,有则卸载

[root@krist mysqldir]# rpm -qa maria* || mysql*
mariadb-libs-5.5.65-1.el7.x86_64
[root@krist mysqldir]# rpm -e --nodeps mariadb-libs-5.5.65-1.el7.x86_64

检查是否存在my.cnf文件,存在则删除

[root@krist mysqldir]# find / -name "my.cnf"

安装依赖环境

[root@krist mysqldir]# yum -y install libevent* libtool* autoconf* libstd* ncurse* bison* openssl*

安装gcc编译工具

yum install -y gcc libgcc gcc-c++ compat-gcc

安装cmake(mysql5.5之后需要用cmake支持编译安装),https://cmake.org/download/

wget https://github.com/Kitware/CMake/releases/download/v3.18.2/cmake-3.18.2.tar.gz
[root@krist mysqldir]# tar zxf cmake-3.18.2.tar.gz 
[root@krist mysqldir]# cd cmake-3.18.2/
[root@krist cmake-3.18.2]# ./configure
[root@krist cmake-3.18.2]# make && make install

下载mysql源码包,编译安装需要boost 库,这里官网下载含boost的源码包,https://downloads.mysql.com/archives/community/

download-mysql

[root@krist mysqldir]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.30.tar.gz
[root@krist mysqldir]# tar zxvf mysql-boost-5.7.30.tar.gz
[root@krist mysqldir]# cd mysql-5.7.30/

新增mysql用户,并且禁止mysql用户登录

[root@krist mysql-5.7.30]# groupadd mysql
[root@krist mysql-5.7.30]# useradd -s /sbin/nologin -g mysql mysql

创建一个用于保存mysql数据库和数据库文件的目录

[root@krist mysql-5.7.30]# mkdir -p /usr/local/mysql/var
[root@krist mysql-5.7.30]# chown -Rf mysql:mysql /usr/local/mysql

接下来编译并安装mysql数据库服务程序

[root@krist mysql-5.7.30]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=boost
[root@krist mysql-5.7.30]# make -j2 && make -j2 install

进入mysql安装目录,生成mysql数据库配置文件

[root@krist mysql-5.7.30]# cd /usr/local/mysql
[root@krist mysql]# ./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var
2020-09-02 15:00:16 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2020-09-02 15:00:18 [WARNING] The bootstrap log isn't empty:
2020-09-02 15:00:18 [WARNING] 2020-09-02T07:00:16.859801Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2020-09-02T07:00:16.860169Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2020-09-02T07:00:16.860173Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)

配置mysql配置文件my.cnf,如果没有则创建,参考:https://blog.csdn.net/djCode/article/details/78621772

[root@krist mysql]# vim /etc/my.cnf
[root@krist mysql]# more /etc/my.cnf 
[client]
port = 3306
socket = /tmp/mysql.sock
[mysql]
[mysqld]
character-set-server = utf8
#服务端口号 默认3306
port = 3306
#mysql安装根目录
basedir = /usr/local/mysql
#mysql数据文件所在位置
datadir = /usr/local/mysql/var
#设置socke文件所在目录
socket  = /tmp/mysql.sock
#最大连接数
max_connections = 400
#最大错误连接数
max_connect_errors = 1000

生成开机启动文件,便于管理,并修改46、47行

[root@krist mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@krist mysql]# chmod 755 /etc/rc.d/init.d/mysqld
[root@krist mysql]# vim /etc/rc.d/init.d/mysqld
 46 basedir=/usr/local/mysql
 47 datadir=/usr/local/mysql/var

启动mysqld,设置开机启动

[root@krist mysql]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/var/krist.com.err'.
 SUCCESS!
[root@Krist mysql]# chkconfig --add mysqld
[root@Krist mysql]# chkconfig mysqld on

修改环境变量/etc/profile,在如下位置添加export PATH=$PATH:/usr/local/mysql/bin

[root@krist /]# vim /etc/profile
    fi
done
export PATH=$PATH:/usr/local/mysql/bin
unset i
unset -f pathmunge
[root@krist /]# source /etc/profile

msql数据库服务程序还会调用到一些程序文件和函数库文件。由于当前是通过源码包方式安装mysql数据库,因此现在也必须以手动方式把这些文件链接过来。

[root@krist /]# cd /usr/local/mysql/
[root@krist mysql]# mkdir -p /var/lib/mysql
[root@krist mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
[root@krist mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
[root@krist mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql

接下来对mysql数据库进行初始化

[root@krist mysql]# cd bin/
[root@krist bin]# mysql_secure_installation 

Securing the MySQL server deployment.

Connecting to MySQL server using password in '/root/.mysql_secret'

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: 1  //强度1中等
Using existing password for root.

Estimated strength of the password: 50 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y   //为root管理员设置数据库密码

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) : y
Success.    //禁止root管理员从远程登录

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    //删除test数据库并取消对其的访问权限
 - 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! 

连接测试成功!

[root@krist bin]# mysql -h localhost -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.30 Source distribution

Copyright (c) 2000, 2020, 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> 

若出现以下错误,更新root账户密码即可

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

alter user 'root'@'localhost' identified by '123456';
 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:DDblog » MYSQL安装及配置

分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

立即登录   注册

DD博客

联系我们每天好心情
切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活