在Oracle Linux 6.7中安装MySQL 8.0.33 mysql安装位置:/mysqlsoft/mysql,数据库文件数据位置:/mysqldata/mysql
1.首先下载安装介质
mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
2. 在根目录下创建文件夹mysqlsoft和数据库数据文件/mysqldata/mysql
[root@sjbf /]# mkdir -p /mysqlsoft [root@sjbf /]# mkdir -p /mysqldata/mysql
3.上传介质mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz到/mysqlsoft目录中并解压
[root@sjbf /]# cd /mysqlsoft [root@sjbf mysqlsoft]# ll total 527224 -rw-r--r-- 1 root root 539869923 May 5 09:50 mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz [root@sjbf mysqlsoft]# tar xvJf mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz [root@sjbf mysqlsoft]# ll total 600560 drwxr-xr-x 9 root root 4096 May 6 03:49 mysql-8.0.33-linux-glibc2.12-x86_64 -rw-r--r-- 1 root root 614964216 May 5 15:28 mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz
建议一般不要修改默认文件名,通过软连接来完成
[root@sjbf mysqlsoft]# ln -s mysql-8.0.33-linux-glibc2.12-x86_6 mysql [root@sjbf mysqlsoft]# ll total 600560 lrwxrwxrwx 1 root root 34 May 6 03:53 mysql -> mysql-8.0.33-linux-glibc2.12-x86_6 drwxr-xr-x 9 root root 4096 May 6 03:49 mysql-8.0.33-linux-glibc2.12-x86_64 -rw-r--r-- 1 root root 614964216 May 5 15:28 mysql-8.0.33-linux-glibc2.12-x86_64.tar.xz [root@sjbf mysqlsoft]# cd mysql [root@sjbf mysql]# ll total 312 drwxr-xr-x 2 7161 31415 4096 Mar 17 02:46 bin drwxr-xr-x 2 7161 31415 4096 Mar 17 02:46 docs drwxr-xr-x 3 7161 31415 4096 Mar 17 02:46 include drwxr-xr-x 6 7161 31415 4096 Mar 17 02:46 lib -rw-r--r-- 1 7161 31415 284945 Mar 17 01:22 LICENSE drwxr-xr-x 4 7161 31415 4096 Mar 17 02:46 man -rw-r--r-- 1 7161 31415 666 Mar 17 01:22 README drwxr-xr-x 28 7161 31415 4096 Mar 17 02:46 share drwxr-xr-x 2 7161 31415 4096 Mar 17 02:46 support-files
4. 创建mysql用户与用户组
[root@sjbf /]# groupadd mysql [root@sjbf /]# useradd -r -g mysql -s /bin/false mysql [root@sjbf /]# id mysql uid=492(mysql) gid=54323(mysql) groups=54323(mysql)
因为用户只用于所有权目的,而不是登录目的,useradd命令使用-r与-s /bin/false选项来创建一个用户没有登录服务器主机的权限。
5.修改/mysqlsoft/mysql与/mysqldata/mysql目录权限
[root@sjbf /]# chown -R mysql:mysql /mysqlsoft/mysql [root@sjbf /]# chown -R mysql:mysql /mysqldata/mysql [root@sjbf /]# chmod -R 775 /mysqlsoft/mysql [root@sjbf /]# chmod -R 775 /mysqldata/mysql
6. MySQL对于libaio库有依赖性。台果这个libaio库没有安装那么数据目录初始化与后续的数据库服务启动将会失败,安装libaio库执行以下操作:
查询是否安装了libaio库
[root@sjbf /]# yum search libaio Loaded plugins: aliases, changelog, kabi, presto, refresh-packagekit, security, tmprepo, ulninfo, verify, versionlock Loading support for kernel ABI =========================================================================================================== N/S Matched: libaio =========================================================================================================== libaio.i686 : Linux-native asynchronous I/O access library libaio.x86_64 : Linux-native asynchronous I/O access library libaio-devel.i686 : Development files for Linux-native asynchronous I/O access libaio-devel.x86_64 : Development files for Linux-native asynchronous I/O access Name and summary matches only, use "search all" for everything.
如果没有安装,可以执行下面的命令来安装
[root@sjbf /]# yum install libaio Loaded plugins: aliases, changelog, kabi, presto, refresh-packagekit, security, tmprepo, ulninfo, verify, versionlock Loading support for kernel ABI Setting up Install Process Package libaio-0.3.107-10.el6.x86_64 already installed and latest version Nothing to do
7.配置mysql参数
只是设置几个简单的mysql运行参数
[root@sjbf mysql]# vi my.cnf [mysqld] basedir=/mysqlsoft/mysql datadir=/mysqldata/mysql bind-address=0.0.0.0 user=mysql port=3306 mysqlx_port=33060 log-error=/mysqldata/mysql/mysql.err pid-file=/mysqldata/mysql/mysqld.pid socket = /mysqlsoft/mysql/mysql.sock mysqlx_socket=/mysqlsoft/mysql/mysqlx.sock character-set-server=utf8 default-storage-engine=INNODB
注意:log-error 一定要配置,因为如果mysql启动错误,可以从日志文件中找到错误原因。其次bind-address配置0.0.0.0是为了监听所有的连接。还有就是socket参数所指定的mysql.sock文件的路径最好设置为/tmp/mysql.sock,因为unix socket文件的缺省位置在/tmp目录中。
8.初始化mysql
[root@sjbf bin]# ./mysqld --user=mysql --defaults-file=/mysqlsoft/mysql/my.cnf --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql --initialize 2023-05-05T20:21:18.355164Z 0 [System] [MY-013169] [Server] /mysqlsoft/mysql-8.0.33-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.33) initializing of server in progress as process 26730 2023-05-05T20:21:18.412269Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2023-05-05T20:21:25.831475Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2023-05-05T20:21:42.025674Z 0 [ERROR] [MY-000067] [Server] unknown variable 'defaults-file=/mysqlsoft/mysql/my.cnf'. 2023-05-05T20:21:42.025812Z 0 [ERROR] [MY-013236] [Server] The designated data directory /mysqldata/mysql/ is unusable. You can remove all files that the server added to it. 2023-05-05T20:21:42.025914Z 0 [ERROR] [MY-010119] [Server] Aborting 2023-05-05T20:21:53.091946Z 0 [System] [MY-010910] [Server] /mysqlsoft/mysql-8.0.33-linux-glibc2.12-x86_64/bin/mysqld: Shutdown complete (mysqld 8.0.33) MySQL Community Server - GPL.
调整一下参数顺序
[root@sjbf bin]# ./mysqld --defaults-file=/mysqlsoft/mysql/my.cnf --initialize --user=mysql --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql [root@sjbf mysql]# cat mysql.err 2023-05-05T20:26:01.800998Z 0 [System] [MY-013169] [Server] /mysqlsoft/mysql-8.0.33-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.33) initializing of server in progress as process 27188 2023-05-05T20:26:01.805553Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. 2023-05-05T20:26:01.870227Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2023-05-05T20:26:09.160682Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2023-05-05T20:26:25.588664Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: #uVfq!szm7y/
其中[Note] A temporary password is generated for root@localhost:#uVfq!szm7y/ 的root@localhost: 后面跟的是mysql数据库登录的临时密码,各人安装生成的临时密码不一样。可以看到到日志文件没有报错,而且有了临时密码,表示初始化成功。
9. 如果想服务能够部署自动支持安全连接,使用mysql_ssl_rsa_setup工具来创建缺省SSL与RSA文件
[root@sjbf bin]# ./mysql_ssl_rsa_setup --datadir=/mysqldata/mysql
10.启动mysql服务
[root@sjbf /]# sh /mysqlsoft/mysql/support-files/mysql.server start /mysqlsoft/mysql/support-files/mysql.server: line 239: my_print_defaults: command not found /mysqlsoft/mysql/support-files/mysql.server: line 259: cd: /usr/local/mysql: No such file or directory Starting MySQLCouldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)[FAILED]
启动mysql服务命令会报错,因为没有修改mysql的配置文件修改Mysql配置文件,修改前为以下内容
if test -z "$basedir" then basedir=/usr/local/mysql bindir=/usr/local/mysql/bin if test -z "$datadir" then datadir=/usr/local/mysql/data fi sbindir=/usr/local/mysql/bin libexecdir=/usr/local/mysql/bin else bindir="$basedir/bin" if test -z "$datadir" then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" fi
修改后的内容如下
if test -z "$basedir" then basedir=/mysqlsoft/mysql bindir=/mysqlsoft/mysql/bin if test -z "$datadir" then datadir=/mysqldata/mysql fi sbindir=/mysqlsoft/mysql/bin libexecdir=/mysqlsoft/mysql/bin else bindir="$basedir/bin" if test -z "$datadir" then datadir="$basedir/data" fi sbindir="$basedir/sbin" libexecdir="$basedir/libexec" fi [root@sjbf /]# cp /mysqlsoft/mysql/support-files/mysql.server /etc/init.d/mysqld [root@sjbf /]# chmod 755 /etc/init.d/mysqld
启动MySQL
[root@sjbf /]# service mysqld start Starting MySQL......[ OK ]
11.配置环境变量
[root@sjbf /]# # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. export MYSQL_HOME=/mysqlsoft/mysql/ export PATH=$PATH:$MYSQL_HOME/bin
12.登录Mysql
初始化成功后,查看初始化密码
[root@sjbf ~]# cat /mysqldata/mysql/mysql.err | grep password 2023-05-05T20:26:25.588664Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: #uVfq!szm7y/
并输入刚刚复制的密码,但是 却提示不能通过mysql.sock文件实现连接
[root@sjbf bin]# ./mysqladmin -u root -p password Enter password: mysqladmin: connect to server at 'localhost' failed error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)' Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
这里就奇怪了,因为在my.cnf文件中设置的socket文件路径为/mysqlsoft/mysql/mysql.sock,但mysql所使用的文件不是启动服务所生成的。
使用-S选项来指定生成的mysql.sock文件进行登录是可以成功登录的
[root@sjbf ~]# mysql -S /mysqlsoft/mysql/mysql.sock -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.33 Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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>
为了方便登录不每次指定-S参数可以在my.cnf文件中指定socket参数
[client] socket = /mysqlsoft/mysql/mysql.sock [root@sjbf tmp]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.33 Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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>
13.重置root用户密码
[root@sjbf tmp]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.33 Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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> set password=password("123456"); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password("123456")' at line 1
用5.7的方法修改密码会报错
mysql> ALTER user 'root'@'localhost' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.14 sec) mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.03 sec) 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.infoschema | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+------------------+ 4 rows in set (0.00 sec)
14.设置允许远程登录mysql
如果要远程访问数据库,只需要把拥有全部权限的root账号对应的记录的Host字段改为%就可以了
mysql> update user set host='%' where user='root'; Query OK, 1 row affected (0.13 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.03 sec) mysql> ALTER USER 'root'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER; Query OK, 0 rows affected (0.09 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> grant all privileges on *.* to 'root'@'%'; Query OK, 0 rows affected (0.14 sec) mysql> flush privileges; Query OK, 0 rows affected (0.04 sec)
测试远程登录
[root@sjbf tmp]# mysql -h 10.13.10.99 -P 3306 -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 8.0.33 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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> select host,user,plugin from user; +-----------+------------------+-----------------------+ | host | user | plugin | +-----------+------------------+-----------------------+ | % | root | caching_sha2_password | | localhost | mysql.infoschema | caching_sha2_password | | localhost | mysql.session | caching_sha2_password | | localhost | mysql.sys | caching_sha2_password | +-----------+------------------+-----------------------+ 4 rows in set (0.00 sec) [root@sjbf tmp]# mysql -h 10.13.10.99 -P 3306 -uroot -p123456 --ssl-cert=/mysqldata/mysql/client-cert.pem --ssl-key=/mysqldata/mysql/client-key.pem mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 17 Server version: 8.0.33 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. 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>