基礎環境centos7.5,mysql版本8.0.20,通過tar包安裝,安裝路徑/usr/local。
mysql官網:https://dev.mysql.com/downloads/mysql/
卸載centos7中自帶的mariadb
[root@localhost ~]# rpm -qa|grep mariadb mariadb-libs-5.5.56-2.el7.x86_64 [root@localhost ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
上傳並解壓安裝包mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz。注意壓縮包是.tar.xz格式的,需要先解壓、再解包。
[root@CentOS7-1 tools]# xz -d mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz [root@CentOS7-1 tools]# tar xf mysql-8.0.20-linux-glibc2.12-x86_64.tar -C /usr/local/
在安裝包下創建數據文件路徑data
[root@CentOS7-1 tools]# cd /usr/local/ [root@CentOS7-1 local]# mv mysql-8.0.20-linux-glibc2.12-x86_64/ mysql [mysql@CentOS7-1 local]$ cd mysql [mysql@CentOS7-1 mysql]$ mkdir data
創建mysql用戶
[root@CentOS7-1 mysql]# useradd mysql [root@CentOS7-1 mysql]# passwd mysql ……密碼相關提示操作……
重命名解壓后的安裝包目錄,並修改目錄的屬主屬組,
[root@localhost mysql]# cd .. [root@localhost local]# chown -R mysql.mysql mysql [root@localhost local]# ll 總用量 0 drwxr-xr-x. 2 root root 6 4月 11 2018 bin drwxr-xr-x. 2 root root 6 4月 11 2018 etc drwxr-xr-x. 2 root root 6 4月 11 2018 games drwxr-xr-x. 2 root root 6 4月 11 2018 include drwxr-xr-x. 2 root root 6 4月 11 2018 lib drwxr-xr-x. 2 root root 6 4月 11 2018 lib64 drwxr-xr-x. 2 root root 6 4月 11 2018 libexec drwxr-xr-x 10 mysql mysql 141 12月 8 14:12 mysql drwxr-xr-x. 2 root root 6 4月 11 2018 sbin drwxr-xr-x. 5 root root 49 12月 13 2020 share drwxr-xr-x. 2 root root 6 4月 11 2018 src
編輯配置文件/etc/my.cnf
[root@localhost local]# cat /etc/my.cnf [mysqld] port=3306 #3306端口 basedir=/usr/local/mysql # mysql的安裝目錄 datadir=/usr/local/mysql/data # mysql數據庫的數據的存放目錄 max_connections=10000 # 允許最大連接數 max_connect_errors=10 # 允許連接失敗的次數,防止有人從該主機試圖攻擊數據庫系統 character-set-server=utf8 # 服務端使用的字符集 default-storage-engine=INNODB # 默認存儲引擎 [mysql] default-character-set=utf8 # 設置mysql客戶端默認字符集
到bin目錄下初始化數據庫,記錄初始密碼
[mysql@CentOS7-1 local]$ cd mysql/bin [mysql@CentOS7-1 bin]$ ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize 2021-12-07T16:29:25.936536Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161) 2021-12-07T16:29:25.936549Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000) 2021-12-07T16:29:25.937104Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 2021-12-07T16:29:25.937260Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 111611 2021-12-07T16:29:25.954467Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2021-12-07T16:29:27.128120Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2021-12-07T16:29:28.520117Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: I<-VCdcL2kZb
將mysql添加到服務
[root@CentOS7-1 bin]# cd ../support-files/ [root@CentOS7-1 support-files]# cp -a mysql.server /etc/init.d/mysqld [root@CentOS7-1 support-files]# chmod +x /etc/init.d/mysqld [root@CentOS7-1 support-files]# chkconfig --add mysqld
配置環境變量/etc/profile
[root@localhost bin]# echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile [root@localhost bin]# source /etc/profile
啟動mysql
[root@localhost bin]# service mysqld start Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'. .... SUCCESS! [root@localhost bin]# service mysqld status SUCCESS! MySQL running (70760)
登錄mysql
[root@localhost bin]# mysql -uroot -p Enter password: 輸入初始化時生成的密碼 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.20 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用戶密碼即相關權限。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Cslc@pass123'; Query OK, 0 rows affected (0.00 sec) mysql> update user set host='%' where user = 'root'; ---設置root用戶可在任意主機登錄,即可遠程登錄 Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> flush privileges; ---刷新權限 Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on *.* to 'root'@'%'; Query OK, 0 rows affected (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
alter語句中,WITH mysql_native_password是為了可以通過sqlyog等mysql客戶端連接,否則會報加密錯誤。