Mysql8 Centos8 编译安装

安装依赖

1
sudo yum -y install wget tar make  cmake gcc gcc-c++ ncurses  ncurses-devel  libaio-devel  openssl openssl-devel  libtirpc-devel  bison

安装rpcsvc

1
2
3
tar -Jxf rpcsvc-proto-1.4.2.tar.xz && cd rpcsvc-proto-1.4.2
./configure
make && make install

解压boost

1
tar -zvxf boost_1_73_0.tar.gz

安装Mysql

1
2
3
4
5
6
cmake .. -DCMAKE_INSTALL_PREFIX=/opt/mysql -DINSTALL_DATADIR=/opt/mysql/data -DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci -DWITH_SSL=yes -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_EMBEDDED_SERVER=1 \
-DFORCE_INSOURCE_BUILD=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all \
-DWITH_BOOST=/opt/boost_1_73_0 # boost解压的路径

gmake && gmake install

初始化数据库

1
2
# 这个阶段完成之后会有一个密码,记得保存一下 
./bin/mysqld --user=ifan --initialize --basedir=/opt/mysql/mysql --datadir=/opt/mysql/data

创建my.cnf

1
2
3
4
5
ln -s /opt/mysql/my.cnf /etc/my.cnf
# 把Mysql加入系统启动
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# 增加执行权限
chmod 755 /etc/init.d/mysqld

修改启动文件,并加入开机启动

1
2
3
4
5
6
7
8
# 编辑
vi /etc/rc.d/init.d/mysqld
# MySQL程序安装路径
basedir=/usr/local/mysql
# MySQl数据库存放目录
datadir=/data/mysql
# 加入开机启动
chkconfig mysqld on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
user = mysql
socket=/tmp/mysql.sock
tmpdir = /tmp
key_buffer_size=16M
max_allowed_packet=128M
default_authentication_plugin=mysql_native_password
open_files_limit = 60000
server-id = 1
character-set-server = utf8
max_connections = 1000
max_connect_errors = 100000
interactive_timeout = 86400
wait_timeout = 86400
sync_binlog=0
back_log=100
default-storage-engine = InnoDB
log_slave_updates = 1

更新root的密码

1
2
3
4
5
mysql -uroot -p
> 输入之前获得的密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-root-password';
flush privileges;

root远程访问

1
2
3
4
5
6
# 设置远程访问
ALTER USER 'root'@'%' IDENTIFIED BY 'root-password';
use mysql;
update user set host='%' where user ='root';
# 授权
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

创建数据库并授权

1
2
3
4
5
6
7
8
9
10
11
# 创建数据库
CREATE DATABASE IF NOT EXISTS ifandb default charset utf8mb4 COLLATE utf8_general_ci;

# 创建用户
CREATE USER 'ifan'@'localhost' IDENTIFIED BY 'ifan';
# 加密方式为mysql_native_password
CREATE USER 'ifan'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY 'ifan';

# 授权用户 ifan 对数据库 ifandb 具有全部操作权限
GRANT ALL privileges on ifandb.* to 'ifan'@'localhost';
GRANT ALL privileges on ifandb.* to 'ifan'@'127.0.0.1';