MySQL學習筆記(平臺CentOS 6)mysql
一、添加mysql用戶c++
[root@mysql-slave ~]# groupadd mysql [root@mysql-slave ~]# useradd -s /sbin/nologin -g mysql -M mysql
二、配置安裝環境sql
[root@mysql-slave ~]# yum groupinstall 'Development Tools' -y [root@mysql-slave ~]# yum install gcc gcc-c++ make ncurses-devel bison perl -y # 依賴包 [root@mysql-slave tools]# tar -zxf cmake-2.8.12.tar.gz # 安裝cmake [root@mysql-slave cmake-2.8.12]# ./configure [root@mysql-slave cmake-2.8.12]# gmake [root@mysql-slave cmake-2.8.12]# gmake install
三、建立目錄/copy實例文件shell
[root@mysql-slave ~]# mkdir /usr/local/mysql [root@mysql-slave ~]# mkdir /db/{3306,3307}/{mysql,tmp} -p [root@mysql-slave ~]# tree /db /db ├── 3306 │ ├── mysql # 3306實例數據文件目錄 │ └── tmp # 3306 mysql.sock存放位置(此文件可靈活放置) └── 3307 ├── mysql # 3307實例數據文件目錄 └── tmp # 3307 mysql.sock存放位置
拷貝配置文件(my.cnf)和啓動腳本(mysqld)至目錄vim
3306實例配置my.cnf內容:安全
[client] port = 3306 socket = /db/3306/tmp/mysql.sock [mysqld] port = 3306 socket = /db/3306/tmp/mysql.sock datadir = /db/3306/mysql pid-file = /db/3306/mysql.pid back_log = 50 max_connections = 100 max_connect_errors = 10 table_open_cache = 2048 max_allowed_packet = 16M binlog_cache_size = 1M max_heap_table_size = 64M read_buffer_size = 2M read_rnd_buffer_size = 16M sort_buffer_size = 8M join_buffer_size = 8M thread_cache_size = 8 thread_concurrency = 8 query_cache_size = 64M query_cache_limit = 2M ft_min_word_len = 4 default-storage-engine = MYISAM thread_stack = 192K transaction_isolation = REPEATABLE-READ tmp_table_size = 64M log-bin=/db/3306/mysql-bin binlog_format=mixed slow_query_log long_query_time = 2 server-id = 1 key_buffer_size = 32M bulk_insert_buffer_size = 64M myisam_sort_buffer_size = 128M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 myisam_recover innodb_additional_mem_pool_size = 16M innodb_buffer_pool_size = 2G innodb_data_file_path = ibdata1:10M:autoextend innodb_write_io_threads = 8 innodb_read_io_threads = 8 innodb_thread_concurrency = 16 innodb_flush_log_at_trx_commit = 1 innodb_log_buffer_size = 8M innodb_log_file_size = 256M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 512M sort_buffer_size = 512M read_buffer = 8M write_buffer = 8M [mysqlhotcopy] interactive-timeout [mysqld_safe] open-files-limit = 8192 log-error=/db/3306/mysql_3306.err pid-file=/db/3306/mysql.pid
3306實例配置文件與3307不一樣的部分:socket
3306實例啓動腳本內容tcp
#!/bin/sh #init port=3306 # 3307這裏端口改爲‘3307’,其餘配置無需改動 mysql_user="root" mysql_pwd="" # 初始mysql沒有密碼 CmdPath="/usr/local/mysql/bin" # mysql程序目錄 mysql_sock="/db/${port}/tmp/mysql.sock" # mysql.sock文件位置 #startup function function_start_mysql() { if [ ! -e "$mysql_sock" ];then printf "Starting MySQL...\n" /bin/sh ${CmdPath}/mysqld_safe --defaults-file=/db/${port}/my.cnf 2>&1 > /dev/null & else printf "MySQL is running...\n" exit fi } #stop function function_stop_mysql() { if [ ! -e "$mysql_sock" ];then printf "MySQL is stopped...\n" exit else printf "Stoping MySQL...\n" ${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /db/${port}/tmp/mysql.sock shutdown fi } #restart function function_restart_mysql() { printf "Restarting MySQL...\n" function_stop_mysql sleep 2 function_start_mysql } case $1 in start) function_start_mysql ;; stop) function_stop_mysql ;; restart) function_restart_mysql ;; *) printf "Usage: /db/${port}/mysqld {start|stop|restart}\n" esac
最終結構:
ide
[root@mysql-slave ~]# tree /db /db ├── 3306 │ ├── my.cnf # 3306實例配置文件 │ ├── mysql │ ├── mysqld # 3306實例啓動/關閉程序 │ └── tmp └── 3307 ├── my.cnf # 3307實例配置文件 ├── mysql ├── mysqld # 3307實例啓動/關閉程序 └── tmp
四、受權學習
[root@mysql-slave ~]# chown -R mysql.mysql /usr/local/mysql [root@mysql-slave ~]# chown -R mysql.mysql /db [root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs chmod +x [root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs ls -l -rwxr-xr-x 1 mysql mysql 756 Aug 19 01:45 /db/3306/mysqld -rwxr-xr-x 1 mysql mysql 755 Aug 19 01:46 /db/3307/mysqld
五、安裝mysql
[root@mysql-slave tools]# tar -zxf mysql-5.5.37.tar.gz [root@mysql-slave tools]# cd mysql-5.5.37 [root@mysql-slave mysql-5.5.37]# cmake \ > -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \ > -DMYSQL_TCP_PORT=3306 \ > -DEXTRA_CHARSETS=all \ > -DENABLED_LOCAL_INFILE=ON \ > -DWITH_INNOBASE_STORAGE_ENGINE=1 \ > -DWITH_FEDERATED_STORAGE_ENGINE=1 \ > -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ > -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ > -DWITHOUT_PARTITION_STORAGE_ENGINE=1 \ > -DWITH_FAST_MUTEXES=1 \ > -DWITH_ZLIB=bundled \ > -DENABLED_LOCAL_INFILE=1 \ > -DWITH_READLINE=1 \ > -DWITH_EMBEDDED_SERVER=1 \ > -DWITH_DEBUG=0 [root@mysql-slave mysql-5.5.37]# make && make install [root@mysql-slave ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile [root@mysql-slave ~]# source /etc/profile
六、初始化mysql
[root@mysql-slave ~]# cd /usr/local/mysql/scripts/ [root@mysql-slave scripts]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/db/3306/mysql --user=mysql WARNING: The host 'mysql-slave' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK Filling help tables... OK # 看到兩個"OK",表示初始化成功 To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/local/mysql/bin/mysqladmin -u root password 'new-password' /usr/local/mysql/bin/mysqladmin -u root -h mysql-slave password 'new-password' Alternatively you can run: /usr/local/mysql/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems at [root@mysql-slave scripts]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/db/3307/mysql --user=mysql # 一樣方法初始化3307實例
初始化成功後,產生文件/目錄
[root@mysql-slave scripts]# ll /db/3306/mysql total 12 drwx------ 2 mysql root 4096 Aug 19 19:12 mysql drwx------ 2 mysql mysql 4096 Aug 19 19:12 performance_schema drwx------ 2 mysql root 4096 Aug 19 19:12 test [root@mysql-slave scripts]# ll /db/3307/mysql total 12 drwx------ 2 mysql root 4096 Aug 19 19:15 mysql drwx------ 2 mysql mysql 4096 Aug 19 19:15 performance_schema drwx------ 2 mysql root 4096 Aug 19 19:15 test
7、啓動mysql
[root@mysql-slave ~]# /db/3306/mysqld start [root@mysql-slave ~]# /db/3307/mysqld start [root@mysql-slave ~]# netstat -ntulp | grep 330* tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2809/mysqld tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 3498/mysqld
8、多實例mysql登錄/關閉
[root@mysql-slave ~]# mysql -S /db/3306/tmp/mysql.sock # 經過指定sock文件登錄(此時無需輸入密碼) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.37-log Source distribution Copyright (c) 2000, 2014, 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
關閉mysql實例(此時mysql沒設置密碼)
[root@mysql-slave ~]# /db/3306/mysqld stop Stoping MySQL... Enter password: # 直接回車,便可關閉3306 mysql實例 [root@mysql-slave ~]# netstat -ntulp | grep 330 tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 3498/mysqld
九、爲多實例mysql設置密碼/登錄/關閉
[root@mysql-slave ~]# /db/3306/mysqld start [root@mysql-slave ~]# mysqladmin -u root -S /db/3306/tmp/mysql.sock password 'q.1234' [root@mysql-slave ~]# mysqladmin -u root -S /db/3307/tmp/mysql.sock password 'q.1234' [root@mysql-slave ~]# mysql -S /db/3306/tmp/mysql.sock # 此時沒法經過sock直接登錄 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
配置密碼後,和mysql單實例相比,登錄時,需指定sock文件位置
[root@mysql-slave ~]# mysql -S /db/3306/tmp/mysql.sock -uroot -pq.1234 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.5.37-log Source distribution Copyright (c) 2000, 2014, 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>
配置密碼後,mysql實例關閉,有兩種方法:
方法一:在關閉時,輸入密碼
[root@mysql-slave ~]# /db/3306/mysqld stop Stoping MySQL... Enter password: # 輸入root的密碼 [root@mysql-slave ~]# netstat -ntulp | grep 330 tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 3498/mysqld
方法二:把密碼寫的腳本中(注意修改權限,安全起見)
[root@mysql-slave ~]# vim /db/3306/mysqld mysql_pwd="q.1234" [root@mysql-slave ~]# vim /db/3307/mysqld mysql_pwd="q.1234" [root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs ls -l -rwxr-xr-x 1 mysql mysql 989 Aug 19 20:28 /db/3306/mysqld -rwxr-xr-x 1 mysql mysql 989 Aug 19 20:29 /db/3307/mysqld [root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs chown root # 修改權限 [root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs chmod 700 [root@mysql-slave ~]# find /db -type f -name 'mysqld' | xargs ls -l -rwx------ 1 root mysql 989 Aug 19 20:28 /db/3306/mysqld -rwx------ 1 root mysql 989 Aug 19 20:29 /db/3307/mysqld [root@mysql-slave ~]# netstat -ntulp | grep 330 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 9865/mysqld tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 3498/mysqld [root@mysql-slave ~]# /db/3306/mysqld stop # 無需密碼,直接關閉mysql Stoping MySQL... [root@mysql-slave ~]# netstat -ntulp | grep 330 tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 3498/mysqld
10、遠程鏈接多實例mysql
mysql> select user,host,password from mysql.user; +------+-------------+-------------------------------------------+ | user | host | password | +------+-------------+-------------------------------------------+ | root | localhost | *312639BE4EF3C0F04E07B607FC0B2D60223203BA | | root | mysql-slave | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | mysql-slave | | +------+-------------+-------------------------------------------+ 6 rows in set (0.00 sec) mysql> grant all privileges on *.* to root@'172.18.10.%' identified by 'q.1234' with grant option; Query OK, 0 rows affected (0.00 sec) # 受權登錄 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> select user,host,password from mysql.user; +------+-------------+-------------------------------------------+ | user | host | password | +------+-------------+-------------------------------------------+ | root | localhost | *312639BE4EF3C0F04E07B607FC0B2D60223203BA | | root | mysql-slave | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | mysql-slave | | | root | 172.18.10.% | *312639BE4EF3C0F04E07B607FC0B2D60223203BA | +------+-------------+-------------------------------------------+ 7 rows in set (0.00 sec)
遠程登錄測試
[root@mysql-slave ~]# mysql -uroot -p -S /db/3306/tmp/mysql.sock mysql> show processlist; +----+------+-----------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+-------+------------------+ | 8 | root | localhost | NULL | Query | 0 | NULL | show processlist | +----+------+-----------+------+---------+------+-------+------------------+
[root@mysql-master ~]# mysql -uroot -pq.1234 -h 172.18.10.11 -P3306 # 遠程登錄只需指定端口便可
[root@mysql-slave ~]# mysql -uroot -p -S /db/3306/tmp/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 5.5.37-log Source distribution Copyright (c) 2000, 2014, 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 processlist; +----+------+--------------------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+--------------------+------+---------+------+-------+------------------+ | 10 | root | 172.18.10.10:50353 | NULL | Sleep | 101 | | NULL | | 11 | root | localhost | NULL | Query | 0 | NULL | show processlist | +----+------+--------------------+------+---------+------+-------+------------------+ 2 rows in set (0.00 sec)